1
將文件夾複製到其他位置後出現問題,我需要重命名目錄中的文件夾以從最後刪除「.deploy」,但我在下面得到以下錯誤消息。我搜索了PowerShell管理權限,但似乎無法找到適用於我的場景的「全部」。複製後重命名文件夾
Get-Content : Access to the path 'C:\OldUserBackup\a.deploy' is denied. At C:\PSScripts\DesktopSwap\TestMergeDir.ps1:28 char:14 + (Get-Content $file.PSPath) | + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (C:\OldUserBackup\a.deploy:String) [Get-Content], UnauthorizedAccessException + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand
以下是我有:
$UserName = [Environment]::UserName
$CurrUser = [Environment]::UserName + '.deploy'
$OldUserDir = 'C:\OldUserBackup'
$CurrDate = Get-Date -format G
$PathExist = Test-Path $OldUserDir
if ($PathExist -eq $true) {
#Copy Desktop, Downloads, Favorites, Documents, Music, Pictures, Videos
Copy-Item -Path $OldUserDir -Destination C:\Users\$UserName\Desktop\CopyTest -Recurse -Force
$configFiles = Get-ChildItem $OldUserDir *.deploy -rec
foreach ($file in $configFiles) {
(Get-Content $file.PSPath) |
Foreach-Object { $_ -replace ".deploy", "" } |
Set-Content $file.PSPath
}
}
沒用使用'的foreach Object'。 'Rename-Item'可以從管道中讀取,所以你可以直接給它提供'Get-ChildItem'的輸出。 –
@AnsgarWiechers但你將如何訪問舊名稱來替換'.deploy'? –
'... | Rename-Item -NewName {$ _。Name -replace'\ .deploy $'}'(注意花括號)。 –