Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to capture Powershell script's return value?
Message
From
22/05/2017 09:50:22
 
 
To
21/05/2017 11:24:15
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01651230
Message ID:
01651312
Views:
78
>>How do you run a Powershell script from vfp and capture its return value?
>>
>>TIA,
>>
>>Alex
>
>I think this is the general problem of VFP getting something from a process' stdout. As far as I know, if the script/program can not be modified, then the stdout must be redirected to a file by the CMD wrapper and fetched from there.
>
>
>SET ESCAPE ON
>
>LOCAL PSCmd AS String
>LOCAL PS1Script AS String
>LOCAL PS1File AS String
>LOCAL RedirStdOut AS String
>
>TEXT TO m.PS1Script NOSHOW
>$a = "Return to Fox!"
>RETURN $a
>ENDTEXT
>
>m.PS1File = ADDBS(SYS(2023)) + SYS(2015) + ".ps1"
>m.RedirStdOut = FORCEEXT(m.PS1File, "log")
>
>STRTOFILE(m.PS1Script, m.PS1File, 0)
>
>LOCAL WShell AS Wscript.Shell
>LOCAL PS 
>
>m.WShell = CREATEOBJECT("WScript.Shell")
>
>m.PSCmd = "cmd /c powershell -ExecutionPolicy RemoteSigned " + ;
>							"-File " + IIF(" " $ m.PS1File, '"' + m.PS1File + '"', m.PS1File) + ;
>							" > " + IIF(" "$ m.RedirStdOut, '"' + m.RedirStdOut + '"', m.RedirStdOut)
>
>m.PS = m.WShell.Exec(m.PSCmd)
>
>DO WHILE m.PS.Status = 0
>	WAIT WINDOW "Waiting for PowerShell" TIMEOUT 1
>ENDDO
>
>DOEVENTS
>
>? "Script returned", m.PS.ExitCode
>? "Script output:"
>? "TEXT"
>? FILETOSTR(m.RedirStdOut)
>? "ENDTEXT"
>
>ERASE (m.PS1File)
>ERASE (m.RedirStdOut)
>
Thank you Antonio !

Alex
Previous
Reply
Map
View

Click here to load this message in the networking platform