Advertisement
Guest User

Test-Service.ps1

a guest
Oct 26th, 2016
889
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.         Every 10 seconds from the time it is started, it will add the date to the above file.
  9.  
  10. #>
  11. Remove-Variable -Name firstLoop -Force -EA SilentlyContinue -WA SilentlyContinue | Out-Null
  12. for($go = 1; $go -lt 2) # $go will always be less than 2, so this script will run until user intervention
  13. {
  14.     $d = Get-Date
  15.     $p = "$($env:USERPROFILE)\Downloads\_TestService.txt"  
  16.     if(Test-Path $p)
  17.     {
  18.         if($firstLoop -ne $false)
  19.         {              
  20.             Add-Content -Value "$($d) - Script Started" -Path $p -EA SilentlyContinue -WA SilentlyContinue
  21.             $firstLoop = $false          
  22.         }
  23.         else
  24.         {  
  25.             Add-Content -Value $d -Path $p -EA SilentlyContinue -WA SilentlyContinue
  26.         }
  27.     }
  28.  
  29.     if(!(Test-Path $p))
  30.     {
  31.         New-Item -Path $p -ItemType File -EA SilentlyContinue -WA SilentlyContinue | Out-Null
  32.         if($firstLoop -ne $false)
  33.         {              
  34.             Add-Content -Value "$($d) - Script Started" -Path $p -EA SilentlyContinue -WA SilentlyContinue
  35.             $firstLoop = $false          
  36.         }        
  37.         else
  38.         {          
  39.             Add-Content -Value $d -Path $p -EA SilentlyContinue -WA SilentlyContinue
  40.         }
  41.     }    
  42.     Start-Sleep -Seconds 10
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement