I'm currently trying to use the Runspace class to run a powershell command in vb.net.
The command NEEDS to be run with administrative privileges I want to use a pipeline to run the command, so i have a more organic output and a more seamless run of the powershell command.
I was looking into applying AuthorizationManager tot he RunspaceConfiguration, but i can't figure out how to get it to work. Is there a way to do what i'm attempting?
He's my code so far:
The command NEEDS to be run with administrative privileges I want to use a pipeline to run the command, so i have a more organic output and a more seamless run of the powershell command.
I was looking into applying AuthorizationManager tot he RunspaceConfiguration, but i can't figure out how to get it to work. Is there a way to do what i'm attempting?
He's my code so far:
'Configure the powershell runspace to run as an administrator Dim config As RunspaceConfiguration = RunspaceConfiguration.Create() Dim warning As New PSSnapInException 'Create Powershell runspace Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace(config) 'Open runspace MyRunSpace.Open() 'Create a pipeline and feed it script text Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline() MyPipeline.Commands.AddScript("add-pssnapin citrix.*") MyPipeline.Commands.AddScript("set-xadefaultcomputername -computername <computerName>") MyPipeline.Commands.AddScript("get-xaapplication -browsername <applicationName>") 'add an extra command to transform the script output objects into nicely formatted strings 'remove this line to get the actual objects that the script returns. For example, the script '"Get-Process" returns a collection of system.diagnostics.process instances. MyPipeline.Commands.Add("Out-String") 'Execute script Dim results As Collection(Of PSObject) = MyPipeline.Invoke() 'Close runspace MyRunSpace.Close() 'Convert script result into a single string Dim MyStringBuilder As New StringBuilder() For Each obj As PSObject In results MyStringBuilder.Append(obj.ToString()) Next 'return the results of the script that has 'now been converted to text Return MyStringBuilder.ToString()