Section 7.5. Recap: Replacing Common Batch File Syntax


7.5. Recap: Replacing Common Batch File Syntax

In this chapter, we've looked at several of the parallels between cmd.exe batch files and MSH scripts. It's safe to assume that anything that can be expressed in batch file syntax can be translated into an MSH script, but scripts cannot just be renamed and expected to run.

Table 7-1 is a quick reference that maps batch file syntax to MSH syntax. With more complex batch files, there may be a need to restructure their layout (especially if they're heavily dependent on GOTOs), but this should provide a good starting point.

Table 7-1. Batch file to MSH script cheat sheet

CMD.EXE batch file

MSH script

REM comment:: comment

# comment

PAUSE

read-host 'Press ENTER to continue'

CALL batchfile

& scriptfile

IF test command

if (test) { command }

IF NOT test command

if (!test) { command }

IF test command1

IF test command2

if (test) { command1; command2 }

IF EXIST path command

if (test-path path) { command }

SHIFT

$argPosition=0 # start of the script...

$args[$argPosition++]

%*

%1

$args

$args[0]

FOR %%f IN (filespec)

DO command

foreach

($f in $(get-childitem filespec)) { command }

IF ERRORLEVEL x command

if ($lastexitcode -eq x) { command }

GOTO label

See Untangle GOTO-based batch files

SET var=value

$env:var=value #environment variables

$var=value #shell variables


Let's now turn our focus away from cmd.exe migration tasks and toward some everyday tasks that are well-suited to MSH scripts. We'll shift gears for the next few pages and cover a number of different commands, scripts, and functions without getting into too much detail.




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net