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;
}