Bishal Thingom
Bishal Thingom

Reputation: 131

How to provide C# RunSpace for PowerShell?

I'm trying to print response of Powershell script which shows me services running on a remote host.

Get-Service -ComputerName "ComputerName"

When I run this on Powershell, I get the desired output. But when I try the same using C#, I get the following exception.

There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type.

The code that I'm using is:

       var powerShell = PowerShell.Create().AddScript(script);

        var results = powerShell.Invoke();

        foreach (var item in results)
        {
            try
            {
                Console.WriteLine(item);
            }
            catch (ExtendedTypeSystemException e)
            {
                Console.WriteLine(e);
                break;
            }
        }

Edit: Output in PowerShell

Status   Name               DisplayName
------   ----               -----------
Stopped  AdtAgent           Microsoft Monitoring Agent Audit Fo...
Stopped  AeLookupSvc        Application Experience
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
Running  Appinfo            Application Information
Stopped  AppMgmt            Application Management
Stopped  AppReadiness       App Readiness
Stopped  AppXSvc            AppX Deployment Service (AppXSVC)
Stopped  AudioEndpointBu... Windows Audio Endpoint Builder
Stopped  Audiosrv           Windows Audio
Running  BFE                Base Filtering Engine
Running  BITS               Background Intelligent Transfer Ser...
Running  BrokerInfrastru... Background Tasks Infrastructure Ser...
Stopped  Browser            Computer Browser
Running  CcmExec            SMS Agent Host
Running  CertPropSvc        Certificate Propagation
Stopped  CmRcService        Configuration Manager Remote Control
Running  COMSysApp          COM+ System Application
Running  CryptSvc           Cryptographic Services
Running  DcomLaunch         DCOM Server Process Launcher
Stopped  defragsvc          Optimize drives

Output using C#

System.Management.Automation.ExtendedTypeSystemException: The following exception occurred while retrieving the string: "Exception calling "ToString" with "0" argument(s): "There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: $this.ServiceName"" --->

PowerShell version is 3. Visual Studio version is 2010 Ultimate

Upvotes: 4

Views: 2195

Answers (1)

Anthony Howell
Anthony Howell

Reputation: 21

Looks like I had a similar problem when calling a script property. But to get the data you're looking for the fastest try this. Modify the command (var script), then where the (thing.Members[%%%%%].Value) is you need to put the PowerShell Property that you want to pull or view. (noted below)

class Program
{
    static void Main(string[] args)
    {
        var script = "Get-NetIPAddress ";

        var powerShell = PowerShell.Create().AddScript(script).Invoke();

        foreach (var thing in powerShell)
        {
            try
            {
                //the Members must be PROPERTIES of the PowerShell object
                var name = (thing.Members["InterfaceAlias"].Value);
                var ip= (thing.Members["IPv4Address"].Value);

                Console.WriteLine("Connection: " + name + " .......... IP: " + ip);
            }
            catch
            {

            }

        }

        Console.Read();
    }
}

Upvotes: 1

Related Questions