How to get all running sites of an IIS webserver using powershell, called from the context of a website? RRS feed

  • Question

  • Hi!

    I am having some problem with displaying the list of running websites of an IIS webserver on a website (running on this observed server).

    I use the following Powershell script to get the sites:

    Import-Module webadministration
    Get-ChildItem IIS:\Sites | where {$_.State -eq 'Started'} | select Name

    This gives the complete list of sites when I run it in Powershell, however when my website calls the script, it gives back only the list of those websites which are in the application pool of the calling site. I made changes in the web.config files, so that the site is trusted, and is run using Local System identity, but still the same result.

    Is there anything I could do to get ALL sites from ALL application pools?

    Thanks, Szabolcs

    Monday, July 4, 2011 9:07 AM


All replies

  • impersonate as a user who as apropriate permissions for all pools (eg an administrator should definetly work)

    Monday, July 4, 2011 10:20 AM
  • Thanks for the tip, I've just tried using this impersonator, but still the same result:(

    Here is my relevant code:



    using (Impersonator imp = new Impersonator("username", "domain", "psw"))
            string pscriptPath = @"c:\Scripts\getSites.ps1";
            string pscript = String.Empty;
            if (System.IO.File.Exists(pscriptPath))
              pscript = System.IO.File.ReadAllText(pscriptPath);
              throw new System.IO.FileNotFoundException("Wrong path for the script file");
            Runspace runSpace = RunspaceFactory.CreateRunspace();
            RunspaceInvoke scriptInvoker = new RunspaceInvoke(runSpace);
            Pipeline pipeline = runSpace.CreatePipeline();
            Command registrationCommand = new Command(pscriptPath);
            Collection<PSObject> results = pipeline.Invoke();
    // ...


    Monday, July 4, 2011 12:52 PM
  • I put this post up about impersonation.  You don't need the machine key encryption, but probably the bit at the top.  That is, if you can log in as the user and store the credentials to disk with the user account.  

    Otherwise you could encrypt the password with machine key like the blog post talks about and then grant access for the service account to the key used to encrypt.
    write-host ((0..56)|%{if (($_+1)%3 -eq 0){[char][int]("116111101110117102102064103109097105108046099111109"[($_-2)..$_] -join "")}}) -separator ""
    Monday, July 4, 2011 5:18 PM