Tuesday, September 28, 2010

App pool restart using POSH

We sometimes want to restart an app pool on a server that is either misbehaving, or coud be locked up.  Here is a script to return all the app pools that are running on the server you specify, and enables you to restart just the app pool you want. This is my raw script, including some code I was expirementing with at the end of the script.

#############################################################################################
#
#Simple script to recycle an app pool of the User's choice on the server of the user's choice
#
# May 24, 2010
#Bryan Loveless 
#
#This must have PowerShell v2 or newer, find it at http://support.microsoft.com/kb/968929 if
# running win2k r1 or earlier. run Get-host if you are not sure what version you have.
# AS OF THIS DATE, THIS SCRIP MUST NOT USE POWERGUI, USE ISE TO MODIFY OR RUN.
#
#############################################################################################
#import-module webAdministration

#get servername to restart the pools on
$servername = Read-Host "Enter the name of the server to restart the pools on."

#connect to the server listed above
$connection = New-PSSession -ComputerName $servername 

#incase there are mutiple machines listed, this should keep track of them all (future implementation)
$psall = Get-PSSession

Write-Host "Here are the avalible app pools on the server you selected."

# run the command to see what app pools there are, but not enter the session yet.
# invoke-command -ComputerName $servername {get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool"} 


#Enter-PSSession -ComputerName $severname  #this way doesnt seem to work, see line below
# $connection | Enter-PSSession
Enter-PSSession -Session $connection

# tried a wait event to establish the connection, didn't work
# wait-event -timeout 5


#perhaps above return just the app pool name and status?
$pools= (get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool")

foreach($item in $pools)
{
 Write-Host $item.name
 Write-Host $item.status

}


$apppoolname = Read-Host "What is the name of the app pool you want to restart?"

#entering the session above so we can see the apps running
#Enter-PSSession -ComputerName $severname

#$appPoolName = $args[0]
$appPool = (get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"})

# it doesnt like to "restart" pools that are stopped, so I will stop and start them instead of recycle
# $appPool.Recycle()
$appPool.Stop()
$appPool.Start()


#end the connection to the server above
Exit-PSSession 
#Remove-PSSession -session $psall

#to help close sessions while coding
#$psall = Get-PSSession
#Remove-PSSession $psall

#Other interesting things that can be done with this variable once declared:
#
#EnumAppsInPool      Method       System.Management.ManagementBaseObject Enum...
#Recycle             Method       System.Management.ManagementBaseObject Recy...
#Start               Method       System.Management.ManagementBaseObject Start()
#Stop                Method       System.Management.ManagementBaseObject Stop()
#Caption             Property     System.String Caption {get;set;}
#Description         Property     System.String Description {get;set;}
#InstallDate         Property     System.String InstallDate {get;set;}
#Name                Property     System.String Name {get;set;}
#Status              Property     System.String Status {get;set;}
#

#Another interesting bit of code to borrow:
#function recycle-pool($strServerName)
#{
#    $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
#    $objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
#    $objWMI.Scope.Options.Authentication = 6
#    $pools = $objWMI.Get()
#    foreach ($pool in $pools)
#    {
#        $pool.recycle()
#        if (!$?)
#        {
#            Write-Host $pool.name " - ERROR"
#        }
#        else
#        {
#            Write-Host $pool.name " - Recycled"
#        }
#}


1 comment:

  1. Hi friends,

    An application pool is a group of one or more URLs that are served by a worker process or a set of worker processes. It sets boundaries for the applications they contain, which means that any applications that are running outside a given application pool cannot affect the applications in the application pool. Thanks a lot.....

    Public Folders

    ReplyDelete