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.

Run a PowerShell ps1 Script

 Keywords: AddScript Process Run Execute Powershell Script File .PS1 System.Management.Automation System.Management.Automation.PowerShell BeginInvoke EndInvoke

;/////////////////////////////////////////////////////////////////////////////////////////////////////////
;Winbatch 2013A - Process a simple Powershell script file
;                 Outputs to text file - combines web request and regex
;
;Stan Littlefield, May 6, 2013
;/////////////////////////////////////////////////////////////////////////////////////////////////////////
;test.ps1 (read in as file)
;$regex = [RegEx]'<div class="post-summary">(.*?)</div>'
;$url = 'http://blogs.msdn.com/b/powershell/'
;$wc = New-Object System.Net.WebClient
;$content = $wc.DownloadString($url)
;$regex.Matches($content) | Foreach-Object { $_.Groups[1].Value } | Out-File "c:\temp\matches.txt"
;/////////////////////////////////////////////////////////////////////////////////////////////////////////

If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

; Input Script
cScript = DirScript():"test.ps1"
If !FileExist(cScript) Then Terminate(@TRUE,"Cannot Continue",cScript:" not found")

; Output File
cFile = "C:\temp\matches.txt"   
If FileExist(cFile) Then FileDelete(cFile)

BoxOpen("Please Wait","Processing... ":cScript)

cRun=FileGet(cScript)
ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell") 
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
oPshell.AddScript(cRun,oScope)
objAsync = oPshell.BeginInvoke() 
oPShell.EndInvoke(objAsync)      
oPshell.Dispose()
oPshell=0

If FileExist(cFile)
   Run( cFile, '' )
Endif
BoxShut()

Exit


Test.PS1

$regex = [RegEx]'<div class="post-summary">(.*?)</div>'
$url = 'http://blogs.msdn.com/b/powershell/'
$wc = New-Object System.Net.WebClient
$content = $wc.DownloadString($url)
$regex.Matches($content) | Foreach-Object { $_.Groups[1].Value } | Out-File "c:\temp\matches.txt"

Article ID:   W17827
Filename:   Process a PowerShell Script File.txt
File Created: 2013:05:07:07:36:00
Last Updated: 2013:05:07:07:36:00