Powershell Service not running while / do in script
Posted: Tue Jun 12, 2018 9:28 am
Greetings,
Thank you in advance, this forum has been awesome for support questions.
Scenario: created a script and want it to run as a service
Issue: The service starts fine, while its running it does not seem to be doing the while / do code, i would expect entries in the log file. I do not know why the while /do loop well appears not to be running. Any help would be great, Thanks Dana
Here is my short script .
Thank you in advance, this forum has been awesome for support questions.
Scenario: created a script and want it to run as a service
Issue: The service starts fine, while its running it does not seem to be doing the while / do code, i would expect entries in the log file. I do not know why the while /do loop well appears not to be running. Any help would be great, Thanks Dana
Here is my short script .
- cls
- $vrmLog = "D:\PS_Scripts\vrmlog\fithbrsrvlog.log"
- $startdate = get-Date -Format g
- $Credential = Import-CliXml -Path "D:\PS_Scripts\Creds\vmruser.cred"
- echo "Starting watcher service $startdate" >> $vrmLog
- while (1)
- {
- New-SSHSession -ComputerName "fit-vsphererep" -Credential $Credential
- $vrmdate = get-Date
- Invoke-SSHCommand -Command "service hbrsrv status" -SessionId 0 | Out-file D:\PS_Scripts\vrmlog\fithbrsrvout.txt
- $SEL = Select-String -Path D:\PS_Scripts\vrmlog\fithbrsrvout.txt -Pattern "Failed"
- if ($SEL -ne $null)
- {
- echo "fit hbrsrv Service has failed $vrmdate" >> $vrmLog
- Send-MailMessage -From "HBRSrv <vcenter@contoso.com>" -To "Dana <someone@contoso.com>", "text< xxxxxxxxxxx@vtext.com>" -Subject "fit-vcenter hbrsrvresult" -Body "fit-vcenter hbrsrv service has failed" -dno onSuccess, onFailure -SmtpServer xx.xx.xx.xx
- New-SSHSession -ComputerName "fit-vsphererep" -Credential $Credential
- Invoke-SSHCommand -Command "service hbrsrv start" -SessionId 0
- echo "attempting to start fit hbrsrv Service $vrmdate" >> $vrmLog
- }
- else
- {
- echo "fit-vcenter hbrsrv Service is running $vrmdate" >> $vrmLog
- }
- del D:\PS_Scripts\vrmlog\fithbrsrvout.txt
- start-sleep -seconds 600
- }