Windows Service From Powershell Script

From UVOO Tech Wiki
Revision as of 18:55, 22 March 2022 by Busk (talk | contribs) (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 $...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

$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