我正在制作一个PowerShell脚本,该脚本查询我们的Office 365租户并将某些信息导出到.csv文件中。我正在努力解决的两个字段是用户的默认电子邮件地址及其分配的订阅。我可以获取数据,但不确定如何操作并使其看起来更美观。
Get-MSOLUser -All | select firstname,lastname,displayname,islicensed,{$_.Licenses.AccountSkuId},{$_.proxyaddresses -cmatch '^SMTP\:.*'},userprincipalname | sort FirstName | Export-Csv $directory\$tenantname\Export.csv -NoTypeInformation
1)我设法获取了他们的主要电子邮件地址,因为小写的smtp地址始终是别名,但是如何删除“ SMTP:”部分?
2)我希望将其简化为我们通常称呼它们的名称,而不是“ reseller-account:SKU部件号”!如:
两个问题确实非常相似!希望您能提供帮助。
答案 0 :(得分:1)
要实现您可以使用Calculated Properties以及一个小函数将SkuId转换为友好名称,并使用-replace
删除SMTP部分的功能,我为您创建了一个简单的函数转换后,您可以像我一样添加其他产品:
可以在此link
中找到Microsoft产品名称/ SKU的列表。function Convert-SkuIdToFriendlyName
{
Param(
[string]$SkuId
)
switch ($SkuId)
{
{$SkuId -match "ENTERPRISEPACK"} {return "OFFICE 365 ENTERPRISE E3"}
{$SkuId -match "ENTERPRISEPREMIUM"} {return "OFFICE 365 ENTERPRISE E5"}
default { 'Unknown' }
}
}
然后使用Calculated属性替换“ SMTP”部分并转换SkuId:
Get-MSOLUser -All |
Select firstname,lastname,displayname,islicensed,
@{N="License";E={Convert-SkuIdToFriendlyName $_.Licenses.AccountSkuId}},
@{N="Email";E={$_.proxyaddresses -cmatch '^SMTP\:.*' -replace 'SMTP\:'}},userprincipalname |
Sort FirstName
答案 1 :(得分:1)
您可以将哈希表用作所需翻译的查找表,如下所示:
# create a hash with the desired translations.
# below are just the examples from your question. You need to fill in the rest..
$Licenses = @{
"ENTERPRISEPACK" = "E3"
"ENTERPRISEPREMIUM" = "E5"
"PROJECTPROFESSIONAL" = "ProjectPro"
"VISIOCLIENT" = "Visio"
}
Get-MSOLUser -All |
Select-Object firstname,lastname,displayname,islicensed,userprincipalname,
@{ Name = 'License'; Expression = { $Licenses[$(($_.Licenses.AccountSkuId) -replace '^.+:', '')] }},
@{ Name = 'PrimaryEmailAddress'; Expression = { ($_.proxyaddresses -cmatch '^SMTP\:.*') -replace "SMTP:", "" }} |
Sort-Object FirstName | Export-Csv $directory\$tenantname\Export.csv -NoTypeInformation
为了获得用户可以列出的所有许可证,可以将代码扩展为:
# create a hash with the desired translations for the license plans.
# below are just the examples from your question. You need to fill in the rest..
$Licenses = @{
"ENTERPRISEPACK" = "E3"
"ENTERPRISEPREMIUM" = "E5"
"PROJECTPROFESSIONAL" = "ProjectPro"
"VISIOCLIENT" = "Visio"
}
# this calculated property returns all (translated) licenses per user as comma delimited string
$userLicenses = @{
Name = 'Licenses'
Expression = {
$result = @()
foreach($lic in $_.Licenses) {
$result += $Licenses[$(($lic.AccountSkuId) -replace '^.+:', '')]
}
$result -join ', '
}
}
Get-MSOLUser -All |
Select-Object firstname,lastname,displayname,islicensed,userprincipalname,$userLicenses,
@{ Name = 'PrimaryEmailAddress'; Expression = { ($_.proxyaddresses -cmatch '^SMTP\:.*') -replace "SMTP:", "" }} |
Sort-Object FirstName | Export-Csv $directory\$tenantname\Export.csv -NoTypeInformation