Advertisement
Guest User

UseRegKeysForServiceSettings.ps1

a guest
Oct 26th, 2016
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.         .SYNOPSIS
  3.         Test .ps1 script to be used as a Windows Service.
  4.  
  5.         .DESCRIPTION
  6.         Save as .ps1 and run.
  7.         Script creates a file "%USERPROFILE%\Downloads\_TestService.txt" in the current users Downloads Folder
  8.         By default this script will write a new line every 10 seconds
  9.  
  10.         .DESCRIPTION
  11.         YOU MAY MODIFY THE FREQUENCY THIS SCRIPT WRITES A NEW LINE TO THE "_TestService.txt" FILE VIA:
  12.         "HKLM:\SYSTEM\CurrentControlSet\Services\_TestService" AND "REGSZ with the name of FREQUENCY"
  13.  
  14. #>
  15. $registryPath = "HKLM:\SYSTEM\CurrentControlSet\Services\_TestService"
  16. $regName = 'Frequency'
  17. $regValue = 10
  18. if(Test-Path $registryPath)
  19. {
  20.     [int]$frequency = Get-ItemProperty -Path $registryPath -Name $regName | select -ExpandProperty Frequency
  21. }
  22. if(!(Test-Path $registryPath))
  23. {
  24.     New-Item $registryPath -Force | Out-Null
  25.     Start-Sleep -Milliseconds 200        
  26.     New-ItemProperty -Path $registryPath -Name $regName -Value $regValue -PropertyType String -Force | Out-Null
  27.     Start-Sleep -Milliseconds 200
  28.     [int]$frequency = Get-ItemProperty -Path $registryPath -Name $regName | select -ExpandProperty Frequency
  29. }
  30. Remove-Variable -Name firstLoop -Force -EA SilentlyContinue -WA SilentlyContinue | Out-Null
  31. for($go = 1; $go -lt 2) # $go will always be less than 2, so this script will run until user intervention
  32. {
  33.     $d = Get-Date
  34.     $p = "$($env:USERPROFILE)\Downloads\_TestService.txt"
  35.     [int]$frequency = Get-ItemProperty -Path $registryPath -Name $regName | select -ExpandProperty Frequency
  36.     if(Test-Path $p)
  37.     {
  38.         if($firstLoop -ne $false)
  39.         {              
  40.             Add-Content -Value "$($d) - Script Started" -Path $p -EA SilentlyContinue -WA SilentlyContinue
  41.             $firstLoop = $false          
  42.         }
  43.         else
  44.         {  
  45.             Add-Content -Value $d -Path $p -EA SilentlyContinue -WA SilentlyContinue
  46.         }
  47.     }
  48.  
  49.     if(!(Test-Path $p))
  50.     {
  51.         New-Item -Path $p -ItemType File -EA SilentlyContinue -WA SilentlyContinue | Out-Null
  52.         if($firstLoop -ne $false)
  53.         {              
  54.             Add-Content -Value "$($d) - Script Started" -Path $p -EA SilentlyContinue -WA SilentlyContinue
  55.             $firstLoop = $false          
  56.         }        
  57.         else
  58.         {          
  59.             Add-Content -Value $d -Path $p -EA SilentlyContinue -WA SilentlyContinue
  60.         }
  61.     }    
  62.     Start-Sleep -Seconds $frequency
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement