我試着用C#執行這個PowerShell命令,並等待輸出,這是我域中的SharePoint網站列表! PowerShell本身工作正常,但使用C#進程調用它,不會等待輸出! 任何意見或建議表示讚賞。使用C調用Powershell命令#
//命令
string cmd = @"[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint') > $null
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
$webapps = @()
foreach ($websvc in $websvcs) {
foreach ($webapp in $websvc.WebApplications) {
foreach ($site in $webapp.Sites) {
$site.URL
}
}
} ";
// C#的過程!
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo("powershell.exe", cmd);
proc.Start();
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.WaitForExit();
string output = "";
while (!proc.HasExited)
{
output += proc.StandardOutput.ReadToEnd();
}
非常感謝:) – 2014-12-02 22:02:25
@KeithHill非常感謝:) – 2014-12-02 22:02:43
非常感謝,對不起我的小PowerShell體驗: 這是我得到的異常, 例外:您無法在空值表達式上調用方法。\ n行:3 char:50 \ n + $ websvcs = $ farm.Services | -FilterScript {$ _。GetType()-eq – 2014-12-02 23:03:27