More fun with scheduled PowerShell
Earlier, I gave you a VBScript wrapper that you could use to run PowerShell scripts as scheduled tasks and be completely hidden from the user. If you want to run a PowerShell cmdlet expression instead of a .ps1 script, you need to take a slightly different approach.
You can use this VBScript as a wrapper:
Dim objShell
Set objShell=CreateObject(“WScript.Shell”)
‘enter the PowerShell expression
‘you need to use short filenames and paths
strExpression=”get-process | where {$_.workingset -ge 50000000} | out-file e:\docume~1\jhicks\results.txt”
strCMD=”powershell -nologo -command ” & Chr(34) &_
“&{” & strExpression &”}” & Chr(34)
‘Uncomment next line for debugging
‘WScript.Echo strCMD
‘use 0 to hide window
objShell.Run strCMD,0
The strExpression is one long line. The important thing to remember is that if you are referencing a file, as I am here, you must use a short file name. Especially when it contains spaces. The better approach for my example would have been to pipe output-file to something like c:\logs\results.txt. But I wanted to make a point about long file names. As with the other VBScript wrapper, when you create your scheduled task use WSCRIPT.
October 9th, 2008 at 6:21 am
Hi,
You say when you create your scheduled task use WSCRIPT.
Why is that? I would have thought cscript would work better.
Thanks for your time,
OldDog
October 9th, 2008 at 6:37 am
Cscript will launch a CMD window which I don’t want to see. Using WScript makes the script run more or less as a GUI.
October 15th, 2008 at 12:51 pm
OK, Thanks,
Next question, how do I schedule a PS1 script?
should this work?
Dim objShell
Set objShell=CreateObject(“WScript.Shell”)
‘ you need To use short filenames And paths
objShell.Run(“cmd /c powershell -command &’\\gcc.int\mp_test\Aspen_TEST\Code\get_applog_pass.ps1′”),0,True
Set objShell= Nothing
October 15th, 2008 at 4:51 pm
Looks like it should work if you schedule the vbscript. Does it?
January 18th, 2009 at 8:59 am
[...] You’re not calling powershell.exe properly for starters. This should help somewhat: http://blog.sapien.com/index.php/200…ed-powershell/ Marco — Microsoft MVP – Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP [...]
January 18th, 2009 at 9:00 am
[...] work this way. If VBScript does work, maybe you need a little trick like this to make it work: http://blog.sapien.com/index.php/200…led-powershell [...]
April 22nd, 2009 at 10:08 am
[...] are a lot of suggestions out there on how to use Scheduled Tasks to launch a .ps1 file; basic, wrapped in a .vbs, or even a self-scheduling script. That’s helpful, as long as you want to run it in a [...]