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.

PowerShell Invoke Command with Grid PassThru

 Keywords:  owerShell Invoke Command PassThru AddScript System.Management.Automation.PowerShell BeginInvoke EndInvoke get-service Where-Object {$_.status -eq "stopped"} out-gridview -Title -PassThru restart-service -Verbose

;***************************************************************************
;**   PowerShell Invoke Command with PassThru
;**
;** Purpose: Invokes PowerShell script with a grid
;** Inputs:  Title and Script
;** Outputs: none
;** Reference:
;**       PowerShell 3.0 introduces a -PassThru option. This means that the Gridview is not the end of the PS pipeline, but adds OK and Cancel buttons to the Grid and any selections are passed to subsequent pipeline commands if OK is clicked.
;**       REQUIRES WinBatch 2013A or newer
;**       
;** Developer: Stan Littlefield, Deana Falk 2013.05.13
;***************************************************************************
If Version( )< '2013A'
   Pause("Notice", "Need 2013A or Newer Version of WinBatch")
   Exit
EndIf

#DefineFunction GetPowerShellVersion()
   PSVer = '*UNKNOWN*'
   If RegExistKey( @REGMACHINE, 'SOFTWARE\Microsoft\PowerShell\3')
      If RegExistValue( @REGMACHINE, 'SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine[RuntimeVersion]' )
         PSVer = RegQueryValue( @REGMACHINE, 'SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine[RuntimeVersion]' )
      EndIf
   ElseIf RegExistKey( @REGMACHINE, 'SOFTWARE\Microsoft\PowerShell\1')
      If RegExistValue( @REGMACHINE, 'SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine[RuntimeVersion]' )
         PSVer = RegQueryValue( @REGMACHINE, 'SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine[RuntimeVersion]' )
      EndIf
   EndIf
   Return PSVer
#EndFunction

; Requires PowerShell 3.0 or newer
psver = GetPowerShellVersion()
majorver = ItemExtract( 1, psver, '.' )
If majorver < 3 
   Pause("Notice", "Need 3.0 or Newer Version of PowerShell")
   Exit
EndIf

;***************************************************************************
;**           Inputs
;***************************************************************************
; PowerShell 3.0 introduces a -PassThru option. This means that the Gridview is not the end of the PS pipeline, but adds OK and Cancel buttons to the Grid and any selections are passed to subsequent pipeline commands if OK is clicked.
title = `Select Service(s) to Restart`
script = `get-service | Where-Object {$_.status -eq "stopped"} | out-gridview -Title "`:title:`" -PassThru | restart-service -Verbose`

;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.AddScript(script,ObjectType("BOOL",@TRUE))
oAsync = oPowerShell.BeginInvoke() ; Invoke() - invoke Does not work because can't determine which overload to use
oPsCollection = oPowerShell.EndInvoke(oAsync)
oPowerShell.Dispose()
oPowerShell=0

Exit

Article ID:   W17826
Filename:   PowerShell Invoke Command with Grid PassThru.txt
File Created: 2013:05:13:08:41:08
Last Updated: 2013:05:13:08:41:08