我正在PowerShell中創建一個腳本,該腳本應該查找用戶的UPM文件夾並將其附加到.old(以便於配置文件重建)。Test-Path返回true,但Remove-Item找不到路徑
下面的代碼片段是我的代碼:
# Rename the UPM profile
Exit-PSSession
cd '\\SYLX-FS-01\D$\UPMProfiles'
$UPMPath = "$target.upm"
$UPMOld = "$target.upm.old"
if (Test-Path $UPMPath -IsValid) {
if (Test-Path $UPMOld -IsValid) {
Remove-Item $UPMOld
Rename-Item $UPMPath -NewName $UPMOld
Write-Host "Renamed UPMProfile"
} else {
Rename-Item $UPMPath -NewName $UPMOld
Write-Host "Renamed UPMProfile"
}
} else {
# Write-Host "UPM Profile not found, no action has been taken on the file server."
$UPM = "False"
}
我每次運行這個它返回該文件找不到錯誤。
Remove-Item : Cannot find path '\\SYLX-FS-01\D$\UPMProfiles\bill.odwyer.upm.old' because it does not exist. At line:8 char:20 + Remove-Item <<<< $UPMOld + CategoryInfo : ObjectNotFound: (\\SYLX-FS-01...er.upm.old:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
據我所知道的,它應該只去排隊8如果$UPMPath
存在,如果沒有的話它應該只是跳到else
。我錯過了什麼?