O365 PowerShell Pull SubLicense

时间:2015-09-02 17:07:00

标签: powershell office365

我正在尝试提取我的O365租户中的所有用户的列表,如果他们获得许可,则会获得他们已授予的子许可列表。以下代码非常适合列出我的子许可证:

$userlicensetest = get-msoluser -userprincipalname "steve.dorr@merrillcorp.com"
$userlicensetest.licenses[0].servicestatus

ServicePlan :: ProvisioningStatus

----------- :: ------------------

INTUNE_O365 :: PendingActivation

YAMMER_ENTERPRISE :: PendingInput

OFFICESUBSCRIPTION ::成功

所以我尝试修改我在网上找到的代码以包含从属许可证信息。这是我到目前为止所建立的:

$ReportPath = "c:\users\userlist.csv"
Add-Content -value ("UserPrincipalName"+","+"IsLicensed"+","+ "Licenses”"+","+ "SubLicenses") -Path $ReportPath
$AllUsers = Get-MsolUser -All 
foreach ($User in $AllUsers)
{
 $UserPrincipalName = $User.UserPrincipalName
 $IsLicensed = $User.IsLicensed
 $Licenses = $User.Licenses.AccountSkuId
 $SubLicenses = $User.Licenses[0].servicestatus

 Add-Content -value ($UserPrincipalName+","+$IsLicensed+","+$Licenses+","+$SubLicenses) -Path $ReportPath
}

问题是它只是从子许可证查询中拉出标题行而不是所有细节行。因此,我在CSV中的行看起来像:

Steve.Dorr@MerrillCorp.com TRUE mymerrillcorp:ENTERPRISEPACK Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft .Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus Microsoft.Online.Administration.ServiceStatus

这不能给我我需要的细节线。

如何将Licenses [0] .servicestatus生成的所有行拉入CSV文件?我不关心它是否会变平并越过更多列,或者在Excel中占用多行。

感谢。

所以,自从我发布这个问题以来,我已经做了一些工作。我没有一个完美的解决方案,把它放到一个漂亮的整洁的CSV文件中,但我确实有一个例程,现在将所有这些信息丢弃到一个文本文件中。以下是我的代码。

$MyCredentials = Get-Credential -Message "Enter Office 365 Email & Password"
Connect-MsolService -Credential $MyCredentials
$ReportFile = "C:\temp\O365Data.txt"
" " | Out-File $ReportFile  #erases the file if it exists

$AllUsers = Get-MsolUser -All 
foreach ($User in $AllUsers)
{
 $UserPrincipalName = $User.UserPrincipalName
 $IsLicensed = $User.IsLicensed
 $Licenses = $User.Licenses.AccountSkuId
 $SubLicenses = $User.Licenses[0].servicestatus
 $OneLine = $UserPrincipalName + " " + $IsLicensed
 $OneLine| Out-File $ReportFile -Append
 if($User.Licenses[0].servicestatus) {$User.Licenses[0].servicestatus | Out-File $ReportFile -Append}
}

1 个答案:

答案 0 :(得分:1)

要创建报告,您需要创建自定义对象以保存您感兴趣的属性。

以下内容将考虑可应用于用户的所有不同许可证,然后生成每行列出一个用户的csv。

# Connect to o365
$MyCredentials = Get-Credential -Message "Enter Office 365 Email & Password"
Connect-MsolService -Credential $MyCredentials

# Prepare result file
$ExportFile = ".\o365Output.csv"
Remove-Item $ExportFile
$Result = @()

# Query all Msol Users
$AllUsers = Get-MsolUser -All

foreach ($User in $AllUsers)
{
    # Generate a new object for each user
    $ReturnObject = [pscustomobject]@{
        UserPrincipalName = $User.UserPrincipalName
        IsLicensed = $User.IsLicensed 
        Licenses = [string]$User.Licenses.AccountSkuId
    }

    # In the event multiple licenses are found append properties for each license
    foreach ($License in $User.Licenses)
    {
        if($($License.ServiceStatus.ServicePlan.ServiceName).count -eq 1)
        {
            $ReturnObject | Add-Member -MemberType NoteProperty -Name $License.ServiceStatus.ServicePlan.ServiceName -Value $License.ServiceStatus.ProvisioningStatus
        }
        else
        {
            for($i = 0; $i -lt $($License.ServiceStatus.ServicePlan.ServiceName).count; $i++)
            {
                $ReturnObject | Add-Member -MemberType NoteProperty -Name $License.ServiceStatus.ServicePlan.ServiceName[$i] -Value $License.ServiceStatus.ProvisioningStatus[$i]
            }
        }
    }   
    $Result += $ReturnObject 
}

# Combine properties from all returned objects
$Properties = $Result | ForEach-Object { Get-Member -InputObject $_ -MemberType NoteProperty | Select-Object -ExpandProperty Name } | Select-Object -Unique | Sort-Object
$Headers = @("UserPrincipalName")
$Headers += $Properties -notlike "UserPrincipalName"

# Export to csv
$Result | Select-Object $Headers | Export-Csv -NoTypeInformation $ExportFile

# Open csv
Invoke-Item $ExportFile