1

所以我是 PowerShell 的新手,我正在尝试让这个功能正常工作。我有 2 个带有 3 个参数的 ValidateSet 数组。这些参数应该更改文件路径并将它们从一台服务器复制到另一台服务器。出于某种原因,我不断收到参数的命令提示符,而不是通过它们。我猜这是 ForEach-Object 的问题,但我不知所措。但是,它适用于 $ArchivePath。我是新人,所以请温柔点... TIA


param(

    [Parameter(Mandatory = $true)]
    [ValidateSet("One", "Two", "Three")]
    [string[]]$Channel

    ,[Parameter(Mandatory = $true)]
    [Alias('Phase')]
    [ValidateSet("Devl", "Test", "Prod")]
    [string[]]$Phase

    ,[Parameter(Mandatory = $false)]
    [string]$FilenameFilter = '.csv'

    ,[Parameter(Mandatory = $false)]
    [switch]$CreateTrigger
    )


function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode); exit $exitcode }

$exitcode = 0

try {

    # Get a list of files on the host server.
    #
   $files = Get-ChildItem -File -Path "\\ServerName\d\Extract\$Phase\FileTransfer\$Channel\Outbound"        

    # Destination directory.
    #
    $LocalPath = "\\ServerName\d\Extract\$Phase\FileTransfer\$Channel\Outbound"     #for testing


    # Set up folder name for Archive server. Formated as YYYYMMDDTHHMMSS  YYYYMMDD --> Var_Date, 'T' --> Var_Constant & HHMMSS --> Var_Time
    #
    $Var_Date = get-date -UFormat "%Y-%m-%d"
    $Var_Constant = 'T'
    $Var_Time = get-date -UFormat "%H-%M-%S"
    $Var_Fulldate = $Var_Date + $Var_Constant + $Var_Time

    $ArchivePath = $env:USERPROFILE + "\Desktop\$Channel\$Var_Fulldate"     #For testing
    New-Item -Type Directory -Path $ArchivePath


    if (-not (Test-Path -Path $ArchivePath -ErrorAction SilentlyContinue)) { $ArchivePath = $Env:TEMP }

    #Look for files in Outbound directory and remove
    Get-ChildItem -File -Path $LocalPath | ForEach-Object { Copy-Item $_.FullName } #Using copy instead of remove for test
    $FileCount = 0

Write-Output Try2   #for testing
pause               #for testing

    foreach ($file in $files) {
        if ((-not $file.IsDirectory) -and ($File.FullName -match $FilenameFilter)) {

            $localfilename = $LocalPath + $file.Name

            if (Test-Path $localfilename) { Copy-Item $localfilename }

            try {

                Copy-Item -Path $(Join-Path -Path $LocalPath -ChildPath $file.Name) -Destination $ArchivePath
    #Remove files from outbound since they've been archived
    #
                #Remove-Item -Path $file.FullName

            "Retrieved file $file"

                $FileCount++
            }
            catch {
                Write-Output Try13  #for testing
                $exitcode = 13
                "failed to retrieve $file"
            }
            finally {
                $error.Clear()
            }
        }
    }

}
catch {
Write-Output Try3
    $exitcode = 14
}
finally {
Write-Output Try4
    $error.Clear()
}

if ($CreateTrigger -and ($exitcode -eq 0) -and ($FileCount -gt 0)) {
    New-Item -Path "$LocalPath\Trigger_File.trg" -ItemType File | Out-Null
}

#ExitWithCode $exitcode     # take out for testing

输出:

PS C:\Users\me> \\Server\blah\USERS\me\My Documents\Folder\Get-FileName_t4.ps1
cmdlet Get-FileName_t4.ps1 at command pipeline position 1
Supply values for the following parameters:
Channel[0]: Three
Channel[1]: 
Phase[0]: Devl
Phase[1]: 


    Directory: C:\Users\me\Desktop\Three


Mode                LastWriteTime         Length Name                                                                                                
----                -------------         ------ ----                                                                                                
d-----       11/22/2019  12:17 PM                2019-11-22T12-17-23                                                                                 
Try2
Press Enter to continue...: 
Retrieved file File1_20191122080912.csv
Retrieved file File2_20191122080922.csv
Retrieved file File3_20191122080925.csv
Retrieved file File4_20191122080932.csv
Retrieved file File5_20191122080933.csv
Retrieved file File6_20191122080933.csv
Try4

4

1 回答 1

0

您收到提示是因为您没有传递这些参数,而是Mandatory=$true设置了您收到提示的参数。由于您的会话是交互式的,因此它会要求您输入正确的值。如果您不想收到提示,请提供强制参数:

"\\Server\blah\USERS\me\My Documents\Folder\Get-FileName_t4.ps1" -Channel Three -Phase Dev1

我注意到的其他几件事:

  • 您不需要提供Mandatory=$false,默认情况Mandatory$false
  • Phase为参数设置别名-Phase也是多余的
于 2019-11-22T21:57:09.833 回答