使用变量作为值
Use Variable as Value
我有一个问题,因为我不能在 PowerShell 上使用变量作为散列 table 的值。
我有两个变量。
从 AD 获取 UserPrincipalName 并添加到 $alias
$alias = (Get-ADUser -Server "Add Server" -filter * -SearchBase "Add OU").userprincipalname
然后使用 $alias
我需要使用 Get-Mailbox
和 Get-MailboxStatistics
获得邮箱配额(以 MB 为单位)并取得百分比
$mailbox = foreach ($user in $alias) {
($user | Get-MailboxStatistics |
Select-Object @{name="TotalItemSize (MB)"; expression={
[Math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)
}})."TotalItemSize (MB)"
}
使用这个最大值。配额(100 GB on MB)
$maxquota = 102400
获得 %
$totalsize = foreach ($size in $mailbox) {
(($size * 100) / $maxquota)
}
邮箱大小示例
PS> $totalsize
2.43220703125
14.3363671875
8.875205078125
5.032177734375
15.548349609375
0.0112109375
然后我想用$alias
和$totalsize
做一个散列table
$test = foreach ($name in $alias) {
foreach($total in $totalsize){}
@{$name = $total}
}
结果应该是
键 - $alias
中的名称
值 - $total
中的百分比
例如:
David - 4.50
Juan - 15.00
Moises - 50
但是当我测试散列时table 我只是收到每个名称的所有值或所有名称仅具有第一个值
我怎样才能得到像 table 这样的所有值?
您正在将 $Test
设置为哈希表数组。分配您的 $Hashtable
,然后使用 Add
方法为其添加值:
Param (
$maxquota = 102400
)
$HashTable = @{}
$alias = (Get-ADUser -Server "Add Server" -filter * -SearchBase "Add OU").userprincipalname
$mailbox = Foreach ($user in $alias) {($user | Get-MailboxStatistics | Select-Object @{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}})."TotalItemSize (MB)"}
$totalsize = foreach ($size in $mailbox) {
(($size * 100) / $maxquota)
}
$test = Foreach($name in $alias){
$HashTable.Add($Name,$totalsize)
}
第 3 步中的代码不会创建哈希表,而是创建哈希表数组。此外,它不知道哪个邮箱大小属于哪个用户,因为该信息在步骤 2 后丢失。
像这样应该可以满足您的要求:
$test = @{} # create new empty hashtable
foreach ($user in $alias) {
$size = Get-MailboxStatistics -Identity $user |
Select-Object @{n='TotalItemSize (MB)';e={...}} |
Select-Object -Expand 'TotalItemSize (MB)'
$test[$user] = ($size * 100) / $maxquota
}
我有一个问题,因为我不能在 PowerShell 上使用变量作为散列 table 的值。
我有两个变量。
从 AD 获取 UserPrincipalName 并添加到
$alias
$alias = (Get-ADUser -Server "Add Server" -filter * -SearchBase "Add OU").userprincipalname
然后使用
$alias
我需要使用Get-Mailbox
和Get-MailboxStatistics
获得邮箱配额(以 MB 为单位)并取得百分比$mailbox = foreach ($user in $alias) { ($user | Get-MailboxStatistics | Select-Object @{name="TotalItemSize (MB)"; expression={ [Math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2) }})."TotalItemSize (MB)" }
使用这个最大值。配额(100 GB on MB)
$maxquota = 102400
获得 %
$totalsize = foreach ($size in $mailbox) { (($size * 100) / $maxquota) }
邮箱大小示例
PS> $totalsize 2.43220703125 14.3363671875 8.875205078125 5.032177734375 15.548349609375 0.0112109375
然后我想用
做一个散列table$alias
和$totalsize
$test = foreach ($name in $alias) { foreach($total in $totalsize){} @{$name = $total} }
结果应该是
键 -
中的百分比$alias
中的名称 值 -$total
例如:
David - 4.50 Juan - 15.00 Moises - 50
但是当我测试散列时table 我只是收到每个名称的所有值或所有名称仅具有第一个值
我怎样才能得到像 table 这样的所有值?
您正在将 $Test
设置为哈希表数组。分配您的 $Hashtable
,然后使用 Add
方法为其添加值:
Param (
$maxquota = 102400
)
$HashTable = @{}
$alias = (Get-ADUser -Server "Add Server" -filter * -SearchBase "Add OU").userprincipalname
$mailbox = Foreach ($user in $alias) {($user | Get-MailboxStatistics | Select-Object @{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}})."TotalItemSize (MB)"}
$totalsize = foreach ($size in $mailbox) {
(($size * 100) / $maxquota)
}
$test = Foreach($name in $alias){
$HashTable.Add($Name,$totalsize)
}
第 3 步中的代码不会创建哈希表,而是创建哈希表数组。此外,它不知道哪个邮箱大小属于哪个用户,因为该信息在步骤 2 后丢失。
像这样应该可以满足您的要求:
$test = @{} # create new empty hashtable
foreach ($user in $alias) {
$size = Get-MailboxStatistics -Identity $user |
Select-Object @{n='TotalItemSize (MB)';e={...}} |
Select-Object -Expand 'TotalItemSize (MB)'
$test[$user] = ($size * 100) / $maxquota
}