I have the following script that watches for appearance of new files in given directory, then renames the appeared file to its md5 hash
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Users\User\Desktop\downloads"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
$watcher.Filter = "*.*"
$action = {
$path = $Event.SourceEventArgs.FullPath
$md5 = (Get-FileHash $path -Algorithm MD5).Hash
$extension = [System.IO.Path]::GetExtension($path)
$newPath = [System.IO.Path]::ChangeExtension($md5, $extension)
Rename-Item $path -NewName $newPath
}
Register-ObjectEvent $watcher "Created" -Action $action
when I run it regularly as .\watchdog.ps1 command it run and work successfully until I close the terminal, it's okay. But I want the script to be running always, so I'm trying to install it as a service.
I'm doing this using nssm like this:
$Binary = (Get-Command pwsh).Source
$Arguments = '-ExecutionPolicy Bypass -NoProfile -File "C:\Users\User\Desktop\watchdog.ps1"'
nssm install MySimpleWatchDogService $Binary $Arguments
but when I'm trying to run it with:
nssm start MySimpleWatchDogService
it doesn't. I get the error instead:
MySimpleWatchDogService: Unexpected status SERVICE_PAUSED in response to START control.
Also when I trying to run it in service manager manually:
How to turn the script into the service?
