I have a functioning PowerShell script which registers a timer object and sends a key every couple of minutes.
$wshell = New-Object -ComObject Wscript.Shell
...
function fctName {
param([ScriptBlock] $action)
...
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed -Action $action
}
fctName {
...
$wshell.sendkeys("{ESC}")
}
Within the Windows PowerShell ISE the script works just fine, without any errors. But, I would love to run it from a batch file, in which I wrote only this line:
Powershell.exe -noexit C:\...\ScriptName.ps1
No errors are thrown but it does not do what I want, which is to prevent Windows going idle.
Because I '-noexit' the cmd window, I can easily stop the event with
Unregister-Event -SourceIdentifier Timer.Elapsed
It seems to be working because only typing it twice throws an error.
- Could it be that my batch file needs different code? Maybe "echo" etc. I'm not very familiar with it
- Could it be that I have to import a module in Powershell?