2016-04-14 26 views
0

最近升級我的工作站到Windows10我一直在檢查我所有的舊腳本,似乎IndexOf的行爲有所不同。Powershell5 IndexOf行爲已更改 - 建議?

在PS4這個工作得很好:

$fullarray = $permissions | %{ 
    $obj = new-object psobject 
    $obj | add-member -name Group -type noteproperty -value $_.Account.AccountName 
    $obj | add-member -name Access -type noteproperty -value $_.AccessRights 
    $obj 
} 
$array = {$fullarray}.Invoke() 
# Convert array to list from which we can remove items 
$arraylist = [System.Collections.ArrayList]$array 
# Remove admin groups/users 
$ExcludeList | % { 
    $index = ($arraylist.group).IndexOf($_) 
    If ($index -gt -1) { 
     $arraylist.RemoveAt($index) | Out-Null 
    } 
} 

然而在PS5的只的IndexOf返回-1所有值。我無法找到一種方式來獲得它的ArrayList在所有的工作 - 來獲取在PS5工作,現在我有這樣的雜牌修復:

$array = {$fullarray}.Invoke() 
# Convert array to list from which we can remove items 
$arraylist = [Collections.Generic.List[Object]]($array) 
# Remove admin groups/users 
ForEach ($HideGroup in $ExcludeList) { 
    $index = $arraylist.FindIndex({$args[0].Group -eq $HideGroup}) 
    If ($index -gt -1) { 
     $arraylist.RemoveAt($index) # | Out-Null 
    } 
} 

爲什麼這個任何想法已經改變,如果你有一個更好的解決方案,將不勝感激!

回答

1

我不知道答案,爲什麼你看到不同的行爲與​​,但我會建議使用Where-Object,而不是你在做什麼:

$fullarray = $permissions | ForEach-Object { 
    $obj = new-object psobject 
    $obj | add-member -name Group -type noteproperty -value $_.Account.AccountName 
    $obj | add-member -name Access -type noteproperty -value $_.AccessRights 
    $obj 
} 
$filteredarray = $fullarray | Where-Object { $Excludelist -notcontains $_.Group } 
+0

好喊。 該腳本是隨着時間的演變,我想我最初更改爲一個ArrayList,因爲我也想根據其他條件(例如,如果用戶是成員已經)刪除,並添加一個項目,一旦我刪除了不需要的組,但我可以看到使用這種方法可能會更簡單。謝謝 – Scepticalist

+0

你也可以用'Where-Object'來做到這一點 –