Powershell:Get-ADUser 的管道输出到 Set-MsolUserLicense

Powershell: Pipe output of Get-ADUser into Set-MsolUserLicense

我是 Powershell 的新手,我一直在尝试解决这个问题。我们正在迁移到 Office 365,这令人头疼,不一定来自 Microsoft 或我们的提供商,但我们有很多 'fun' 让这个对我们有利。

我有一个用户的 CSV,我必须从 one-product 和 re-license 中删除他们在 O365 中的许可证以获得另一个产品。我正在尝试以两个 'one-liners' 的形式执行此操作,但我迷路了。这是我目前所拥有的:

Import-Csv "C:\Users\myuser\Desktop\convert.csv" | ForEach-Object {Get-ADUser -Identity $_.user | select UserPrincipalName | Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "mycorp:ENTERPRISEDESKLESS"}

CSV 有一个 header 的 'user' 和一些用户记录作为他们的帐户名。如果我检查命令直到 Select 函数的输出,一切都正常工作,这是该输出到 Set-MsolUserLicense 的管道不起作用。

我现在没有办法测试它,但它应该像这样工作

Import-Csv "C:\Users\myuser\Desktop\convert.csv" | ForEach-Object {Get-ADUser -Identity $_.user | Set-MsolUserLicense -RemoveLicenses "mycorp:ENTERPRISEDESKLESS"}

根据 Set-MsolUserLicense 的帮助信息

-UserPrincipalName <string>
    The user ID of the user to update.

    Required?                    true
    Position?                    named
    Default value                
    Accept pipeline input?       true (ByPropertyName)
    Accept wildcard characters?  false

参数 UserPrincipalName 正在接受来自管道和 ByPropertyName 的输入,因此当您将其通过管道传递给 Set-MsolUserLicense 时,由于 Get-ADUser 将为您提供该名称的 属性,因此它应该能够自动使用正确的值。