Invoke Powershell

Started by mathia, November 23, 2021, 11:43:18 AM

Previous topic - Next topic

mathia

I am at a loss as to how to get the values once this executes. The count returns 94, so I'm assuming something is being cpatured, but I don't know what to do to get the results. Everything I have tried gives me an Unknown COM object error.  Obviously, I'm trying to reuse a template I have seen many times on the forum and the website.  Thanks in advance for any assistance you might be able to offer.

:recursive2
   ; Example script.
   strScript = "get-childitem '\\extract\Development\AlfredMathis\pdfinfo' -recurse -force | select-object FullName"
   
   ; 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 varible.
   objPs.AddCommand("Set-Variable");
   objPs.AddParameter("name",  "proc");
   objPs.AddParameter("value", "WinBatch*");
   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()
      objCast = objItem.name
      Message("Process Info", "Process name: ":objCast.name:@CRLF:"id: )
   next
   objPs.Dispose()
   objPs=0
   
   exit

td

Ignoring some minor issues with your script for the moment, the "get-childitem" cmdlet returns a collection of objects of types System.IO.FileInfo and/or System.IO.DirectoryInfo. However, it is unclear what it is returning when you filter the output by a property name, "FullName" in your case.

The following worked on my system:
Code (winbatch) Select
strScript = "get-childitem 'C:\temp\*.wbt' -recurse -force"
; Boiler plate.
ObjectClrOption("useany", "System.Management.Automation")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
objPs     = objAutoPs.Create()
   
;    Set the script varible.
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.FullName())
next
objPs.Dispose()
objPs=0


You can use the PS cmdlet "(Get-Command Get-ChildItem).OutputType" to get return type info for the cmdlet.

[edit] Maybe one of the resident PS fans will jump in with more information about what the cmdlet is returning when you use the filter.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Just seems like a lot of overkill to get something you could do with native WB.

td

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

But, if OP is still interested.... [not elegant but workable]
Code (WINBATCH) Select


;Plan-B with Tony's code
;writes Powershell output to file
cFile= "c:\temp\wbfiles.txt"
If FileExist(cFile) Then FileDelete(cFile)
;change location of .wbt files to fit your situation
strScript = "$files = get-childitem 'C:\scripts\Powershell\*.wbt' -recurse":@LF
strScript = strScript:"$files.FullName | Out-File ":cFile:" -force"
; Boiler plate.
ObjectClrOption("useany", "System.Management.Automation")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
objPs     = objAutoPs.Create()   
;    Set the script varible.
objPs.AddScript(strScript,ObjectType("BOOL",@TRUE))
; Invoke the script.
objAsync = objPs.BeginInvoke()
objPs.EndInvoke(objAsync)
objPs.Dispose()
objPs=0
If FileExist(cFile)
   Run("notepad",cFile)
Else
   Terminate(@TRUE,"File Not Created",cFile)
Endif
Exit



td

I am still curious about the data type results other than "System-Object" are produced by the "select-object FullName" cmdlet in the OP's original script.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on November 24, 2021, 07:13:33 AM
I am still curious about the data type results other than "System-Object" are produced by the "select-object FullName" cmdlet in the OP's original script.


you can specify with 'Select-Object -Property Fullname' for clearer results. 


[EDIT]: actually that does not answer your question. But in playing around with it in PS the BaseType shows as System.Array.


$files = get-childitem 'C:\scripts\Powershell\*.wbt' -recurse | Select-Object -Property Fullname
$files.Properties
$files.GetType()



[EDIT_2] and with some versatility this one liner

Get-ChildItem C:\scripts\Powershell\ -Recurse -Include *.wbt | foreach { $_.FullName } | Out-File C:\temp\wbFiles.txt -force