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.  
  4.  
  5. $vrmLog = "D:\PS_Scripts\vrmlog\fithbrsrvlog.log"
  6.  
  7.  
  8.  
  9. $startdate = get-Date -Format g
  10.  
  11.  
  12.  
  13. $Credential = Import-CliXml -Path "D:\PS_Scripts\Creds\vmruser.cred"
  14.  
  15.  
  16.  
  17. echo "Starting watcher service $startdate" >> $vrmLog
  18.  
  19.  
  20.  
  21. while (1)
  22.  
  23. {
  24.  
  25.    
  26.  
  27.     New-SSHSession -ComputerName "fit-vsphererep" -Credential $Credential
  28.  
  29.    
  30.  
  31.     $vrmdate = get-Date
  32.  
  33.    
  34.  
  35.     Invoke-SSHCommand -Command "service hbrsrv status" -SessionId 0 | Out-file D:\PS_Scripts\vrmlog\fithbrsrvout.txt
  36.  
  37.    
  38.  
  39.     $SEL = Select-String -Path D:\PS_Scripts\vrmlog\fithbrsrvout.txt -Pattern "Failed"
  40.  
  41.    
  42.  
  43.     if ($SEL -ne $null)
  44.  
  45.     {
  46.  
  47.         echo "fit hbrsrv Service has failed $vrmdate" >> $vrmLog
  48.  
  49.         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
  50.  
  51.         New-SSHSession -ComputerName "fit-vsphererep" -Credential $Credential
  52.  
  53.         Invoke-SSHCommand -Command "service hbrsrv start" -SessionId 0
  54.  
  55.         echo "attempting to start fit hbrsrv Service $vrmdate" >> $vrmLog
  56.  
  57.     }
  58.  
  59.     else
  60.  
  61.     {
  62.  
  63.         echo "fit-vcenter hbrsrv Service is running $vrmdate" >> $vrmLog
  64.  
  65.  
  66.  
  67.     }
  68.  
  69.    
  70.  
  71.     del D:\PS_Scripts\vrmlog\fithbrsrvout.txt
  72.  
  73.    
  74.  
  75.     start-sleep -seconds 600
  76.  
  77.    
  78.  
  79. }
  80.  
  81.  

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.