1

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;
}
Arjan
  • 469
  • 4
  • 12
  • 2
    If PowerShell can do it, so can C#, why go round the houses: [List all websites in IIS c#](http://stackoverflow.com/questions/4593412/list-all-websites-in-iis-c-sharp) – Alex K. Feb 02 '16 at 17:10
  • thanks but .. In a next step I'd like to run the PScript remotely on several servers :) I'm developing a live domain listing of the whole server farm .. available on 1 convenient intranet page. Seemed most lightweight using PS, util I ran into this issue – Arjan Feb 02 '16 at 19:39
  • Are all your servers in the same Active Directory domain? – Kev Feb 02 '16 at 19:47
  • hey, no they are not in the office domain, they need to be reachable from outside.. – Arjan Feb 02 '16 at 19:54
  • Presume you have WinRM/PowerShell remoting bits set up to run these powershell bits remotely? – Kev Feb 02 '16 at 20:05
  • not running remotely yet, first get it working locally ;) – Arjan Feb 02 '16 at 20:14

1 Answers1

0

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

Arjan
  • 469
  • 4
  • 12