VMware Cloud Community
cjfink
Contributor
Contributor
Jump to solution

Scheduling a *.ps1 script

I've seen a few posts on what needs to be entered in Action tab for a Windows scheduled task, but for some reason, it's not working.

I have a script that I want to run daily, it runs fine from the PowerCLI command line.  When I set up the scheduled task though it doesn't work.  The script is a basic one that takes a snapshot of some VM's and removes snapshots older than 3 days.

I have tried the following in the Action tab:

Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments:  -noprofile -ExecutionPolicy Bypass -File c:\temp\scripts\lithuania\dailysnapshots.ps1

I've also tried this in the Action tab:

Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments: -PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noprofile -ExecutionPolicy Bypass -File c:\temp\scripts\lithuania\dailysnapshots.ps1

The task kicks off, but the snapshots don't get created.

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Under which account are the scheduled tasks running?

In the Task Scheduler you will find the account General tab under the "When running the task, use the following user account"

You have to make sure that you have a profile for that account, and an eventual CredentialSToreItem file, if you don't want to hard-code the credentials in the script itself.

You also have to make sure that there are actions required in the script, no prompts.

I normally use these settings

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments: -ExecutionPolicy Bypass -NonInteractive -NoLogo -NoProfile -Command "& 'C:\Scripts\Script.ps1'"

If you need to pass parameters to the script, you can do (and Parameter1 needs to be defined in your script with param([int]$Parameter1)

Arguments: -ExecutionPolicy Bypass -NonInteractive -NoLogo -NoProfile -Command "& 'C:\Scripts\Script.ps1' -Parameter1 123"

If you are having issues with the script, it might be useful to add Start-Transcript and Stop-Transcript cmdlets at the beginning/end of the script.

That way you can have some logging of what goes on in the script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Under which account are the scheduled tasks running?

In the Task Scheduler you will find the account General tab under the "When running the task, use the following user account"

You have to make sure that you have a profile for that account, and an eventual CredentialSToreItem file, if you don't want to hard-code the credentials in the script itself.

You also have to make sure that there are actions required in the script, no prompts.

I normally use these settings

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Arguments: -ExecutionPolicy Bypass -NonInteractive -NoLogo -NoProfile -Command "& 'C:\Scripts\Script.ps1'"

If you need to pass parameters to the script, you can do (and Parameter1 needs to be defined in your script with param([int]$Parameter1)

Arguments: -ExecutionPolicy Bypass -NonInteractive -NoLogo -NoProfile -Command "& 'C:\Scripts\Script.ps1' -Parameter1 123"

If you are having issues with the script, it might be useful to add Start-Transcript and Stop-Transcript cmdlets at the beginning/end of the script.

That way you can have some logging of what goes on in the script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
cjfink
Contributor
Contributor
Jump to solution

Thank you LucD!!

The account I was using was only an AD service account and had no local profile.  The transcript option helped too, as it pointed out the path to my VMlist.txt was wrong.

Thank you very much for your help sir, and have a great holiday season!!

Reply
0 Kudos