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 .
  1. cls
  2.  
  3. $vrmLog = "D:\PS_Scripts\vrmlog\fithbrsrvlog.log"
  4.  
  5. $startdate = get-Date -Format g
  6.  
  7. $Credential = Import-CliXml -Path "D:\PS_Scripts\Creds\vmruser.cred"
  8.  
  9. echo "Starting watcher service $startdate" >> $vrmLog
  10.  
  11. while (1)
  12. {
  13.    
  14.     New-SSHSession -ComputerName "fit-vsphererep" -Credential $Credential
  15.    
  16.     $vrmdate = get-Date
  17.    
  18.     Invoke-SSHCommand -Command "service hbrsrv status" -SessionId 0 | Out-file D:\PS_Scripts\vrmlog\fithbrsrvout.txt
  19.    
  20.     $SEL = Select-String -Path D:\PS_Scripts\vrmlog\fithbrsrvout.txt -Pattern "Failed"
  21.    
  22.     if ($SEL -ne $null)
  23.     {
  24.         echo "fit hbrsrv Service has failed $vrmdate" >> $vrmLog
  25.         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
  26.         New-SSHSession -ComputerName "fit-vsphererep" -Credential $Credential
  27.         Invoke-SSHCommand -Command "service hbrsrv start" -SessionId 0
  28.         echo "attempting to start fit hbrsrv Service $vrmdate" >> $vrmLog
  29.     }
  30.     else
  31.     {
  32.         echo "fit-vcenter hbrsrv Service is running $vrmdate" >> $vrmLog
  33.  
  34.     }
  35.    
  36.     del D:\PS_Scripts\vrmlog\fithbrsrvout.txt
  37.    
  38.     start-sleep -seconds 600
  39.    
  40. }
  41.  

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.