We use the following PowerShell script to automate a policy server restart. We call it from a batch file which runs in the task scheduler (also included below). I hope this is helpful.
-- Keats
##
## restartsmps.ps1
## Script to gracefully shutdown and restart the Siteminder policy server process
##
## Generate log file per day
$Logfile = "d:\logs\restartsmps$(get-date -f yyyyMMdd).log"
Function writeLog
{
Param ([string]$logstring)
Write-Host "$(Get-Date -Format g) - $logstring"
If ($Logfile)
{
Add-content $Logfile -value "$(Get-Date -Format g) - $logstring"
}
}
$processName = "SmPolicySrv"
# stop service in the background
Stop-Service -Name $processName -Force -Verbose
# loop N times and check status for "Stopped"
For ($i=1; $i -lt 20; $i++)
{
#give it n seconds to stop
Start-Sleep -Seconds 10
$SERVICESTATE = (Get-Service | where{$_.Name -eq $processName}).Status
#Write-Host $i, $SERVICESTATE
writeLog "Stopping $processName. Attempt $i. Service state=$SERVICESTATE"
If ($SERVICESTATE -eq "Stopped") { Break }
}
$SERVICESTATE = (Get-Service | where{$_.Name -eq $processName}).Status
If ($SERVICESTATE -eq "Stopped")
{
# Give it a few more seconds just to be safe and then restart the service
Start-Sleep -Seconds 10
Start-Service -Name $processName
Start-Sleep -Seconds 5
$SERVICESTATE = (Get-Service | where{$_.Name -eq $processName}).Status
writeLog "Restarted service $processName. Status is now $SERVICESTATE"
}
Else
{
writeLog "ERROR: Failed to stop service $processName"
}
======================================
:
: RestartSMPS.bat
: Batch file to be used from task scheduler for invoking a PowerShell script
:
SET SCRIPT_NAME=D:\scripts\restartsmps.ps1
SET PS_EXE=C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe
%PS_EXE% -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process %PS_EXE% -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """%SCRIPT_NAME%""' -Verb RunAs}"