Tag Archives: powershell

Windows Service Bus 1.1: looking for Powershell script to delete topic/subscription

Questions: I would like to delete SB topic and their subscription through power-shell script. I’m able to delete using C# code which available within Microsoft.ServiceBus namespace and NamespaceManager class. public void DeleteSubscription(string topicPath, string name); But looking for power-shell script? I got below script which delete entire SB namespace. Remove-SBNamespace –Name ‘ServiceBusDefaultNamespace’ -Force Please suggest… Read More »

Powershell and PDFSharp, pass custom objects to function?

Questions: I am trying to create a PDF file based on some data that a powershell script retrieves from a computer. I have the following code Add-Type -Path \\xxxx\yy\aaaa\bbb\cccc\PdfSharp.dll $doc = New-Object PdfSharp.Pdf.PdfDocument $doc.Info.Title = “Created dynamically” $page = $doc.AddPage() $gfx = [PdfSharp.Drawing.XGraphics]::FromPdfPage($page) $pen = New-Object PdfSharp.Drawing.XPen([PdfSharp.Drawing.XColors]::Black, 0.5) #$gfx.DrawRectangle($pen,left start, top start, width, height) $gfx.DrawRectangle($pen,80,… Read More »

How to invoke C# method from PowerShell whose signature has parameters marked with 'params'

Questions: I’m trying to invoke a C# library method from PowerShell that has the following signature: public void Load<T>(T thing, params Expression<Func<T, object>>[] retrievals); For the ‘retrievals’ parameter, I only want to pass just one Expression argument, but when I do, PowerShell gives me the following error: Cannot find an overload for “Load” and the… Read More »

How to execute multi-line powershell command

Questions: Trying to execute a PowerShell command in asp.net c# but it returns no results, where am I going wrong? var shell = PowerShell.Create(); var script = $@”$Groups = Get-ADGroup {groupname}; $members = ForEach ($Group in $Groups) {{Get-AdGroupMember -Identity $Group -Recursive}}; $members | Get-AdUser -Properties Department | Select-Object Name, Department | Sort Department, Name”; shell.Commands.AddScript(script);… Read More »

Strange issue passing parameters to PowerShell from C#

Questions: I have a C# method in my program which can invoke powershell scripts, taking the arguments as an array of strings then passing it to PowerShell, strangely it seems to work fine for one script which takes integers as arguments, but fails for another which takes strings, below is the method and the scripts… Read More »

How to enable privileges in System.Management.Automation.PowerShell pipeline instance?

Questions: I have made a c# console application that is initiating a powershell pipeline instance and adding command “Stop-Computer” to execute, but when it is executing, its giving an error “Privileges not held”. This is my code. using(System.Management.Automation.PowerShell powershellInstance = System.Management.Automation.PowerShell.Create()) { powershellInstance.AddCommand(“Stop-Computer”); powershellInstance.Invoke(); if(powershellInstance.Streams.Error.Count > 0) { foreach(var error in powershellInstance.Streams.Error) { Console.WriteLine(error); }… Read More »

How to read errors from Powershell pipeline in c#?

Questions: I am using System.Management.Automation.Runspaces.Pipleline to create a powershell pipeline instance and execute my powershell scripts in c# console application, the problem is that if the script end up with an error, then i don’t know how to print that error on the console screen. This is my code, System.Management.Automation.Runspaces.Runspace PowershellRunspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(); PowershellRunspace.Open(); System.Management.Automation.RunspacesPipeline… Read More »