Difference between revisions of "Windows Service From Powershell Script"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 1: Line 1:
 +
# Event Log
 +
```
 +
New-EventLog –LogName Application –Source "watchAuth0LDAPConn"
 +
Write-EventLog –LogName Application –Source "watchAuth0LDAPConn" –EntryType Information –EventID 1 –Message "This is a test message."
 +
```
 +
 +
# Script
 
C:\app\watchAuth0LDAPConn.ps1
 
C:\app\watchAuth0LDAPConn.ps1
 
```
 
```
 
$url = "127.0.0.1:49948"
 
$url = "127.0.0.1:49948"
 
$sleep = 3
 
$sleep = 3
Write-Output "Starting $url connection watcher."
+
$msg = "Starting $url connection watcher."
 +
Write-Output $msg
 +
Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 1 -EntryType Information -Message $msg
 
while($true){
 
while($true){
 
   try {
 
   try {
          $R = Invoke-WebRequest $url
+
    $r = Invoke-WebRequest $url
 
   }
 
   }
 
   catch {
 
   catch {
          $_.Exception.Message
+
    $_.Exception.Message
          Write-Output "URL $url is unresponsive. Restarting Auth0LDAP service."
+
    $msg = "URL $url is unresponsive. Restarting Auth0LDAP service."
 +
    Write-Output $msg
 +
    Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 2 -EntryType Warning -Message $msg
 
   }
 
   }
 
   Start-Sleep -s $sleep
 
   Start-Sleep -s $sleep
 
}
 
}
 +
```
  
# New-Service -Name "Auth0LDAPAppHealthWatcher" -BinaryPathName C:\app\checkTcpPort.ps1
+
# Create service
# start-service Auth0LDAPAppHealthWatcher
 
```
 
  
Use nssm.exe to create service
+
Download and use nssm.exe to create service - https://nssm.cc/download
- https://nssm.cc/download
 
 
```
 
```
 
$Binary = (Get-Command Powershell).Source
 
$Binary = (Get-Command Powershell).Source
Line 37: Line 46:
 
```
 
```
  
you may be able to use sc.exe but I had issues with New-Service command
+
 
 +
 
 +
 
 +
you may be able to use sc.exe but I had issues with New-Service command limits because it was ps1 and not exe

Revision as of 19:45, 22 March 2022

Event Log

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

Script

C:\app\watchAuth0LDAPConn.ps1

$url = "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 {
    $r = Invoke-WebRequest $url
  }
  catch {
    $_.Exception.Message
    $msg = "URL $url is unresponsive. Restarting Auth0LDAP service."
    Write-Output $msg
    Write-EventLog -LogName "Application" -Source "watchAuth0LDAPConn" -EventID 2 -EntryType Warning -Message $msg
  }
  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 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 limits because it was ps1 and not exe