(imported comment written by dmoore21)
Just to give you an idea of what I meant above, here is how I deploy and execute Powershell scripts from the console…
//
//PowerShell Script
//
//1. Save old ExecutionPolicy value
//
parameter “PolicyExisted”="{exists value “ExecutionPolicy” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” of (if exists x64 registry then x64 registry else registry)}"
parameter “oldExecutionPolicy”="{if (parameter “PolicyExisted” as boolean) then (value “ExecutionPolicy” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” of (if exists x64 registry then x64 registry else registry) as string) else “”}"
//
//2. set to ExecutionPolicy=Unrestricted and Pull PowerShell exe from registry… if 64bit then pull PowerShell x64
//
if {x64 of operating system}
parameter “PowerShellexe”="{value “Path” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” of x64 registry}"
regset64 “http://HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”=“Unrestricted”
else
parameter “PowerShellexe”="{value “Path” of key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” of registry}"
regset “http://HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”=“Unrestricted”
endif
//
//3. Download PowerShell script and check size/hash
//
download http://temserver:52311/Uploads/script.ps1
continue if {(size of it = 5955 AND sha1 of it = “4f9d85970da5511498bc0a5db5e9f05884e897e5”) of file “script.ps1” of folder “__Download”}
//
//4. Execute PowerShell with ps1 script file
//
action uses wow64 redirection false
waithidden “{parameter “PowerShellexe”}” -file “__Download\script.ps1 | Format-Table -AutoSize”
action uses wow64 redirection {x64 of operating system}
//
//5. Restore ExecutionPolicy back
//
if {x64 of operating system}
if {parameter “PolicyExisted” as boolean}
regset64 “http://HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”="{parameter “oldExecutionPolicy”}"
else
regdelete64 “http://HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”
endif
else
if {parameter “PolicyExisted” as boolean}
regset “http://HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”="{parameter “oldExecutionPolicy”}"
else
regdelete “http://HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell” “ExecutionPolicy”
endif
endif
//
//END OF SCRIPT
//
You’ll want to pay close attention to section 2…