CSV 的值 headers 为空,尽管数据在 CSV 文件 headers 中

Values of CSV headers being null, despite data being in the CSV file headers

我正在编写一个脚本,它将从公司的某些 DLS 中删除所有禁用的用户以及可能附加到用户帐户的许可证。

到目前为止,当我 运行 它时,它给我一个错误,指出脚本中的参数为空,但导出的 CSV 文件中有明确的 headers。脚本是:

Get-MsolUser -EnabledFilter DisabledOnly| select UserPrincipalName, isLicensed |Export-Csv -path "file\path\test" 

$Remove = Import-Csv "file\path\test"| Out-String 

Foreach($users in $Remove) {

    Remove-DistributionGroupMember -Identity CC@company.com -Member $users.UserPrincipalName -Confirm:$false

    Remove-MailboxPermission -Identity Sales -User $users.UserPrincipalName -AccessRights FullAcces -InheritanceType All -Confirm:$false

    Remove-DistributionGroupMember -Identity Everyone -Member $users.UserPrincipalName -Confirm:$false

        if($user.isLicensed -eq "true") {
        Set-MsolUserLicense -UserPrincipalName $users.UserPrincipalName -RemoveLicenses "reseller-account:DESKLESSPACK"

        Set-MsolUserLicense -UserPrincipalName $users.UserPrincipalName -RemoveLicenses "reseller-account:O365_BUSINESS_PREMIUM"

        Set-MsolUserLicense -UserPrincipalName $users.UserPrincipalName -RemoveLicenses "reseller-account:EXCHANGEDESKLESS"

        Set-MsolUserLicense -UserPrincipalName $users.UserPrincipalName -RemoveLicenses "reseller-account:O365_BUSINESS_ESSENTIALS"
        }
}

Remove-Item –path "file\path\test" –recurse

他们有两个 headers UserPrincipalNameisLicensed,但我得到的错误是 memberuser 都是空的。

请告诉我你的想法

当您使用 Import-Csv 时,它会将该信息转换为 pscustomobject,并以 headers 作为访问您信息的键。当您使用 | Out-String 时,它会将 object 转换为 string,其格式基于 ps1xml 文件中关于 pscustomobject 类型的内容。

CSV:

"a","b","c"
"1","2","3"

代码:

(Import-Csv -Path .\example.csv).GetType().FullName
# => System.Management.Automation.PSCustomObject

(Import-Csv -Path .\example.csv | Out-String).GetType().FullName
# => System.String

您 运行 遇到的错误是 string 类型没有您尝试访问的成员。