无法在CSV中写入空字符串

时间:2016-10-24 04:18:42

标签: powershell csv active-directory

我想问一下是否有什么问题。如果查询在Active Directory上找不到任何用户,但我想在CSV中输出“null”,但它没有按预期工作。

示例输出:

Name       UserGiven   UserLastName
usvalj02adm Juhn Paul   Valencia
ustalu00    Juhn Paul   Valencia
usnaic00adm Juhn Paul   Valencia
usmenm03adm Marion Paul Mendoza
usjorv00ADM Vincent Jorge
usgols01ADM Vincent Jorge
usgedr00    Vincent Jorge
usdhaa00adm Vincent Jorge
uscrud00adm Don Eduard  Cruz
uscabg00adm Don Eduard  Cruz

它重复上一个查询并在未找到用户时输出。有人可以帮忙吗?

Import-Module ActiveDirectory

Get-Content -Path 'C:\Users.txt' | foreach {
    $User   = Get-ADUser $_
    $Groups = Get-ADPrincipalGroupMembership $User

    if ($User.givenName -eq $null) {
        $Empty = @{
            Name         = $_
            UserGiven    = "null"
            UserLastName = "null"
            #GroupDN      = "null"
        }
        New-Object PSObject -Property $Empty
    } else {
        $Props = @{
            Name         = $_
            UserGiven    = $User.givenName
            UserLastName = $User.surName
            #GroupDN      = "null"
        }
        New-Object PSObject -Property $Props
    }
    # One CSV line for each user/group 
} | Export-Csv -Path 'C:\UserGroups.csv' -NoTypeInfo

1 个答案:

答案 0 :(得分:1)

使用{null}的""''替代品,因为CSV中的字符串不为空,它是“”''