0

で提供されるが、私はは、Remove-Itemはファイル名は、それは私にしばらく時間がかかったように私はこのことについて非常に興味があります変数

まずそれを把握することができなかったときに動作していない、私は取得するには、次のスクリプトを実行しましたそれは以下のように示した

$entryList = New-Object System.Collections.ArrayList 

Get-ChildItem -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName" -ErrorAction Stop | sort -Property "LastWriteTime" | ForEach-Object { 
    if($_.Name.Contains(".zip")) { 
      $entryList.Add($_.Name) | Out-Null 
     } 
    } 

ディレクトリ内のすべてのZipファイルは:

2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy.zip 
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (2).zip 
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (3).zip 
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy.zip 
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (6).zip 
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy - Copy (2).zip 

は、その後、私は最初の1(2016-08-30_21-15-17_server-1.1.20558_client-1.1を削除しようとしました。 20518 - Copy - Copy.zip)を次のように削除します:

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\$entryList[0]" -ErrorAction Stop 

Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. 
At line:1 char:1 
+ Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$s ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : ReadError: (\\toolsbackup....lback\autopatch:String) [Remove-Item], PathTooLongException 
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.RemoveItemCommand 

パスが長すぎる例外があります。私は、ファイル名は$するentrylistことによってそれを渡すのではなく「削除-項目」に入れる場合は、[0]、それはあなたの問題はあなたの引用符で囲まれた文字列に「[0] $するentrylist」を使用している

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518 - Copy (2).zip" -ErrorAction Stop 
+0

脇に: '$ entryList = Get-ChildItem -Path" \\ tools-backup.nas \ Tools-Backup \ FakeS3 \ Rollback \ $ serverName "* .zip -ErrorAction Stop | sort -Property "LastWriteTime" ' – mklement0

答えて

2

を働きました。

実行この作品(もしくは動作しない)方法を確認するには、このコード...

$entryList = New-Object System.Collections.ArrayList 
$entryList.Add("This is an entry.") 

"Broken" 
# This is a string with: This is an entry.[0] 
Write-Output "This is a string with: $entryList[0]" 

"Fixed1" 
# This is a string with: This is an entry. 
Write-Output "This is a string with: $($entryList[0])" 

# or... 
"Fixed2" 
# This is a string with: This is an entry. 
$item = "This is a string with: {0}" -f $entryList[0] 
Write-Output $item 

あなたが何か試すことができます、代わりに名前を使用するのではなく、また、あなたを

Remove-Item -Path "\\tools-backup.nas\Tools-Backup\FakeS3\Rollback\$serverName\$($entryList[0])" -ErrorAction Stop 

をFullNameを使用するようにコードをリファクタリングする可能性があります。

$entryList.Add($_.FullName) 

お楽しみください。

+0

ありがとうございました:) – user1888955

1

コーリーギルが正しい。二重引用符で囲まれた文字列の中で文字列配列を参照する場合は、配列名の前に$(を、後に)を追加する必要があります。したがって、

Write-Host "This is a $test[0]" 

あなたが望む結果は得られません。以下は、配列内の文字列を正しく取得し、文字列に挿入します。

WRite-Host "This is a $($test[0])" 

それは私が最初のPowerShellに出始めたとき私を得たこれらのほとんどの「落とし穴」の一つです。

+0

ありがとうございました:) – user1888955

+0

PowerShellで最初に何かを計算させたい場合は、Something $($ RunThisFirst)で示された部分式を使用し、PowerShellが内部のものかっこが最初です。 – user4317867

+0

@ user4317867ありがとう、私は私のメモにそれを書いた:) – user1888955

関連する問題