Men*_*ian 2 powershell exchange-server office365
我是 PowerShell 的新手(今天早上开始)。我已成功连接到 Office 365,并且能够从 Office 365 获取用户列表,并从 Exchange 部分获取邮箱字段。我不知道如何将它们结合起来。
我正在寻找的是能够从邮箱对象导出某些字段,但仅限于那些属于非阻止的、许可的 Office 365 用户的邮箱。我们有很多用户的邮箱尚未被删除,但他们可能不再获得许可或可能被阻止。
这是我现在正在运行的两个导出。它们是完全出口的。我尝试通过 isLicensed 筛选 Office 265 用户,但从未得到任何结果,因此我只是下载了所有内容并使用 Excel 对其进行了后期处理。但我需要定期运行这个......
这是代码:
Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName,Name,PrimarySMTPAddress,CustomAttribute2 | Export-CSV C:\temp\o365\mailboxes.csv
Get-MsolUser -all | Select-Object SignInName, DisplayName, Office, Department, Title, IsLicensed | export-csv c:\temp\o365\Users.csv
Run Code Online (Sandbox Code Playgroud)
任何援助将不胜感激。
好的,据我了解您要做什么...您想要获取该IsLicensed属性为$true且该BlockCredential属性为 的所有 O365 用户的列表$false。然后,您希望从这些用户的邮箱对象中提取一些数据;显示名称、名称、PrimarySMTPAddress 和 CustomAttribute2。
我们可以通过几种方法来做到这一点。第一个更容易在 shell 中组合在一起,但实际运行需要更长的时间。第二个需要一些设置,但很快就能完成。
因为我们知道我们想要的标准是什么Get-MsolUser,所以我们将使用管道取出我们想要的东西并将其直接扔进Get-Mailbox。
Get-MsolUser -All | Where-Object {$_.IsLicensed -eq $true -and $_.BlockCredential -eq $false} |
Select-Object UserPrincipalName |
ForEach-Object {Get-Mailbox -Identity $_.UserPrincipalName | Select-Object DisplayName,Name,PrimarySMTPAddress,CustomAttribute2}
Run Code Online (Sandbox Code Playgroud)
O365 PowerShell 不喜欢给我们提供过滤初始查询的方法,所以我们在第二步中处理这个问题,在这里......
Where-Object {$_.IsLicensed -eq $true -and $_.BlockCredential -eq $false}
这意味着,对于从 传入的每个项目Get-MsolUser -All,我们只需要那些属性Islicensed设置为 $true 和BlockCredential$false 的项目。
Get-MsolUser现在,我们只关心确定要查找哪些邮箱的结果,因此我们将从与之前的过滤器匹配的每个对象中获取一个属性。
Select-Object UserPrincipalName
如果您仅运行到目前为止的所有内容,您将在 shell 中获得我们现在要通过管道传输到的所有帐户的 UPN 列表Get-Mailbox。
继续我们的循环...如果您ForEach-Object还没有了解,它用于{}针对管道中的每个项目运行一个脚本块( 之间的所有内容),一次一个。
Get-Mailbox -Identity $_.UserPrincipalName
欢迎来到管道运营商($_)。我们之前的工作Select-Object是通过管道提供一组对象,这个占位符变量将在我们处理它们时保存每个对象。由于这些对象都有一个UserPrincipalName属性,我们引用该属性来将值传递给 的Identity参数Get-Mailbox。
Here's a simple example of how this works..
PS> 1,2,3 | ForEach-Object {Write-Host $_}
1
2
3
Run Code Online (Sandbox Code Playgroud)
Each item is passed along the pipeline, where we write them out one at a time. This is very similar to your standard foreach loop. You can learn more about their differences in this Scripting Guy post.
Moving on...
Select-Object DisplayName,Name,PrimarySMTPAddress,CustomAttribute2
We wrap it up with one last Select-Object for the information that you want. You can then look at the results in the shell or pipe into Export-Csv for working with in Excel.
Now... Since the pipeline works sequentially, there's some overhead to it. We're running a command, collecting the results, and then passing those results into the next command one at a time. When we get to our Get-Mailbox, we're actually running Get-Mailbox once for every UPN that we've collected. This takes about 2.5 minutes in my organization and we have less than 500 mailboxes. If you're working with larger numbers, the time to run this can grow very quickly.
Since a large amount of the processing overhead in the first method is with using the pipeline, we can eliminate most of it by handling our data collection as early and thoroughly as possible.
$Users = Get-MsolUser -All | Where-Object {$_.IsLicensed -eq $true -and $_.BlockCredential -eq $false} | Select-Object -ExpandProperty UserPrincipalName
$Mailboxes = Get-Mailbox | Select-Object UserPrincipalName,DisplayName,Name,PrimarySMTPAddress,CustomAttribute2
$Results = foreach ($User in $Users) {
$Mailboxes | Where-Object UserPrincipalName -eq $User
}
$Results | Export-Csv myFile.csv
Run Code Online (Sandbox Code Playgroud)
前两行非常不言自明。我们获取我们关心的所有用户帐户信息(仅 UPN),然后获取我们关心的所有邮箱属性。
foreach ($User in $Users)
中的每个条目$Users都将存储在 中$User,然后我们将在后面的脚本块中使用它(在 中{})。
$Mailboxes | Where-Object UserPrincipalName -eq $User
中的每个项目$Mailboxes都通过管道传输到Where-Object我们然后检查该UserPrincipalName属性是否等于 的当前值的位置$User。然后所有匹配项都存储在 中$Results,可以再次通过管道传输到Export-CsvExcel 中进行工作。
虽然此方法较难在 shell 中编写,并且需要一些额外的初始设置,但它的运行速度明显更快;我的组织需要 22 秒,而第一种方法需要 2.5 分钟。
UserPrincipalName我还应该指出,与邮箱数据集一起使用只是为了帮助确保这些数据集与帐户数据集之间的牢固匹配。如果您不希望它出现在最终结果中,您可以随时通过管道$Results输入另一个Select-Object并仅指定您关心的属性。
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
11222 次 |
| 最近记录: |