Querying IIS 6.x - Output Issue

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Jason Christie Jason Christie 8 months, 1 week ago.

  • Author
    Posts
  • #37076
    Profile photo of Jason Christie
    Jason Christie
    Participant

    Hi,

    I am querying a number of web servers running IIS 6.0 to determine the ServerComment, ServerState, Bindings, Port and Site Name.

    I am able to collect all of the data into a CSV without issue apart from the ServerState. All of the data is being written to the CSV one line at a time which is what I want but unfortunately I can't get the ServerState to do the same. I get as an example "4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2" on each line instead of the individual site status on each line. What am I doing wrong? I have only been using PowerShell for a few months so my knowledge is extremely limited but growing daily.

    
    $file = Get-Content "C:\Temp\PowerShellScripts\IISQuery\servers.txt"
    $OutputDir = "C:\Temp\PowerShellScripts\IISQuery\Output"
    $OutputFile = Join-Path $OutputDir "IIStest.csv"
    Add-Content -Path $OutPutFile -Value "ComputerName, ServerComment, Status, Bindings, Port"
    foreach ($computername in $file)
    {
    $IISWebServer = get-WmiObject -Namespace "root\MicrosoftIIsV2" -ComputerName $computername -query 'Select * from IIsWebServer' -Authentication 6
    $IISWebServerSet = get-WmiObject -namespace "root\MicrosoftIISv2" -ComputerName $computername -query 'Select * from IIsWebServerSetting' -Authentication 6
    $PScomputername = $IISWebServerSet.PScomputername
    $ServerComment = $IISWebServerSet.ServerComment
    $SiteStatus = $IISWebServer.ServerState
    $Bindings = [string]::join(';',($IISWebServerSet.ServerBindings | select -expand hostname))
    $Port = [string]::join(';',($IISWebServerSet.ServerBindings | select -expand Port))
    $SiteName = $IISWebServerSet.Name
    
    Add-Content -Path $OutPutFile -value "$PScomputername,$ServerComment,$SiteStatus,$Bindings,$Port"
    
    }
    
    

    Thank you in advance.

    Jason

  • #37078
    Profile photo of Michael Maher
    Michael Maher
    Participant

    Hi Jason,

    If you pipe the ServerState property to Get-Member it shows up as an int.

    $IISWebServer = get-WmiObject -Namespace "root\MicrosoftIIsV2" -ComputerName $c -query 'Select * from IIsWebServer' -Authentication 6
    
    $IISWebServer.ServerState | gm
    
    TypeName: System.Int32
    

    You could use a table to look up the returned value and return it in a friendly format.

    Starting = 1,
    Started = 2,
    Stopping = 3,
    Stopped = 4,
    Pausing = 5,
    Paused = 6,
    Continuing = 7

    Michael

  • #37079
    Profile photo of Michael Maher
    Michael Maher
    Participant

    On reflecting on your question again it looks like you might even be asking about a multi-dimensional array? Multiple web servers having multiple web sites.

    If that's the case then an Excel row per server probably isn't the best format for presenting this data.

  • #37080
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    I've modified your code a bit

    
    $file = Get-Content "C:\Temp\PowerShellScripts\IISQuery\servers.txt"
    $OutputDir = "C:\Temp\PowerShellScripts\IISQuery\Output"
    $OutputFile = Join-Path $OutputDir "IIStest.csv"
    $serverdata = @()
    
    foreach ($computername in $file)
    {
    $IISWebServer = Get-WmiObject -Namespace root\MicrosoftIIsV2 -Class IISWEbServer -ComputerName $computername -Authentication 6
    foreach ($webserver in $IISWebServer) {
    
    $IISWebServerSet = Get-WmiObject -Namespace root\MicrosoftIISv2 -Class IISWebServerSetting -ComputerName $computername -Filter "Name='$($webserver.Name)'"  -Authentication 6
    
    $serverdata += New-Object -TypeName PSObject -Property @{
    PScomputername = $IISWebServerSet.PScomputername
    ServerComment = $IISWebServerSet.ServerComment
    SiteStatus = $webserver.ServerState
    Bindings = [string]::join(';',($IISWebServerSet.ServerBindings | select -expand hostname))
    Port = [string]::join(';',($IISWebServerSet.ServerBindings | select -expand Port))
    SiteName = $IISWebServerSet.Name
    }
    
    } ## end of foreach ($webserver in $IISWebServer)
    
    }  ## end of foreach ($computername in $file)
    $serverdata | Export-Csv -Path $OutputFile -NoTypeInformation
    

    First off you're creating a csv file the hard way by doing the formatting your self. Let PowerShell do the work for you

    The first few lines are the same then I create an empty array
    $serverdata = @()
    This will hold the output data
    I've modified your WMI calls. Don't use a query – just give the class name you want to use. Very well done on picking up that you need the -authentication parameter – thats the one that trips most people.

    Your $IISWebserver variable will contain one entry for each site on your web server so to match everything up use another foreach loop to work through them

    Use a filter when access IISWebServerSetting (again no need for the query) to restrict the result to the site you want
    Add an object to your array that holds the values you need

    At the end use Export-Csv to write the data into CSV file.
    If you need further explanation please don't hesitate to ask

  • #37081
    Profile photo of Jason Christie
    Jason Christie
    Participant

    Hi Michael – many thanks for your replies.

    All of the data is being returned as expected except the ServerState. What is happening, is that for each website, instead of it presenting a single value for the state e.g. 4 for stopped, it is inserting the combined status of all of the queried sites into a single cell for each site e.g. "4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2". On the web server I am querying I have 19 sites.

     
    PSComputerName,ServerComment,SiteStatus,Bindings,Port,Name
    Hostname,Site1,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80,W3SVC/123
    Hostname,Site2,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/124
    Hostname,Site3,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80;80;80,W3SVC/125
    Hostname,Site4,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/126
    Hostname,Site5,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/127
    Hostname,Site6,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80;80,W3SVC/128
    Hostname,Site7,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80,W3SVC/129
    Hostname,Site8,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80,W3SVC/130
    Hostname,Site9,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/131
    Hostname,Site10,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/132
    Hostname,Site11,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/133
    Hostname,Site12,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80,W3SVC/134
    Hostname,Site13,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/135
    Hostname,Site14,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80;80,W3SVC/136
    Hostname,Site15,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/137
    Hostname,Site16,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/138
    Hostname,Site17,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/139
    Hostname,Site18,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80;80,W3SVC/140
    Hostname,Site19,4 2 2 2 2 4 2 2 2 2 2 2 2 2 2 4 4 2 2,"URL, URL, URL",80;80,W3SVC/141
     

    Cheers,

    Jason

  • #37090
    Profile photo of Jason Christie
    Jason Christie
    Participant

    Thank you very much Richard, your assistance is very much appreciated.

    Your modifications and explanations are very welcome and have helped me learn something new about PowerShell, which has made me a very happy boy.

    Have a great day.

You must be logged in to reply to this topic.