Powershell 1.0을 사용하여 배열에서 항목을 제거하고 있습니다. 내 스크립트는 다음과 같습니다.PowerShell에서 배열에서 항목을 제거하는 방법은 무엇입니까?
param (
[string]$backupDir = $(throw "Please supply the directory to housekeep"),
[int]$maxAge = 30,
[switch]$NoRecurse,
[switch]$KeepDirectories
)
$days = $maxAge * -1
# do not delete directories with these values in the path
$exclusionList = Get-Content HousekeepBackupsExclusions.txt
if ($NoRecurse)
{
$filesToDelete = Get-ChildItem $backupDir | where-object {$_.PsIsContainer -ne $true -and $_.LastWriteTime -lt $(Get-Date).AddDays($days)}
}
else
{
$filesToDelete = Get-ChildItem $backupDir -Recurse | where-object {$_.PsIsContainer -ne $true -and $_.LastWriteTime -lt $(Get-Date).AddDays($days)}
}
foreach ($file in $filesToDelete)
{
# remove the file from the deleted list if it's an exclusion
foreach ($exclusion in $exclusionList)
{
"Testing to see if $exclusion is in " + $file.FullName
if ($file.FullName.Contains($exclusion)) {$filesToDelete.Remove($file); "FOUND ONE!"}
}
}
PowerShell의 Get-ChildItem은 System.Array 유형을 반환한다는 것을 알고 있습니다. 내가 뭘하려는 것은 ArrayList에에 $의 filesToDelete를 변환 한 후 ArrayList.Remove를 사용하여 항목을 제거입니다
Method invocation failed because [System.Object[]] doesn't contain a method named 'Remove'.
: 제거 방법을 사용하려고 할 때 나는 때문에이 오류가 발생합니다. 이게 좋은 생각인가요? 아니면 어떤 방식 으로든 $ filesToDelete를 System.Array로 직접 조작해야합니까?
감사
(한 오타'PSIsContainere') 네,'Where-Object'도 선호합니다. 그러나 질문에는 두 개의 루프가 있습니다 - 내부는'$ exclusionList'를 거쳐서'-not $ ($ f = $ _. Fullname; $ exclusionList |? {$ f.Contains ($ _)})' – stej
감사합니다. 리차드 - $ 제외에 문자열 배열을 사용할 수 있습니까? 코드를 자세히 살펴보면 모든 제외에 대해 get-childitem을 호출해야한다는 것을 알 수 있습니다. 내가 많은 예외를 가지고 있다면 이것은 잘 수행되지 않을 것이다. –
@stej : Correct – Richard