WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

PowerShell
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Invoke PowerShell Commands

 Keywords:  PowerShell Execute Run Invoke BeginInvoke EndInvoke Command System.Management.Automation System.Management.Automation.PowerShell Create AddCommand AddParameter AddArgument Get-Process

;***************************************************************************
;**   PowerShell Invoke Command 
;**
;** Purpose: Invokes PowerShell Commands
;** Inputs: Commands Parameters and Arguements
;** Outputs: Results in a Reportview
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**       
;** Developer: Deana Falk 2013.05.06
;***************************************************************************
If Version( )< '2013A'
   Pause("Notice", "Need 2013A or Newer Version of WInBatch")
   Exit
EndIf

;Host applications can use the PowerShell object to run any combination of cmdlets and scripts that can be invoked at the command line. 
;By using this object, the host application can create a pipeline, add commands and scripts to the pipeline, specify the runspace where 
;the commands are run, and then invoke the pipeline synchronously or asynchronously. For more information about host applications, see 
;Writing a Windows PowerShell Host Application.
;
;http://msdn.microsoft.com/en-us/library/ee706556(v=vs.85).aspx

ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell") ; "v4.0.30319"

oPowerShell = objAutoPs.Create()
oPowerShell.AddCommand("Get-Process")
oPowerShell.AddCommand("Sort-Object")
oPowerShell.AddParameter("descending")
oPowerShell.AddArgument("id")
oAsync = oPowerShell.BeginInvoke() ; Invoke() - invoke Does not work because can't determine which overload to use
oPsCollection = oPowerShell.EndInvoke(oAsync)

aRslts = ArrDimension(1024,2)
cntr = 0
ForEach oItem In oPsCollection
   oCast = oItem.BaseObject()
   aRslts[cntr,0] = oCast.Id
   aRslts[cntr,1] = oCast.ProcessName
   cntr = cntr+1
Next

; Display as an array
MyDialogFormat=`WWWDLGED,6.2`

MyDialogCaption=`Powershell Get-Process`
MyDialogX=002
MyDialogY=059
MyDialogWidth=766
MyDialogHeight=353
MyDialogNumControls=003
MyDialogProcedure=`DEFAULT`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=0

MyDialog001=`231,333,036,012,PUSHBUTTON,"PushButton_OK",DEFAULT,"OK",1,10,32,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`499,333,036,012,PUSHBUTTON,"PushButton_Cancel",DEFAULT,"Cancel",0,20,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`019,011,706,298,REPORTVIEW,"ReportView_1",aRslts,DEFAULT,DEFAULT,30,2097152,DEFAULT,DEFAULT,"192|192|192"`

ButtonPushed=Dialog("MyDialog")
Exit

Article ID:   W17825
Filename:   Invoke PowerShell Commands.txt
File Created: 2013:05:06:09:13:06
Last Updated: 2013:05:06:09:13:06