我无法让 Test-Path 从管道中工作。由于某种原因,该命令总是评估为假并执行 IF 语句中包含的命令。
代码如下所示:
$BACKUPFOLDER = "F:\TEST\Old\"
Get-ChildItem "$SrcFolder\*.pgp" | Where-Object {!$_.PsIsContainer} | ForEach-Object {
Add-Content -Path $LOGFILE -Value "`r`n - Decrypting file $_ -"
Decrypt-Files $_ "$SrcFolder$($_.BaseName)" $Credential $DecryptionLogFile
if (!(Test-Path "$BackupFolder$($_.BaseName)")) {
Add-Content -Path $LOGFILE -Value "`n`rMoving $_ to $BackupFolder"
Move-Item $_ $BackupFolder
}
else {
Add-Content -Path $LOGFILE -Value "`n`rCould not move $_ to $BackupFolder. Destination file already exists."
}
我得到的错误是 Move-Item 命令:
Move-Item : Cannot create a file when that file already exists.
At F:\Scripting\1-Dev\Modules\Include.ps1:505 char:26
+ Move-Item <<<< $_ $BackupFolder
+ CategoryInfo : WriteError: (F:\TEST...201301.xlsx.pgp:FileInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
我很困惑,因为该文件已经存在于备份文件夹中,但由于某种原因,脚本仍然通过 IF 语句并执行 move-item 命令。我的语法不适合测试路径吗?
我正在解密的文件将命名为 VPAS-201301.xlsx.pgp,而我正在提取的文件将是 VPAS-201301.xlsx。