Page 1 of 1

PowerShell From PB

Posted: Wed Oct 26, 2011 11:47 pm
by buddymatkona
This shows a few ways to use PowerShell by sending PureBasic strings.
If you need the results inside PB, one way to avoid the OOP interface is by piping PS output to a file.

I am starting a PS-PB string collection like DoPBFileCount$ and CompareFiles$ shown in the example below.

Code: Select all

; SendPowerShell is a variation on a Nituvious post of SendConsole
Global hInput
Procedure SendPowerShell(szText.s)
  szLength = Len(szText.s)
  For sz = 0 To szLength+1
    szCharacter$ = Mid(szText.s,sz+1,1)
    szLetterByLetter=Asc(szCharacter$)
    Delay(7) ; -- Make sure the szCharacter$ has been recorded. Else it may skip duplicates which is bad.
    PostMessage_(hInput,#WM_CHAR,szLetterByLetter,0)
  Next
  PostMessage_(hInput,#WM_CHAR,13,0)
EndProcedure

  DevDir$ = "D:\PBTest\" ; Your DIR and FILE info as needed here and below
  Cmd$ = "(Get-ChildItem "
  Filter$ = " -filter "  + Chr(34) + "*.pb" + Chr(34) ; your wildcard file choice here
  Property$ = ").Count"
  DoPBFileCount$ ="$FileCount = " + Cmd$ + DevDir$ + FILTER$ + Property$ ; PS will count files in DevDir
  CountToHost$ ="write-host $FileCount"
  File1$ = "$strReference = Get-Content " + Chr(34) + "D:\PBTest\T1.pb" + Chr(34)
  File2$ = "$strDifference = Get-Content " + Chr(34) + "D:\PBTest\T2.pb" + Chr(34)
  CompareFiles$ = "Compare-Object $strReference $strDifference"
  Name.s = "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" ; On Win7, this is really PS Ver 2
  
hInput = FindWindow_(0, @Name)
If hInput = 0
  RunProgram(name)
  Delay(100) ; don't look too soon :)
  hInput = FindWindow_(0, @Name)
EndIf

If hInput = 0
  Debug "Error, program did not open or string mismatch!"
Else
  SendPowerShell("echo Hello PSWorld!")
  
  SendPowerShell(DoPBFileCount$)
  SendPowerShell(CountToHost$) ;Show Dev:\Dir\*.pb FileCount
  
  SendPowerShell(File1$) 
  SendPowerShell(File2$)
  SendPowerShell(CompareFiles$) ; Show File1,File2 Differences
EndIf