- This topic has 3 replies, 4 voices, and was last updated 3 years, 2 months ago by
March 6, 2017 at 1:53 pm #65856ParticipantTopics: 23Replies: 15Points: 28Rank: Member
I created a PowerShell script on a Windows Server 2008 R2 server and it works fine when launched interactively.
I created a scheduled task to launch the same script every hour and it looks it is completed, but I don’t see the expected results.
So, I presume that something goes wrong during the scheduled execution or that I made some mistake while creating the scheduled task.
How can I troubleshoot the scheduled execution to detect the problem?
If I insert some write-host statements to debug the script where are they written?
March 6, 2017 at 2:10 pm #65859ParticipantTopics: 12Replies: 1623Points: 2,565Rank: Community Hero
The common issues are credentials are different than what was used to test the task. To keep things simple to troubleshoot, you can leverage:
Add-Content -Path "C:\troubleshoot.log" -Value "Step 1"
Make sure the creds have permissions to the log.
March 6, 2017 at 2:13 pm #65860ParticipantTopics: 0Replies: 199Points: 2Rank: Member
I had a similar problem, also on 2008 R2. I could not get the working directory to take, I had to use full path names for everything, which I didn’t want to do. I finally gave up, wrote a .bat file to call the PS script and scheduled the .bat file instead. Then everything worked as it should. As a bonus, I could make changes to the .bat file without having to changed the scheduled task, enter the service account password again, etc.
March 6, 2017 at 4:06 pm #65871ParticipantTopics: 12Replies: 524Points: 1,219Rank: Community Hero
To schedule a PS script I recommend using the SCHTASKS.EXE tool as in:
# To schedule a PowerShell script: $ScriptPath = 'C:\Sandbox\Monitor-Backup3.ps1' $TaskName = 'BackupMonitor' $TaskRun = "powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -Command ""$ScriptPath""" # Example: Weekly on Sundays at 2 AM SCHTASKS.EXE /Create /S $Env:COMPUTERNAME /RU SYSTEM /SC WEEKLY /D SUN /TN $TaskName /TR $TaskRun /ST 02:00 /RL HIGHEST /F break # Example: Daily at 7 AM SCHTASKS.EXE /Create /S $Env:COMPUTERNAME /RU SYSTEM /SC DAILY /TN $TaskName /TR $TaskRun /ST 07:00 /RL HIGHEST /F break # Run now: SCHTASKS /Run /TN "\$TaskName" break
To check whether the script worked or not, or whether it did what it’s supposed to do, I recommend re-writing the script to log its actions to text file. I use this write-log function.
- The topic ‘Troubleshooting a scheduled script’ is closed to new replies.