Difference between revisions of "Windows Service From Powershell Script"

From UVOO Tech Wiki
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
# Event Log
+
https://github.com/jeremybusk/windowsWatcherServiceViaPowershell/blob/main/README.md
 +
 
 +
# Create Event Log Application and test event
 
```
 
```
 
New-EventLog –LogName Application –Source "watchAuth0LDAPConn"
 
New-EventLog –LogName Application –Source "watchAuth0LDAPConn"
Line 5: Line 7:
 
```
 
```
  
# Script
+
# Create Script
 
C:\app\watchAuth0LDAPConn.ps1
 
C:\app\watchAuth0LDAPConn.ps1
 
```
 
```
$url = "127.0.0.1:49948"
+
$url = "http://127.0.0.1:49948"
 
$sleep = 3
 
$sleep = 3
 
$msg = "Starting $url connection watcher."
 
$msg = "Starting $url connection watcher."
Line 14: Line 16:
 
Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 1 -EntryType Information -Message $msg
 
Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 1 -EntryType Information -Message $msg
 
while($true){
 
while($true){
   try {
+
   try { ($rspStatusCode = Invoke-WebRequest http://localhost:49948).StatusCode
    $r = Invoke-WebRequest $url
+
  } catch {
 +
    $rspStatusCode = $_.Exception.Response.StatusCode.Value__
 +
    # $_.Exception.Message
 
   }
 
   }
   catch {
+
   if (!(200, 401 -eq $rspStatusCode)){
     $_.Exception.Message
+
     write-output "True"
     $msg = "URL $url is unresponsive. Restarting Auth0LDAP service."
+
     $msg = "URL $url is unresponsive or not resonding with valid rsp code. Restarting Auth0LDAP service."
 
     Write-Output $msg
 
     Write-Output $msg
 
     Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 2 -EntryType Warning -Message $msg
 
     Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 2 -EntryType Warning -Message $msg
 +
    restart-service "Auth0 ADLDAP"
 
   }
 
   }
 
   Start-Sleep -s $sleep
 
   Start-Sleep -s $sleep
Line 27: Line 32:
 
```
 
```
  
# Create service
+
# Create Service
  
 
Download and use nssm.exe to create service - https://nssm.cc/download
 
Download and use nssm.exe to create service - https://nssm.cc/download
Line 36: Line 41:
 
```
 
```
  
 +
# Start Service
 
```
 
```
 
start-service watchAuth0LDAPConn
 
start-service watchAuth0LDAPConn
Line 41: Line 47:
 
```
 
```
  
remove
+
Remove Service
 
```
 
```
 
nssm.exe remove watchAuth0LDAPConn confirm
 
nssm.exe remove watchAuth0LDAPConn confirm

Latest revision as of 20:52, 22 March 2022

https://github.com/jeremybusk/windowsWatcherServiceViaPowershell/blob/main/README.md

Create Event Log Application and test event

New-EventLog –LogName Application –Source "watchAuth0LDAPConn"
Write-EventLog –LogName Application –Source "watchAuth0LDAPConn" –EntryType Information –EventID 1 –Message "This is a test message."

Create Script

C:\app\watchAuth0LDAPConn.ps1

$url = "http://127.0.0.1:49948"
$sleep = 3
$msg = "Starting $url connection watcher."
Write-Output $msg
Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 1 -EntryType Information -Message $msg
while($true){
  try { ($rspStatusCode = Invoke-WebRequest http://localhost:49948).StatusCode
  } catch {
    $rspStatusCode = $_.Exception.Response.StatusCode.Value__
    # $_.Exception.Message
  }
  if (!(200, 401 -eq $rspStatusCode)){
    write-output "True"
    $msg = "URL $url is unresponsive or not resonding with valid rsp code. Restarting Auth0LDAP service."
    Write-Output $msg
    Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 2 -EntryType Warning -Message $msg
    restart-service "Auth0 ADLDAP"
  }
  Start-Sleep -s $sleep
}

Create Service

Download and 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

start-service watchAuth0LDAPConn
get-service watchAuth0LDAPConn

Remove Service

nssm.exe remove watchAuth0LDAPConn confirm

you may be able to use sc.exe but I had issues with New-Service command limits because it was ps1 and not exe