0
votes

All,

I have a working Powershell script that will run a VBScript from within it just fine if I run it from Powershell however, if I create a schedule task in windows 2016 to run the powershell script it never runs the VBScript within it.

Does anyone know how to get the schedule task to run correctly?

Powershell code calling the vbscript:

& cscript /nologo "$env:userprofile\wuscripts\nwuemail.vbs";

Do I have to do something special to get Task Scheduler to run that line in the Powershell script? Again the powershell script runs just fine with the vbscript line by calling it from Powershell cmd.

EDIT: The Schedule task is running under the System user. This script will run on many servers and setting the administrator password for all of them would be a PITA, so would like to avoid that if possible.

Thanks for the read.

2
Have you considered running the vbscript directly from the scheduler? I would also consider what value is going into $env:userprofile as it's no longer you running it, it's the task scheduler. - BishNaboB
Why are you using VBScript at all, if you are just using PowerShell to call it? Just convert the .vbs to a .ps1 and call the .ps1 or as the other commenter stated,m just run the .vbs. What is the .ps1 doing and what is the .vbs doing? - postanote

2 Answers

0
votes

Try this instead

Start-process "wscript.exe" -ArgumentList "$env:userprofile\wuscripts\nwuemail.vbs"
0
votes

Thank you BishNaboB, your comment got me thinking about the task user. It was using System user and not Administrator which the scripts reside. Changing the $env:userprofile to the direct path fixed it. But now I have to rewrite the rest of the script.

Mohammed Bizri, I did not try your code as the first comment helped me get to the resolve, but thanks for the help.

Thanks to all who commented. Great community!