Azure Powershell过滤
因此,我正在编写一个连接到Azure AD和Exchange Online的脚本,以检索使用率超过90gb的所有邮箱,唯一的问题是它需要很多时间,因为它会在所有邮箱中运行,即使是那些没有达到90gb的邮箱,当我尝试使用“-Filter”参数时,我会收到一个在cmdlet中无法识别的错误 这是我的台词-Azure Powershell过滤,azure,powershell,azure-active-directory,powershell-2.0,azure-powershell,Azure,Powershell,Azure Active Directory,Powershell 2.0,Azure Powershell,因此,我正在编写一个连接到Azure AD和Exchange Online的脚本,以检索使用率超过90gb的所有邮箱,唯一的问题是它需要很多时间,因为它会在所有邮箱中运行,即使是那些没有达到90gb的邮箱,当我尝试使用“-Filter”参数时,我会收到一个在cmdlet中无法识别的错误 这是我的台词- $members = Get-Mailbox -resultsize unlimited | %{Get-MailboxStatistics $_.distinguishedname} | Sel
$members = Get-Mailbox -resultsize unlimited | %{Get-MailboxStatistics $_.distinguishedname} | Select Identity, DisplayName, @{name=”TotalItemSize”; expression={[math]::Round(($_.TotalItemSize.ToString().Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}}, ItemCount -Filter "TotalItemSize -gt 90"
-Filter
不是选择对象的参数
这应该可以解决这个问题:
$members=Get Mailbox-resultsize unlimited-Filter“TotalItemSize-gt'90'”{Get MailboxStatistics$.differentizedName}}124;选择标识,显示名称,@{name=“TotalItemSize”;表达式={[math]::Round($..TotalItemSize.ToString().Split(“”[1]。Split(“”[0]。替换(“,”)/1GB),2)},ItemCount
不幸的是,属性TotalItemSize
不在其中,因此您只能使用如下ForEach对象循环:
$threshHoldBytes = 90GB # --> 96636764160 bytes
$largeMailBoxes = Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | ForEach-Object {
$totalByteSize = [int64]($_.TotalItemSize.ToString() -replace '^[^\(]+\(([\d,]+\s*bytes)\)', '$1' -replace '\D')
if ($totalByteSize -gt $threshHoldBytes) {
$_ | Select-Object Identity DisplayName,
@{Name = 'TotalItemSize (GB)'; Expression={[math]::Round($totalByteSize / 1Gb, 2)}},
ItemCount
}
}
# display on screen
$largeMailBoxes | Format-Table -AutoSize # or use Out-GridView if it doesn't fit the console width
# export to Csv
$largeMailBoxes | Export-Csv -Path 'Path\To\OverSizedMailboxes.csv' -NoTypeInformation
或者使用Where Object
子句(这意味着您必须做两次数学运算..)
您还可以创建一个小的辅助函数来进行计算,这样代码就更容易理解了:
function Get-ItemSizeInGB ([string]$totalSize) {
# you can also use this function for property TotalDeletedItemSize
$byteSize = [int64]($totalSize -replace '^[^\(]+\(([\d,]+\s*bytes)\)', '$1' -replace '\D')
# return the size in Gb, rounded to two decimals
[math]::Round($byteSize / 1Gb, 2)
}
现在你可以做了
$threshHoldGB = 90 # this time, we have a threshold in Gb
$largeMailBoxes = Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics |
Where-Object { (Get-ItemSizeInGB $_.TotalItemSize.ToString()) -gt $threshHoldGB } |
Select-Object Identity DisplayName,
@{Name = 'TotalItemSize (GB)'; Expression={(Get-ItemSizeInGB $_.TotalItemSize.ToString())}},
ItemCount
# display on screen
$largeMailBoxes | Format-Table -AutoSize # or use Out-GridView if it doesn't fit the console width
# export to Csv
$largeMailBoxes | Export-Csv -Path 'Path\To\OverSizedMailboxes.csv' -NoTypeInformation
正则表达式详细信息:
^ Assert position at the beginning of the string
[^\(] Match any character that is NOT a “(”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\( Match the character “(” literally
( Match the regex below and capture its match into backreference number 1
[\d,] Match a single character present in the list below
A “digit” (any decimal number in any Unicode script)
The literal character “,”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
bytes Match the character string “bytes” literally (case sensitive)
)
\) Match the character “)” literally
The `-replace '\D'` removes all non-numeric characters
<代码> >“TooTimeSimule- GT’90’<代码>大于90字节。它是否工作?无法将参数“过滤器”绑定到目标。异常设置“过滤器”:“TooTimeMeStudio”不是一个可识别的可过滤属性。我们没有收到您的消息。是否有任何给定的答案解决了您的问题?如果是,请点击它来考虑它。✓ 左边的图标。这将帮助有类似问题的其他人更容易找到它,并有助于激励其他人回答您将来可能遇到的任何问题。
^ Assert position at the beginning of the string
[^\(] Match any character that is NOT a “(”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\( Match the character “(” literally
( Match the regex below and capture its match into backreference number 1
[\d,] Match a single character present in the list below
A “digit” (any decimal number in any Unicode script)
The literal character “,”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s Match a single character that is a “whitespace character” (any Unicode separator, tab, line feed, carriage return, vertical tab, form feed, next line)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
bytes Match the character string “bytes” literally (case sensitive)
)
\) Match the character “)” literally
The `-replace '\D'` removes all non-numeric characters
$members = Get-Mailbox -resultsize unlimited | %{Get-MailboxStatistics $_.distinguishedname} | Select Identity, DisplayName, @{name=”TotalItemSize”; expression={[math]::Round(($_.TotalItemSize.ToString().Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}}, ItemCount | ?{$_.TotalItemSize -gt 90}