【发布时间】:2014-12-01 19:35:25
【问题描述】:
我尝试使用 C# 执行此 PowerShell 命令,并等待输出,这是我域中的 SharePoint 网站列表!! PowerShell 本身工作正常,但使用 C# 进程调用它,不等待输出!!!! 任何想法或建议表示赞赏。
//命令
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();
}
【问题讨论】:
标签: powershell c#-4.0 process