我想要一些简单的东西:列出所有的网站(目前只在我自己的IIS7上,但稍后为服务器场)。我想使用powershell-4命令,比如或Get-网站,因为pShell很容易在其他服务器上远程执行。
我想从内部网网页中触发pShell,以显示服务器场的每个主机的活动域绑定概述。
该脚本在Powershell窗口本身运行良好,它也可以从C# -WIN窗体调用,但它只返回页面所在的站点,而不是所有站点。到底怎么回事??
我使用url "http://localwebsite/getsites“从浏览器中直接调用它。
C#代码:
[ Route("getsites") ]
public string test(string machine)
{
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
var pipeline = runspace.CreatePipeline();
//string script = @"Get-Item ""IIS:\sites\*""";
string script = @"Import-Module WebAdministration; Get-WebBinding";
pipeline.Commands.AddScript(script);
pipeline.Commands.Add("Out-String");
var results = pipeline.Invoke();
runspace.Close();
var stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
//results has just 1 element
stringBuilder.AppendLine(obj.ToString());
}
var result = stringBuilder.ToString();
//result contains just the current site instead of all 10 sites
return result;
}发布于 2016-02-03 16:59:55
有意思的!Get-WebBinding不返回列表中的最后一个站点,而是只返回运行c#代码的站点的名称!!我使用一个路由(“getsite”)直接从浏览器调用代码
测试:
Import-Module WebAdministration
Get-WebBinding | Out-File "e:\temp\out-file.txt"使用浏览器调用它(使用上面的mvc片段)
http://localwebsiteName.nl/getsites输出:
protocol bindingInformation
-------- ------------------
http *:80:localwebsiteName.nl 我仍然不明白为什么,因为IIS应用程序池在我的个人域帐户下运行,拥有所有的管理权限。有人有主意吗?
Final update: This behaviour appears a limitation in WebAdminstration package.
the same Ps-script returns ALL websites (as expected) when executed remotely.
Call seq:
browser > local MVC > pShell > remote pShell > local MVC > browser我为什么要费心?因为现在我可以通过添加服务器名和启用pShell远程处理来添加服务器。
https://stackoverflow.com/questions/35159410
复制相似问题