2017-05-09 4 views
0

私はリモートホスト上で動作しているサービスを表示するPowershellスクリプトの応答を表示しようとしています。C#RunSpace for PowerShellを提供するには?

私はこれをPowershellで実行すると、目的の出力が得られます。しかし、私はC#を使用して同じをしようとすると、私は次の例外が表示されます。

このスレッドでスクリプトを実行するためのRunspaceはありません。 System.Management.Automation.Runspaces.RunspaceタイプのDefaultRunspaceプロパティに1つを指定できます。

私が使用しているコードは次のとおりです。

 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; 
      } 
     } 

編集:C#

System.Management.Automationを使用して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 

出力。 ExtendedTypeSystemException: に続くe " " ToString "を" 0 "引数で呼び出す例外:"このスレッドでスクリプトを実行するためのRunspace はありません。 System.Management.Automation.Runspaces.RunspaceタイプのDefaultRunspaceプロパティに1つを指定できます。あなたが起動しようとしたスクリプトブロック た:

PowerShellのバージョンが3 のVisual Studioバージョンである、私は同様の問題ときがあったように$ this.ServiceName「」---> 2010アルティメット

答えて

0

が見えるさスクリプトプロパティを呼び出します。しかし、あなたが探しているデータを得るには、これを最速に試してみてください。コマンド(varスクリプト)を変更し、次に、(thing.Members [%%%%%]。Value)に、プルまたは表示するPowerShellプロパティを配置する必要があるかどうかを確認します。 (下記参照)

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(); 
    } 
} 
関連する問題