Script
$Username="Username" $Password="Password" function RecycleAppPool($Server,$AppPool) { Write-Host "ServerName:$Server ApplicationPool:$AppPool" # Credentials $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password # Checking App Pool by name $AppItem = (Invoke-Command $Server -ScriptBlock{param($AppPool) Get-WebAppPoolState | Where-Object {$_.ItemXPath -match "$using:AppPool"} } -ArgumentList $AppPool -Credential $Cred)[0] If($AppItem -ne $Null) { try { # Recycle App Pool $Recycle = Invoke-Command $Server -ScriptBlock{param($AppPool)Restart-WebAppPool -name "$using:AppPool"} -ArgumentList $AppPool -Credential $Cred Write-Host "Recycle Successful" } catch { Write-Host "Error: " -NoNewline -ForegroundColor red Write-Host $_.Exception.Message Contiune } } } RecycleAppPool "ServerName" "AppPoolName"
You can modify script based on your requirement
No comments:
Post a Comment