Tuesday, June 22, 2010

Logging of IIS AppPools status for remote servers (Win 2003) using PowerShell

Hi,

I was having a struggle in writing a script which would log the status of the App Pool from across all the remote servers. I tried using two things -

1> ADSI
2> Get-WMIObject

Using the ADSI, I faced issue in which it was not able to access the remote server. It used to say RPC server unavailable. But the same when I tried using Get-WMIObject, it was able to get data from remote server. The catch here was, the Children class was availabe from ADSI but the same was not found using Get-WMIObject . Finally I was able to write the script using the Get-WMIObject.

----------------------

Param(



[string]$global:computer = $env:COMPUTERNAME


,[string]$LogDir = "d:\AppPoolLogging"


,[switch]$doNotRestart


,[switch]$enableLogging = $true


)






function Convert-AppPoolState([int]$value){


switch ($value){


1 {"Starting"; break}


2 {"Started" ; break}


3 {"Stopping"; break}


4 {"Stopped" ; break}


default {"Unknown"}


}


}






Function Write-Log($appPoolName,$message){


$dateTime = Get-Date -format "yyyyMMdd"


$LogFile = "$dateTime.log"


$logPath = join-path $LogDir $LogFile


$output = (Get-Date -Format "yyyyMMdd_HH:mm:ss") + "|" + $computer + "|" + $appPoolName + "|" + $message


$output


If ($enableLogging) {$output | Out-File $logPath -Append}


}






Function Trim-AppPoolName ($appPoolName){
[string]$appPoolName.Substring(15)
}



$computers = Get-Content "c:\computerlistall.txt"


foreach ($computer in $computers)


{



$iis = Get-WmiObject -Namespace root\MicrosoftIISv2 -Class IIsApplicationPoolSetting -ComputerName $computer -Authentication PacketPrivacy


$iis

$i = $iis.Count

 
for ($k = 0; $k -lt $i; $k++)
{



$name = $iis[$k].Name


$ApplicationPoolName = Trim-AppPoolName $name


$state = $iis[$k].AppPoolState


$Status = Convert-AppPoolState $state


Write-Log $ApplicationPoolName $Status


}


}

3 comments:

  1. I am using your script on my domain but getting some error. Not much familiar with PowerShell. The below error I get:

    Get-WmiObject : Access denied

    all the servers are in same domain.

    ReplyDelete
  2. Sorry for responding late. May be you are not an administrator on the server. try by adding yourself as an administrator.

    ReplyDelete
  3. + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    Get-WmiObject : Invalid namespace "root\MicrosoftIISv2"
    At C:\Users\rksingh3\AppData\Local\Temp\1e809d56-9804-454b-84ac-c93a04a61ab6.ps1:99 char:8
    + $iis = Get-WmiObject -Namespace root\MicrosoftIISv2 -Class IIsApplicationPoolSet ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    ..can u help with above error

    ReplyDelete