我有一個PowerShell腳本存儲在一個名爲「腳本」與內容串:如何添加PowerShell的管理單元在C#
get-user | out-file C:\Users\user\Desktop\user.txt -append
我的C#代碼:
RunspaceConfiguration runConfig = RunspaceConfiguration.Create();
PSSnapInException psEx = null;
runConfig.AddPSSnapIn("VMWare.View.Broker", out psEx);
Runspace runspace = RunspaceFactory.CreateRunspace(runConfig);
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);
Collection<PSObject> results = new Collection<PSObject>();
results = pipeline.Invoke();
runspace.Close();
如果我調試代碼中,我得到以下異常:
No snap-ins have been registered for Windows Powershell Version 2
如果我手動運行該腳本,並添加管理單元,它工作正常
我得到'因爲以下錯誤無法加載Windows PowerShell管理單元Microsoft.Exchange.Management.PowerShell.E2010:'Microsoft.Exchange.Data.Directory.Globals'的類型初始值設定項引發異常。有沒有人遇到過這種情況? – BRogers
問一個新問題,@BRogers - 這不是正確的地方。 – x0n