Powershell Service not running while / do in script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
danarstevenson
Posts: 8
Last visit: Wed Mar 20, 2024 5:41 am

Powershell Service not running while / do in script

Post 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.  
Last edited by danarstevenson on Wed Jun 13, 2018 5:45 am, edited 1 time in total.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Service not running while / do in script

Post by jvierra »

To log to the event log use "Write-Host" and not "Write-Output" (echo).
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Powershell Service not running while / do in script

Post 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
:
User avatar
danarstevenson
Posts: 8
Last visit: Wed Mar 20, 2024 5:41 am

Re: Powershell Service not running while / do in script

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell Service not running while / do in script

Post by jvierra »

As I noted above you must use "Write-Host" and NOT "Write-Output". Only "Write-Host" is sent to the registry.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked