RUn PS1 script with param

Started by erezpaz, June 14, 2015, 07:03:29 AM

Previous topic - Next topic

erezpaz

Hi

I want to run PS1 scripts from inside Winbatch and pass parameters to it. The PS1 will include param ($LogFile, $TTL) to accept my parameters. I found this KB article that help to run but I didn't find a whey to pass parameters to the PS1.

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/dotNet/System_Management/Automation/PowerShell+Process~a~PowerShell~Script~File.txt

Thanks
Erez

stanl

Winbatch essentially 'shells out' to powershell as my script you referenced indicates. If you know the parameters in advance, or collect them from a WB dialog, I would suggest you create a PS1 template with |p1| and |p2| as placeholders to be replaced with the actual parameters. Basically

Code (Winbatch) Select

PSscript=fileget(template)

PSscript=StrReplace(PSscript,"|p1|", param1)
PSscript=StrReplace(PSscript,"|p2|", param2)

......
oPshell.AddScript(PSscript,oScope)
objAsync = oPshell.BeginInvoke()
oPShell.EndInvoke(objAsync)     
oPshell.Dispose()
oPshell=0




td

If your PS script can be modified to use variables then something like the following might be useful:

Code (winbatch) Select
; Example script.
strScript = 'get-process |where {$_.Name -like $proc}'

; Boiler plate.
ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
objPs     = objAutoPs.Create()

; Set the script variable.
objPs.AddCommand("Set-Variable");
objPs.AddParameter("name",  "proc");
objPs.AddParameter("value", "WinBatch*");

; Add the script.
objPs.AddScript(strScript,ObjectType("BOOL",@TRUE))

; Invoke the script.
objAsync = objPs.BeginInvoke()
objPsCol = objPs.EndInvoke(objAsync)

; Display results.
nCount = objPsCol.Count - 1
for x = 0 to nCount
   objItem = objPsCol.Item(x)
   objCast = objItem.BaseObject()
   Message("Process Info", "Process name: ":objCast.ProcessName:@CRLF:"id: ":objCast.Id)
next
objPs.Dispose()
objPs=0
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade