[英]PowerShell to Get Disabled AD Users Still Licensed in O365
I'm trying to run a report, to get all the users who are disabled in AD, but still have a license assigned in Office 365. I've found a couple of scripts on various sites, and they work if just run within the PowerShell console, but the moment I try to export to a CSV, it loses the license assignment information. 我正在尝试运行报告,以获取在AD中被禁用但在Office 365中仍分配有许可证的所有用户。我在各个站点上找到了两个脚本,并且如果它们仅在PowerShell控制台,但是当我尝试导出为CSV时,它会丢失许可证分配信息。
The script I'm currently using is: 我当前使用的脚本是:
Get-MsolUser -All | where {$_.isLicensed -eq $true -and $_.BlockCredential -eq $true} | select userprincipalname,islicensed,Licenses,UsageLocation
This works, and shows the below 这有效,并显示以下内容
UserPrincipalName IsLicensed Licenses UsageLocation
UserPrincipalName IsLicensed许可证的使用位置
----------------- ---------- -------- -------------
----------------- ---------- -------- -------------
joe.bloggs@domain.com True {tennent:ENTERPRISEPACK} US
joe.bloggs@domain.com True {tennent:ENTERPRISEPACK}美国
However, the moment I add: 但是,我添加的那一刻:
| Export-Csv -Path C:\LicenseReport.csv
The report changes to: 该报告将更改为:
UserPrincipalName IsLicensed Licenses UsageLocation
UserPrincipalName IsLicensed许可证的使用位置
joe.bloggs@domain.com TRUE System.Collections.Generic.List`1[Microsoft.Online.Administration.UserLicense] US
joe.bloggs@domain.com是System.Collections.Generic.List`1 [Microsoft.Online.Administration.UserLicense]美国
I've tried a number of other select properties for the license, such as 我已经为许可证尝试了许多其他选择属性,例如
But none work. 但是没有任何作用。 How do I get the report to export with the License details?
如何获取带有许可证详细信息的报告?
Try this: 尝试这个:
Get-MsolUser -All | where {$_.isLicensed -eq $true -and $_.BlockCredential -eq $true} |
select userprincipalname,islicensed,@{N="Licenses";E={$_.Licenses.AccountSkuId}},UsageLocation
and that's in case you have only one license, but if you have more, you need to join them with commas so it will be a string compatible for the csv export, for example: 如果您只有一个许可证,但是如果有更多许可证,则需要用逗号将它们加入,这样它将是与csv导出兼容的字符串,例如:
Change this: 更改此:
@{N="Licenses";E={$_.Licenses.AccountSkuId}}
To This: 为此:
@{N="Licenses";E={$_.Licenses.AccountSkuId -join ','}}
This is the command I use & it works: 这是我使用的命令,它有效:
Get-MsolUser -All | ?{$_.isLicensed-eq "TRUE"} | Select DisplayName, SignInName, @{n="LicensesType";e={$_.Licenses.AccountSKUid}} | Export-Csv -Path C:\output.csv -NoTypeInformation
Be aware though, the MSOnline module is no longer developed. 但是请注意,不再开发MSOnline模块。 You should consider moving to the AzureAD PowerShell module .
您应该考虑移至AzureAD PowerShell模块 。 Here is the syntax for that:
这是它的语法:
Get-AzureADUser -All 1 | ?{($_.AssignedLicenses | ?{$_.SkuId -eq $license.SkuId})} | SELECT DisplayName, UserPrincipalName, @{l="License";e={$license.SkuPartNumber}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.