Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Schedule PS1 does not work 1

Status
Not open for further replies.

spazman

Programmer
May 29, 2001
500
CA
Okay I have a powershell script, it backs up an SQL DB to two locations. It has one command line parameter. If its set to "Midday" it only copies the backup to one location.

The script without parameter runs as a scheduled task. It I added the parameter it doesn't run.

If I run the same command powershell c:\PS_Scripts\backup-db.ps1 "Midday" from Start |Run or a batch file it works. If I schedule the batch file, or the powershell line it doesn't work.

I have tried

powershell C:\PS_Scripts\backup-db.ps1 "Midday"
powershell "C:\PS_Scripts\backup-db.ps1 "Midday""
powershell C:\PS_Scripts\backup-db.ps1 Midday

same line in the batch file, they all run manually but not in a sceduled task.
 
I can't remember why exactly but I read that powershell scripts may need to be run from a .vbs file. The Run dialog is more of a command interpreter. Perhaps, what's why it work there and not as a scheduled task.

scheduled task: 'backup.vbs'
Code:
set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit c:\PS_Scripts\backup-db.ps1 Midday")

-Geates
 
Thanks for the posts. I will check them out and keep you in the loop, plus there may be stars to hand out!
 
Nothing will work.

I wrote a batch file that has Powershell <ScriptName>.ps1 if I double click the batch file, it runs, if I schedule it it runs, doesn;t really run and never terminates.
 
OK, I've done a couple of tests on a system here. I wrote a .BAT file with the following command:
Code:
powershell -command "& 'd:\is\powershell\export-ip.ps1'"

The PS1 script uses the Quest AD cmdlets to export some information from Active Directory to a CSV file.

I verified that PowerShell was in the PATH environment variable. I then scheduled a job using the credentials of an account that had access to AD, and had the Quest snap-in loaded in its PowerShell profile. The job ran successfully and produced the expected CSV file. I then changed the job to use the credentials of another account. This second account has access to AD, but does not have the Quest snap-in loaded in its PowerShell profile. The job ran and completed, but no CSV file was produced. I expected this since PowerShell wouldn't know the Quest command.

So, some questions for you:
1. In the batch file did you follow the syntax given above, using the -command switch?
2. Is PowerShell in the system-wide PATH environment variable? If not you will need to specify the full path to the PowerShell executable
3. Does the account used to run the scheduled task have the necessary permissions to SQL/file shares/backup devices/etc?
4. Does the PS1 script use only native PowerShell (or built-in Windows) commands/cmdlets, or does it rely on 3rd-party cmdlets? If it relies on 3rd-party commands, does the account used to run the job have that snap-in loaded in its PowerShell profile?

Scheduling PowerShell does work, we just need to figure out what's different/missing in your environment.

 
Sorry, it would also help if I read your original post a little more closely. I missed that it only failed with the parameter. Try the following in your batch file:
Code:
powershell -command "& 'C:\PS_Scripts\backup-db.ps1' Midday"
or
Code:
powershell -command "& {C:\PS_Scripts\backup-db.ps1 Midday}"
 
I ended up using

powershell -command "& {C:\PS_Scripts\backup-db.ps1 Midday}" and it worked.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top