Starting a DOS Batch file on a remote computer using PowerShell remoting

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.

photo

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.

Tags: , , , , , , , ,

PowerShell | Automation | Tips & Tricks

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2013, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2013 by Klaus Graefensteiner

Rendertime:
Page rendered at 5/19/2013 12:06:43 PM (PST Pacific Standard Time UTC DST -7)