I want to automate a script to run every 10 minutes but I’m stuck.
I put the required credentials on the first time I run the script but then it doesn’t seem to run (never runs actually).
Furthermore there are logon failure (batch events) in eventlog and this means that every 10 minutes it goes to run the script but something wrong with the credentials happen.
I use the following code:
# Change these three variables to whatever you want $jobname = "Automate Sth" $script = "C:\myScript.ps1" $repeat = (New-TimeSpan -Minutes 10) $scriptblock = [scriptblock]::Create($script) $trigger = New-JobTrigger -Once -At (Get-Date).Date -RepeatIndefinitely -RepetitionInterval $repeat $msg = "Enter the username and password that will run the task" $credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain) $options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery Register-ScheduledJob -Name $jobname -ScriptBlock $scriptblock -Trigger $trigger -ScheduledJobOption $options -Credential $credential
What I’m doing wrong?
Is there any way to put the credentials only at the beginning and then to run the script automatically with no logon failures?
What I have to change in my above “job scheduler” to work fine?
Thank you in advance!!!