使用选项

时间:2017-09-21 23:44:38

标签: powershell

我正在尝试为RoboCopy创建一个包装器,并且(似乎)传递的参数有问题。如果我在函数中定义选项,我可以使RoboCopy工作,但是传入的选项会导致RoboCopy失败。但是,如果我将传入的选项与本地定义的选项进行比较,看起来它们实际上是相同的。当我运行下面的代码时,使用传入参数的第一个RoboCopy会同时获取文件和选项,显示此信息并失败并显示路径错误。

Files : 
Options : /COPY:DAT /R:1000000 /W:30

但第二个正确显示

Files : *.xml
Options : /FFT /S /E /COPY:DAT /PURGE /MIR /LEV:1 /MT:8 /R:10 /W:5

它正确反映。 但是,最后的所有Ifs都返回YES。

function Invoke-PxMirror {
    [CmdletBinding()]
    param (
        [String]$source,
        [String]$destination,
        [String]$copyOptions,
        [String]$selectionOptions,
        [String]$retryOptions,
        [String]$logFile,
        [String]$logOptions
    )

    if ($copyOptions) {
        $copyOptions = $copyOptions.Split()
    }
    if ($selectionOptions) {
        $selectionOptions = $selectionOptions.Split()
    }
    if ($retryOptions) {
        $retryOptions = $retryOptions.Split()
    }
    if ($logFile) {
        $logFile = "/log:$logFile"
    }
    if ($logOptions) {
        $logOptions = $logOptions.Split()
    }

    $sourceX = '\\Mac\Px\Support\Px Tools\Recipe Validation'
    $destinationX = "$env:ProgramData\PragmaticPraxis"
    $copyOptionsX = '/mir /lev:1 /copy:dat /mt'
    $selectionOptionsX = '*.xml /fft'
    $retryOptionsX = '/r:10 /w:5'
    $logFileX = "$env:temp\PxAssets.log"
    $logOptionsX = '/njh /ns /nc /np'

    $copyOptionsX = $copyOptionsX.Split()
    $selectionOptionsX = $selectionOptionsX.Split()
    $retryOptionsX = $retryOptionsX.Split()
    $logFileX = "/log:$logFileX"
    $logOptionsX = $logOptionsX.Split()

    &RoboCopy $source $destination $copyOptions $selectionOptions $retryOptions
    &RoboCopy $sourceX $destinationX $copyOptionsX $selectionOptionsX $retryOptionsX


    if ($copyOptions -eq $copyOptionsX) {
        Write-host "copyOptions YES"
    } else {
        Write-host "copyOptions NO"
    }
    if ($selectionOptions -eq $selectionOptionsX) {
        Write-host "selectionOptions YES"
    } else {
        Write-host "selectionOptions NO"
    }
    if ($retryOptions -eq $retryOptionsX) {
        Write-host "retryOptions YES"
    } else {
        Write-host "retryOptions NO"
    }
    if ($logFile -eq $logFileX) {
        Write-host "logFile YES"
    } else {
        Write-host "logFile NO"
    }
    if ($logOptions -eq $logOptionsX) {
        Write-host "logOptions YES"
    } else {
        Write-host "logOptions NO"
    }

}



$mirrorParameters = @{
    source = '\\Mac\Px\Support\Px Tools\Recipe Validation'
    destination = "$env:ProgramData\PragmaticPraxis"
    copyOptions = '/mir /lev:1 /copy:dat /mt'
    selectionOptions = '*.xml /fft'
    retryOptions = '/r:10 /w:5'
    logFile = "$env:temp\PxAssets.log"
    logOptions = '/njh /ns /nc /np'
}
Invoke-PxMirror @mirrorParameters

更新: 所以,是的,改变变量类型(从字符串到字符串数组)并不是一个好主意。我怀疑我不需要为修改后的$ logFile指定一个字符串,但其余的似乎是正确的。当然它有效,但“作品”不足以成为“正确”。

function Invoke-PxMirror {
    [CmdletBinding()]
    param (
        [string]$source,
        [string]$destination,
        [string]$copyOptionsString,
        [string]$selectionOptionsString,
        [string]$retryOptionsString,
        [string]$logFile,
        [string]$logOptionsString
    )

    if ($copyOptionsString) {
        [string[]]$copyOptions = $copyOptionsString.Split(" ")
    }
    if ($selectionOptionsString) {
        [string[]]$selectionOptions = $selectionOptionsString.Split()
    }
    if ($retryOptionsString) {
        [string[]]$retryOptions = $retryOptionsString.Split()
    }
    if ($logFile) {
        [string]$logFile = "/log:$logFile"
    }
    if ($logOptionsString) {
        [string[]]$logOptions = $logOptionsString.Split()
    }

    &RoboCopy $source $destination $copyOptions $selectionOptions $retryOptions

$invokeMirror
}



$mirrorParameters = @{
    source = '\\Mac\Px\Support\Px Tools\Recipe Validation'
    destination = "$env:ProgramData\PragmaticPraxis"
    copyOptions = '/mir /lev:1 /copy:dat /mt'
    selectionOptions = '*.xml /fft'
    retryOptions = '/r:10 /w:5'
    logFile = "$env:temp\PxAssets.log"
    logOptions = '/njh /ns /nc /np'
}
Invoke-PxMirror @mirrorParameters

0 个答案:

没有答案