by Klaus Graefensteiner
8. April 2011 08:06
Introduction
Integrating PowerShell with an existing set of DOS batch files is not as easy as it should be. This blog post describes how to start a batch file on an remote computer from a PowerShell script. It also features a more advanced example of passing parameters into a batch file on a local computer, the batch file forwards the parameters to a PowerShell script. Then the PowerShell Remoting infrastructure finally sends some of these parameters to the batch file on the remote computer.

Figure 1: An original photo that doesn’t have anything to do with this blog post’s topic
Important
PowerShell Remoting is a beast. Here are some tricks that will help you taming it:
Simple remote batch file kick-off
#IMPORTANT: PSRemoting needs to be enabled on host and remote vm
$Username = "sky\bobster"
$Password = "skyster"
$ComputerName = "Cloud59"
$Script = {C:\HelloWorld.bat}
#Create credential object
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord
#Create session object with this
$Session = New-PSSession -ComputerName $ComputerName -credential $Cred
#Invoke-Command
$Job = Invoke-Command -Session $Session -Scriptblock $Script -AsJob
$Null = Wait-Job -Job $Job
#Close Session
Remove-PSSession -Session $Session
Advanced parameter hopping
Batch file that calls other batch file and passing in parameters
Start-RemoteBatchWithPS cloud59 sky\bobster skyste "C:\HerlloWorld.bat" "Hallo Welt!"
Batch file that calls PowerShell script with parameters from initial batch file
@echo %1
@echo %2
@echo %3
@echo %4
@echo %5
@echo %6
PowerShell -command "E:\SmokeTesting\Start-BatchFilePlus.ps1 %1 %2 %3 %4 %5 %6" -NoExit
PAUSE
PowerShell script
#IMPORTANT: PSRemoting needs to be enabled on host and remote vm
Param([string] $ComputerName, [string] $UserName, [string] $Password, [string] $RemoteBatchFile, [string] $RemoteBatchFileParam1, [string] $RemoteBatchFileParam2)
$DebugPreference = "continue"
Write-Debug $ComputerName
Write-Debug $UserName
Write-Debug $Password
Write-Debug $RemoteBatchFile
Write-Debug $RemoteBatchFileParam1
Write-Debug $RemoteBatchFileParam2
#Create credential object
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord
#Create session object with this
$Session = New-PSSession -ComputerName $ComputerName -credential $Cred
#Invoke-Command
$ScriptString = @'
Start-Process -FilePath "c:\windows\system32\cmd.exe" -ArgumentList "/k", {0}, {1} -NoNewWindow -PassThru -RedirectStandardError "C:\Testing\stderrorlog.txt" -RedirectStandardOutput "C:\Testing\stdoutlog.txt" -Wait
'@
$ScriptString = $ScriptString -f $RemoteBatchFile, "`"$RemoteBatchFileParam1 $RemoteBatchFileParam2`""
Write-Debug $ScriptString
$Script = [scriptblock]::Create($ScriptString)
$Job = Invoke-Command -Session $Session -Scriptblock $Script -AsJob
$Job
$Null = Wait-Job -Job $Job
#Close Session
Remove-PSSession -Session $Session
Batch file on remote computer
echo Greeting
echo %1 %2
Download
All files for testing these scripts are can be downloaded here: Start-BatchFile.zip
Ausblick
Integrating the old DOS commands and PowerShell is far to difficult for the casual user to pickup and be successful right from the get-go. PowerShell Remoting is also more for the advanced user. I hope over time PowerShell is going to address these usability issues.