我正在制作一个PowerShell脚本,它查询Office365个租户,并将某些信息导出到一个.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)而不是“分销商-帐户:SKU零件号”,我希望把它缩短到我们通常提到的名称!例如:
两个问题真的很相似,但非常相似!希望你能帮上忙。
发布于 2018-10-19 09:56:19
为了实现这一点,您可以使用计算性质和一个小函数将SkuId转换为友好的名称,并使用-replace
删除SMTP部分,我为您创建了一个简单的转换函数,您可以像我一样添加其他产品:
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' }
}
}
然后使用计算的属性替换“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
发布于 2018-10-19 10:02:53
您可以使用哈希表作为所需翻译的查找表,如下所示:
# 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
https://stackoverflow.com/questions/52888419
复制相似问题