【发布时间】:2020-04-01 08:38:29
【问题描述】:
我正在尝试使用 Powershell 从 C# 读取我的 IIS 本地网站 但我总是得到一个空输出
public static ManageWebsite GetWebSiteStatus(string websiteName)
{
ManageWebsite websiteState = new ManageWebsite();
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
PowerShell ps = PowerShell.Create(); // Create a new PowerShell instance
ps.Runspace = runspace; // Add the instance to the runspace
ps.Commands.AddScript(@"Get-Website -Name """ + websiteName + @""" | %{$_.state}"); // Add a script
Collection<PSObject> results = ps.Invoke();
string powershell_output = string.Empty;
runspace.Close();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
powershell_output = obj.BaseObject.ToString();
}
websiteState.Status = powershell_output;
websiteState.WebSite = websiteName;
return websiteState;
}
我不知道为什么,我总是使用该函数在 C# 中使用 powershell 调用其他东西,并且总是可以正常工作
【问题讨论】:
标签: c# asp.net powershell iis web-testing