Windows Service From Powershell Script

From UVOO Tech Wiki
Revision as of 19:14, 22 March 2022 by Busk (talk | contribs)
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 - 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

remove

nssm.exe remove watchAuth0LDAPConn confirm

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