LEARN with scripting TRAINING CREATE with SCRIPTING TOOLS SHARE in a COMMUNITY BUY Scripting Products

SAPIEN Solutions

SAPIEN homepage
SAPIEN Software
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

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]

4 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?

Leave a Reply


Entries (RSS) and Comments (RSS).