Jump to content

Powershell running external commands with variables for arguments


Recommended Posts

Posted

I'm just trying to do a simple script wrapper for robocopy but I seen to be running into an issue in the way in which it is interpreting the variable for the arguments.

 

This is the form of the command I want to run

 

robocopy d:\source d:\destination -e -zb

 

So I built the command in the script similar to this:

 

$strSourceFolders = "E:\Test Folders\SourceFolder";
$strDestinationFolders = "E:\Test Folders\DestinationFolder";
$strRobocopyoptions = "-e -zb";

robocopy $strSourceFolders $strDestinationFolders $strRobocopyOptions

 

When this is run it takes the two arguments in the $strRobocopyOptions as one argument eg "-e -zb" and not "-e" and "zb" and therefore produces an error. I have tried adding spaces etc as part of the variable with no luck.

 

$strSourceFolders = "E:\Test Folders\SourceFolder";
$strDestinationFolders = "E:\Test Folders\DestinationFolder";
$strRobocopyoptions = "-e";

robocopy $strSourceFolders $strDestinationFolders $strRobocopyOptions

 

The above works as there is only one extra argument provided.

 

$strSourceFolders = "E:\Test Folders\SourceFolder";
$strDestinationFolders = "E:\Test Folders\DestinationFolder";
$strRobocopyoptions1 = "-e";
$strRobocopyoptions2 = "-zb";

robocopy $strSourceFolders $strDestinationFolders $strRobocopyOptions1 $strRobocopyOptions1

 

It works in the above format as well.

 

$strSourceFolders = "E:\Test Folders\SourceFolder";
$strDestinationFolders = "E:\Test Folders\DestinationFolder";

robocopy $strSourceFolders $strDestinationFolders "-e -zb"

 

The above works as well but I want to simplify it by having the extra options as one variable as I usually do in VB Script. Can someone point me in the right direction of how to get this interpreted as I desire ?

 

Cheers

 

Chris

Posted
I want to simplify it by having the extra options as one variable

Here's one way you can do it. Hopefully the script below is self-explanatory, but if you have any questions just ask.

 

function RoboWrapper {

 [CmdletBinding()]
   param (
       [Parameter(Mandatory = $true)]
         [string] $Source, 
       [Parameter(Mandatory = $true)]
         [string] $Destination,
       [Parameter(Mandatory = $false)]
         [string] $LogPath = ( "$PSScriptRoot\{0} {1}.log" -f "Robocopy", (Get-Date).ToString("yyyy-MM-dd hh.mm.ss") )
   )

   <# 
   -------------------------------------------------------
   Robocopy Parameter Reference
   -------------------------------------------------------
   MIR = Mirror a directory
   NP  = No Progress - Don't display percentage copied
   NDL = No Directory List - Don't log directory names
   NC  = Don't log file classes (existing, new file, etc.)
   TEE = Output to console window, as well as the log file
   R:n = Number of retries on failed copies: default is 1 million
   W:n = Wait time between retries: default is 30 seconds
   BYTES = Show file sizes in bytes
   ------------------------------------------------------- 
   #>
   $RobocopyParameters = '/MIR /NP /NDL /NC /TEE /R:3 /W:3 /BYTES';
   $ArgumentList = '"{0}" "{1}" /LOG:"{2}" {3}' -f $Source, $Destination, $LogPath, $RobocopyParameters;

   Write-Verbose -Message ('Starting Robocopy job with arguments: {0}' -f $ArgumentList);
   Start-Process -Wait -FilePath Robocopy.exe -ArgumentList $ArgumentList -NoNewWindow;
}

# [color="#FF0000"]Example[/color]
RoboWrapper -Source "C:\Stuff\Source" -Destination "C:\Stuff\Destination" -Verbose

 

I created a function because you can copy-and-paste it into your PowerShell profile (minus the example at the end) and access 'RoboWrapper' from any PowerShell prompt just as you would with the standard cmdlets.

 

By the way, it's not a good idea to use Hungarian notation with PowerShell. :eek:

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...