Use this function instead of PsCmd.
Function PsCmd3
Function PsCmd3
;/
function# $commands [$cl] [str&output] [flags] ;;flags: 1 UTF-8 output, 2 ANSI output
;Executes PowerShell script stored in a variable or macro.
;Waits and returns powershell.exe's return value.
;commands - one or more PowerShell commands.
;;;QM 2.3.5. Can be macro, like "macro:MacroName". Gets whole text, or text that follows #ret line. Ignores first line if it looks like macro options ( /...).
;;;QM 2.3.5. If "", gets caller's text that follows #ret line.
;cl - additional powershell.exe command line arguments.
;output - variable that receives output text. If omitted or 0, displays the text in QM output.
;flags - output text encoding. See example.
;REMARKS
;Uses a temporary file and command line -File to pass it to powershell.exe. Also adds command line -ExecutionPolicy Bypass.
;Error if powershell.exe does not exist. PowerShell is available on Windows 7/2008/+, and can be downloaded for some older Windows versions.
;See also: <PsFile>
;EXAMPLES
;PsCmd3 "get-date"
;str code=
;;get-process
;PsCmd3 code "" _s
;out _s
;PsCmd3 "" "" 0 1
;#ret
;[Console]::OutputEncoding=[System.Text.Encoding]::UTF8
;Write-Host "test ąčę αβγ"
#exe addtextof "<script>"
#opt nowarnings 1
opt noerrorshere 1
Scripting_GetCode commands _s
__TempFile tf.Init("ps1")
if(_unicode) str s.from("[0xEF][0xBB][0xBF]" commands); s.setfile(tf)
else _s=commands; _s.setfile(tf)
s.format("%s -ExecutionPolicy Bypass -File ''%s''" cl tf)
ret sub.PsRunCL(s output flags)
#sub PsRunCL
function# $cl [str&output] [flags]
opt noerrorshere 1
str ps.searchpath("powershell.exe"); if(!ps.len) end "PowerShell not installed"
str s.format("%s %s" ps cl)
if(!&output) int print=1; str so; &output=so
int enc=0; if(flags&1) enc=0x800; else if(flags&2) enc=0x400
int r=RunConsole2(s output 0 enc)
output.trim("[]")
if(print and output.len) out output
ret r