我試圖導出用戶列表,獲取這些用戶的隨機樣本,然後從原始列表中刪除該隨機樣本。如果我手動添加數據到這個數組像這樣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