<# .SYNOPSIS Prepares a Perl script for being run from a Perl installation available from a UNC path, a local drive or a network drive, in a self-contained cmd/batch file, that also contains the Perl code. If you have the path \\server\share\perl\bin\perl.exe, the specified path to Perl2Cmd should be "-PerlShare \\server\share\perl". If it's on a mapped drive, it would be "-PerlShare X:\somedir\perl" Author: Joakim Svendsen http://www.powershelladmin.com/wiki/Self-contained_batch_file_with_perl_code .PARAMETER PerlShare Path to root Perl directory, containing a bin subdirectory with perl.exe and a full Perl environment such as from a regular Strawberry or ActiveState Perl installation. .PARAMETER PerlScript Path to the Perl script you want to turn into a self-contained batch file. .PARAMETER OutputFile The name of the output file. If it exists, you will be prompted to overwrite. #> param( [Parameter(Mandatory=$true)][string] $PerlShare, [Parameter(Mandatory=$true)][string] $PerlScript, [Parameter(Mandatory=$true)][string] $OutputFile ) # Remove trailing backslashes and append \bin\perl.exe $perlShare = $perlShare -replace '\+$', '' $perlShare = $perlShare -replace '$', '\bin\perl.exe' # Exit if the source file doesn't exist if ( !(Test-Path $perlScript) ) { "Error: $perlScript does not exist. Exiting." exit 1 } # Be polite and prompt before overwriting an existing output file. if (Test-Path $outputFile) { $private:answer = Read-Host "$outputFile exists. Overwrite? (Y/n) [yes]" if ($private:answer -match '^n') { 'Aborting.'; exit } } $private:lines = Get-Content $perlScript $private:top = @" `@rem = '-- Batch wrapper created with Svendsen Tech Perl2Cmd (PowerShell version) -- `@echo off $perlShare %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 goto endofperl `@rem '; "@ $private:bottom = @" __END__ :endofperl "@ # Print some info. @" ########################################################## ### Creating cmd file: $outputFile ### Using Perl from: $perlShare ### From: $perlScript ########################################################## "@ # Write to the file. @($private:top) + $private:lines + @($private:bottom) | Out-File -encoding ASCII $outputFile 'Done!'