Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

creating a Runspace with administrative privileges

$
0
0
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:
        '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()


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>