+ Post New Thread
Results 1 to 4 of 4
Scripts Thread, Powershell running external commands with variables for arguments in Coding and Web Development; I'm just trying to do a simple script wrapper for robocopy but I seen to be running into an issue ...
  1. #1
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    5,009
    Thank Post
    120
    Thanked 282 Times in 260 Posts
    Rep Power
    108

    Powershell running external commands with variables for arguments

    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

    Code:
    robocopy d:\source d:\destination -e -zb
    So I built the command in the script similar to this:

    Code:
    $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.

    Code:
    $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.

    Code:
    $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.

    Code:
    $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

  2. #2


    Join Date
    Feb 2007
    Location
    51.403651, -0.515458
    Posts
    8,902
    Thank Post
    226
    Thanked 2,676 Times in 1,973 Posts
    Rep Power
    787
    Quote Originally Posted by ChrisH View Post
    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.

    Code:
    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;
    }
    
    # Example
    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.

  3. Thanks to Arthur from:

    ChrisH (6th March 2014)

  4. #3
    morganw's Avatar
    Join Date
    Apr 2009
    Location
    Cambridge
    Posts
    816
    Thank Post
    46
    Thanked 132 Times in 126 Posts
    Rep Power
    39
    Quote Originally Posted by ChrisH View Post
    I want to simplify it by having the extra options as one variable.
    Code:
    $strRobocopyoptions = @("-e", "-zb");

  5. Thanks to morganw from:

    ChrisH (6th March 2014)

  6. #4
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    5,009
    Thank Post
    120
    Thanked 282 Times in 260 Posts
    Rep Power
    108
    Thank you both @Arthur I will take a lot from your structure for function parameters for the future and @morganw thanks for introducing me to splatting!.

SHARE:
+ Post New Thread

Similar Threads

  1. ems launch via command line with variable
    By zbjsy in forum Windows Server 2008 R2
    Replies: 10
    Last Post: 2nd April 2012, 05:15 PM
  2. Replies: 4
    Last Post: 11th January 2011, 09:39 PM
  3. Blocking external email sending/recieving for a pupil
    By flyinghaggis in forum How do you do....it?
    Replies: 10
    Last Post: 7th September 2006, 10:34 AM
  4. Command line tool for share permissions
    By NetworkGeezer in forum Windows
    Replies: 8
    Last Post: 19th June 2006, 06:32 PM
  5. Internal/External Email with ISA and Exchange 2000
    By Ueline in forum Wireless Networks
    Replies: 5
    Last Post: 7th December 2005, 11:00 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •