私はリモートホスト上で動作しているサービスを表示する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アルティメット