我正在尝试创建一个包含哈希表的数组。
这是我正在使用的代码。
Import-Module ActiveDirectory
$users = Get-ADUser -Filter {enabled -eq $true} -properties * | ? {$_.DistinguishedName -like "*,OU=Customer Service,OU=Basic,*" -and $_.DistinguishedName -NotLike "*Mercury Test Users*" -and $_.EmailAddress -Like "*@*"}
$users.Count
$mailboxes
foreach($user in $users){
$formatted = @{}
$mailbox = Get-mailbox -Identity $user.Name
$formatted.Name = $user.Name
$formatted.Email = $mailbox.PrimarySmtpAddress.Local + "@" + $mailbox.PrimarySmtpAddress.Domain
$mailboxes += $formatted
}
$mailboxes
它输出
除第一个值之外的所有错误文本都重复。
我希望有一些可以访问的东西,例如 $mailboxes[X].Name,而不是 $mailboxes.Name
答案1
该New-Object
cmdlet 将在此处帮助您,请尝试一下。
Import-Module ActiveDirectory
$users = Get-ADUser -Filter { enabled -eq $true } -properties * | ? { $_.DistinguishedName -like "*,OU=Customer Service,OU=Basic,*" -and $_.DistinguishedName -NotLike "*Mercury Test Users*" -and $_.EmailAddress -Like "*@*" }
$users.Count
$mailboxes = foreach ($user in $users) {
$mailbox = Get-mailbox -Identity $user.Name
New-Object PSObject -Property @{
"Name" = $user.Name
"Email" = $mailbox.PrimarySmtpAddress.Local + "@" + $mailbox.PrimarySmtpAddress.Domain
}
}
$mailboxes