{"id":27,"date":"2006-12-26T04:00:00","date_gmt":"2006-12-26T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/26\/more-fun-with-scheduled-powershell\/"},"modified":"2006-12-26T04:00:00","modified_gmt":"2006-12-26T12:00:00","slug":"more-fun-with-scheduled-powershell","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/26\/more-fun-with-scheduled-powershell\/","title":{"rendered":"More fun with scheduled PowerShell"},"content":{"rendered":"<p><a href=\"\/current\/2006\/12\/20\/schedule-hidden-powershell-tasks.html\">Earlier<\/a>, I gave you a VBScript wrapper that you could use to run PowerShell scripts as scheduled tasks and be completely hidden from the user.&nbsp; If you want to run a PowerShell cmdlet expression instead of a .ps1 script, you need to take a slightly different approach.<\/p>\n<p>You can use this VBScript as a wrapper:<\/p>\n<p><span style=\"font-family: courier new,courier,mono;\">Dim objShell<\/span><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">Set objShell=CreateObject(&#8220;WScript.Shell&#8221;)<\/span><br style=\"font-family: courier new,courier,mono;\" \/><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">&#8216;enter the PowerShell expression<\/span><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">&#8216;you need to use short filenames and paths <\/span><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">strExpression=&#8221;get-process | where {$_.workingset -ge 50000000} | out-file e:\\docume~1\\jhicks\\results.txt&#8221;<\/span><br style=\"font-family: courier new,courier,mono;\" \/><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">strCMD=&#8221;powershell -nologo&nbsp; -command &#8221; &amp; Chr(34) &amp;_<\/span><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">&#8220;&amp;{&#8221; &amp; strExpression &amp;&#8221;}&#8221; &amp; Chr(34) <\/span><br style=\"font-family: courier new,courier,mono;\" \/><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">&#8216;Uncomment next line for debugging<\/span><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">&#8216;WScript.Echo strCMD<\/span><br style=\"font-family: courier new,courier,mono;\" \/><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">&#8216;use 0 to hide window<\/span><br style=\"font-family: courier new,courier,mono;\" \/><span style=\"font-family: courier new,courier,mono;\">objShell.Run strCMD,0<\/span><br style=\"font-family: courier new,courier,mono;\" \/><br \/>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.&nbsp; 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.&nbsp; 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,24,25],"tags":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-general","category-vbscript","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}