我试着用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