I want something simple: List all websites (for now just on my own IIS7, but later for the serverfarm). I'd like to use powershell-4 commands, like Get-WebBinding or Get-Website, because pShell is easily executed remote on other servers.
I want like to trigger the pShell from an intranet webpage, to display a live domain-binding-overview per host of the server farm.
The Script works fine in Powershell window itself, called from C# -WIN forms it also works, but called from webpage (MVC5) it returns only the site the page is hosted on, instead of all sites .. What's going on??
I'm calling it directly from browser using the url "http://localwebsite/getsites".
C# code:
[ 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;
}
Interesting! Get-WebBinding does not return the last site in the list, but only the name of the site where the c# code runs!! I'm calling the code directly from the browser using a [Route("getsites")]
The test:
Import-Module WebAdministration
Get-WebBinding | Out-File "e:\temp\out-file.txt"
Calling it using the browser (using mvc-snippet from above)
http://localwebsiteName.nl/getsites
Output:
protocol bindingInformation
-------- ------------------
http *:80:localwebsiteName.nl
I still don't understand why, because the IIS app pool runs under my personal domain account with all the admin rights .. anyone has an idea?
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
Why do I bother? Because now I can addd servers just by adding their names and enableing pShell remoting