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.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

7 Responses to “More fun with scheduled PowerShell”

  1. OldDog Says:

    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

  2. Jeffery Hicks Says:

    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.

  3. OldDog Says:

    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

  4. Jeffery Hicks Says:

    Looks like it should work if you schedule the vbscript. Does it?

  5. redirect powershell stdout to objShell.Exec.Stdout.ReadAll()( | keyongtech Says:

    [...] 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 [...]

  6. PowerShell starts, but never executes nor exits? | keyongtech Says:

    [...] 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 [...]

  7. Pause until “then” « The PoSH Student Says:

    [...] 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 [...]


Entries (RSS) and Comments (RSS).