PowerShell -使用PSCustomObject重命名MsolUser属性

bpsygsoo  于 2023-01-26  发布在  Shell
关注(0)|答案(1)|浏览(138)

我正在尝试创建一个脚本,该脚本将导入UPN的csv并获得分配的365个许可证。要获得许可证,我们必须使用{$.licenses.accountskuid}。当我导出到文件时,标头当然是{$.licenses.accountskuid}。我正在使用PSCustomObject尝试清理最终结果,但不确定缺少了什么。
这是脚本。导出的文件显示的列标题与我想要的完全一样,但{$.licenses.accountskuid}下没有数据。其他列的所有数据都显示正确。我确定我只是{$.licenses.accountskuid}的语法错误。我仍在学习PowerShell,所以如果有更简单的方法,我会洗耳恭听,并愿意学习。谢谢

`Import-CSV -Path "C:\Temp\users.csv" | ForEach-Object {
$msUser = Get-MsolUser -UserPrincipalName $_.UserPrincipalName 
    [PScustomobject]@{
            UserPrincipalName = $msUser.UserPrincipalName
            DisplayName       = $msUser.FullName
            IsLicensed        = $msUser.IsLicensed
            LicenseSKU        = $msUser.{$_.licenses.accountskuid} }
            } |
Export-CSV -Path "C:\temp\output.csv" -Append -NoTypeInformation`
toe95027

toe950271#

继续我的评论并回答您的第二个问题如何获得SKU产品代码的友好名称:

# create a lookup Hashtable to convert the SKU product codes to friendly names.
# for demo, this is a shortened list. If you want the complete (MASSIVE) lookup table see
# https://scripting.up-in-the.cloud/licensing/list-of-o365-license-skuids-and-names.html
$skuids = @{
    'AAD_BASIC'='Azure Active Directory Basic'
    'INTUNE_A'='Microsoft Intune'
    'O365_BUSINESS'='Microsoft 365 Apps for Business'
    'OFFICE_BASIC'='Office 365 Basic'
    'SHAREPOINTWAC_DEVELOPER'='Office for the Web for Developer'
    'SHAREPOINTWAC_EDU'='Office for the Web for Education'
    'SPB'='Microsoft 365 Business Premium'
    'SPE_E3'='Microsoft 365 E3'
    'SPE_E5'='Microsoft 365 E5'
    'SPE_F1'='Microsoft 365 F3'
    'STANDARDPACK'='Office 365 E1'
    'TEAMS1'='Microsoft Teams'
    'VISIO_CLIENT_SUBSCRIPTION'='Visio Online'
    'WIN10_PRO_ENT_SUB'='Windows 10/11 Enterprise (Original)'
    'YAMMER_ENTERPRISE'='Yammer Enterprise'
}

Import-CSV -Path "C:\Temp\users.csv" | ForEach-Object {
    $msUser  = Get-MsolUser -UserPrincipalName $_.UserPrincipalName 
    # get the SKU product id part of the user license and try to
    # convert that to its friendly name using the $skuids Hashtable
    $license = ($msUser.licenses.AccountSkuId -split ':')[-1].Trim()
    if ($skuids.ContainsKey($license)) { $license = $skuids[$license] }

    [PScustomobject]@{
        UserPrincipalName = $msUser.UserPrincipalName
        DisplayName       = $msUser.FullName
        IsLicensed        = $msUser.IsLicensed
        LicenseSKU        = $license
    }
} | Export-CSV -Path "C:\temp\output.csv" -Append -NoTypeInformation

相关问题