Я пытаюсь экспортировать список пользователей, получить случайный образец этих пользователей, а затем удалить этот случайный образец из исходного списка. Если я вручную добавлю данные в массив, например, Technet article, он работает нормально. Но когда я пытаюсь импортировать из csv или использовать Get-Content для получения данных, он ничего не удалит. Любая помощь будет принята с благодарностью.Обновление переменных массивов в Powershell
<#-->The first 4 lines are the same for all attempts. Between attempts I exited PowerShell.<--#>
$Date = Get-Date -DisplayHint Date
Get-ADUser -Filter * -SearchBase "OU=Users,DC=Domain" -SearchScope OneLevel -Properties sAMAccountName |
Select-Object @{Name="User ID";Expression={$_."sAMAccountName"}}, @{Name="Date";Expression={$Date}} |
Export-Csv "C:\File.csv" -NoTypeInformation
#-->First Attempt<--#
$Import = Import-Csv C:\File.csv
$Sample = Get-Random -InputObject $Import -Count 20
$List = $Import -ne $Sample
#This shows 20.
$Sample.Count
#These both show the same number instead of $List being 20 less than $Import.
$Import.Count
$List.Count
#-->Second Attempt<--#
$Import = Get-Content C:\File.csv
$Sample = Get-Random -InputObject $Import -Count 20
$List = $Import -ne $Sample
#This shows 20.
$Sample.Count
#These both show the same number instead of $List being 20 less than $Import.
$Import.Count
$List.Count
#-->Third Attempt<--#
[System.Collections.ArrayList]$Import = Import-Csv C:\File.csv
[System.Collections.ArrayList]$Sample = Get-Random -InputObject $Import -Count 20
[System.Collections.ArrayList]$List = $Import -ne $Sample
#This shows 20.
$Sample.Count
#These both show the same number instead of $List being 20 less than $Import.
$Import.Count
$List.Count
#-->Fourth Attempt<--#
[System.Collections.ArrayList]$Import = Import-Csv C:\File.csv
[System.Collections.ArrayList]$Sample = Get-Random -InputObject $Import -Count 20
[System.Collections.ArrayList]$List = $Import.Remove($Sample)
#This shows 20.
$Sample.Count
#This shows 1000.
$Import.Count
#This shows 0.
$List.Count
Я думаю, что вы хотите это: '$ List = (Compare-Object -ReferenceObject $ Import -DifferenceObject $ Sample) .InputObject'. –
@AlexanderObersht спасибо, это работает. – pshellnewb