0
我需要執行以下腳本: Get-MailboxDatabase -Status |選擇服務器名稱,名稱,數據庫大小通過C#執行PowerShell腳本
我嘗試了一些Powershell和Command類的解決方案,但它們不起作用。我收到的錯誤: 值參數不能爲空。
我需要執行以下腳本: Get-MailboxDatabase -Status |選擇服務器名稱,名稱,數據庫大小通過C#執行PowerShell腳本
我嘗試了一些Powershell和Command類的解決方案,但它們不起作用。我收到的錯誤: 值參數不能爲空。
我認爲這會做你要找的內容:
private string RunLocalExchangePowerShell(string script)
{
// create the runspace and load the snapin
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
Runspace runSpace = RunspaceFactory.CreateRunspace(rsConfig);
runSpace.Open();
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
Pipeline pipeLine = runSpace.CreatePipeline();
// load the script and convert the output to a string
pipeLine.Commands.AddScript(script);
pipeLine.Commands.Add("out-string");
// get the results
Collection<PSObject> results;
results = pipeLine.Invoke();
// loop through the results and build the output
StringBuilder sb = new StringBuilder();
foreach (PSObject obj in results)
{
sb.AppendLine(obj.ToString());
}
// close the pipeline and runspace
pipeLine.Dispose();
runSpace.Close();
return sb.ToString();
}
用法示例:
Console.WriteLine(prog.RunLocalExchangePowerShell("Get-MailboxDatabase -Status | select ServerName,Name,DatabaseSize"));
你是如何初始化PSEXEC變量? – 2012-08-04 06:31:56
@TimurYaroshenko對不起,我編輯了上述內容並進行了測試。 – 2012-08-04 14:28:11
非常感謝你;-) – 2012-08-04 20:44:09