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?