Page 1 of 1

Powershell Service not running while / do in script

Posted: Tue Jun 12, 2018 9:28 am
by danarstevenson
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 .

Code: Select all

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

}

Re: Powershell Service not running while / do in script

Posted: Tue Jun 12, 2018 9:54 am
by jvierra
To log to the event log use "Write-Host" and not "Write-Output" (echo).

Re: Powershell Service not running while / do in script

Posted: Tue Jun 12, 2018 11:32 am
by mxtrinidad
Just an observation!

You're using both the New-SSHSession and the Invoke-SSHCommand. But, I don't see using the -sshsession parameter in the Invoke-SSHCommand to connect to the ssh session created in with the "New-SSHSession".

Sorry! I realized you're using the -SessionId 0 to connect.
:)

Check out the following link which could help fine tuned the script: https://stackoverflow.com/questions/417 ... le_rich_qa

You could use the -OutVariable 'variable-name' to be output to a file.

Code: Select all

:
Invoke-SSHCommand -SSHSession $CurrentSession -Command "ls -l /" -OutVariable results
$results.Output | Out-File c:\MyLogfiles\SessionLog.txt -append
:

Re: Powershell Service not running while / do in script

Posted: Wed Jun 13, 2018 7:53 am
by danarstevenson
Greetings,

I changed the echo to write output and now it shows me the information when I run this deployed as an exe, I get log entries while in the do / while block all good. Thank you

I then deploy as a service as the would be final step for testing, and nothing happens.

I was wondering if someone had a very simple example of a working script that is deployed as a service that is working with a do / while or something like that in it that i could view.

Please and thank you
Dana

Re: Powershell Service not running while / do in script

Posted: Wed Jun 13, 2018 10:04 am
by jvierra
As I noted above you must use "Write-Host" and NOT "Write-Output". Only "Write-Host" is sent to the registry.