2016-06-15 12 views
1

フォルダを別の場所にコピーした後に問題が発生しましたが、ディレクトリ内のフォルダの名前を変更して ".deploy"を最後から削除する必要がありますが、以下のエラーが表示されます。私はPowerShellの管理者権限についてはGoogleで検索しましたが、私のシナリオでは「キャッチオール」を見つけることができませんでした。ここでコピー後にフォルダの名前を変更

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 
    } 
} 

答えて

1

あなたはディレクトリのみを取得するためにGet-ChildItemコマンドレットに-Directoryスイッチを使用する必要があります。次に、Rename-Itemコマンドレットを使用して、フォルダの名前を変更します。私は、新しいフォルダ名を取得するには、簡単なregex-replace機能を使用します。

$deployFolders = Get-ChildItem $OldUserDir *.deploy -rec -Directory 
$deployFolders | Foreach { 
    $_ | Rename-Item -NewName ($_.Name -replace ('\.deploy$')) 
} 

あなたもForeach-Objectコマンドレット(Thanks to AnsgarWiechers)を使用する必要はありません:の

Get-ChildItem $OldUserDir *.deploy -rec -Directory | 
    Rename-Item -NewName { $_.Name -replace ('\.deploy$') } 
+0

無駄な使用を'ForEach-Object'です。 'Rename-Item'はパイプラインから読み込むことができるので、' Get-ChildItem'の出力に直接フィードすることができます。 –

+0

@AnsgarWiechersしかし、あなたは '.deploy'を置き換えるために古い名前にどうやってアクセスしますか? –

+3

'... |名前の変更 - アイテム-NewName {$ _。名前 - 置き換え '\ .deploy $'} '(中括弧に注意してください)。 –

関連する問題