Difference between revisions of "Windows Service From Powershell Script"

From UVOO Tech Wiki
Jump to navigation Jump to search
(Created page with "C:\app\watchAuth0LDAPConn.ps1 ``` $url = "127.0.0.1:49948" $sleep = 3 Write-Output "Starting $url connection watcher." while($true){ try { $R = Invoke-WebRequest $...")
 
Line 20: Line 20:
  
 
Use nssm.exe to create service
 
Use nssm.exe to create service
 +
- https://nssm.cc/download
 
```
 
```
 
$Binary = (Get-Command Powershell).Source
 
$Binary = (Get-Command Powershell).Source

Revision as of 18:56, 22 March 2022

C:\app\watchAuth0LDAPConn.ps1

$url = "127.0.0.1:49948"
$sleep = 3
Write-Output "Starting $url connection watcher."
while($true){
  try {
          $R = Invoke-WebRequest $url
  }
  catch {
          $_.Exception.Message
          Write-Output "URL $url is unresponsive. Restarting Auth0LDAP service."
  }
  Start-Sleep -s $sleep
}

# New-Service -Name "Auth0LDAPAppHealthWatcher" -BinaryPathName C:\app\checkTcpPort.ps1
# start-service Auth0LDAPAppHealthWatcher

Use nssm.exe to create service - https://nssm.cc/download

$Binary = (Get-Command Powershell).Source
$Arguments = '-ExecutionPolicy Bypass -NoProfile -File "C:\app\watchAuth0LDAPConn.ps1"'
.\nssm.exe install watchAuth0LDAPConn $Binary $Arguments
start-service watchAuth0LDAPConn
get-service watchAuth0LDAPConn

you may be able to use sc.exe but I had issues with New-Service command