0

에서 제공하지만제거 품목은 파일 이름이 나에게 렸어요 나는 이것에 대해 매우 궁금 변수

먼저 그것을 알아낼 수 없을 때 작동하지 않는, 내가 얻을 수있는 다음과 같은 스크립트를 실행 이 아래와 같이 보여

$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 

은 그 때 나는 첫 번째 (2016-08-30_21-15-17_server-1.1.20558_client-1.1을 삭제하려고했습니다. 20518 - 복사 - 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

코리 길 (Kory Gill)이 정확합니다. 큰 따옴표로 묶인 문자열에서 문자열 배열을 참조 할 때는 배열 이름 앞에 $(을 추가하고 이후에는 )을 추가해야합니다. 따라서

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

원하는 결과를 얻지 못할 수도 있습니다. 아래는 배열의 문자열을 올바르게 가져 와서 문자열에 삽입합니다.

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

PowerShell을 처음 시작했을 때 저를 잡았던 작은 "gotchas"중 하나입니다.

+0

감사합니다. – user1888955

+0

PowerShell에서 먼저 무언가를 계산하기를 원한다면, "Something $ ($ RunThisFirst)"로 표시된 하위 식을 사용하면 PowerShell은 내부의 내용을 계산합니다 괄호가 제일 먼저 나온다. – user4317867

+0

@ user4317867 고마워, 나는 내 메모에 그것을 썼다 :) – user1888955

관련 문제