【问题标题】:Adding a ROW for missing Attribute values to Export-CSV将缺失属性值的 ROW 添加到 Export-CSV
【发布时间】:2020-07-01 11:34:24
【问题描述】:

我使用以下 POWER SHELL 脚本,从“Manager”用户属性中提取(到 csv)经理姓名。

#This script, , Exports the Manager name of the employee`s in the TXT file.
# users.txt file - contains a simply list of user names ( samaccount-names )

Get-Content D:\powershell\permmisions\Users.txt | Foreach-Object {

Get-ADUser -Identity $_ -Properties Manager | Select-Object name, Manager | Export-Csv D:\Powershell\ADuserinformation\Export-Managers-of-specific-users.csv
-Append

}

我面临的挑战是导出 CSV 文件的时间, 列表“SKIPS”空白值字段,以防没有为用户设置管理器。 并且没有创建 ROWS ,其中缺少 MANAGER。

我想做的是输入字符(〜)的脚本,例如,其中值为空白。 这样,将在 CSV 文件中为空白 MANAGER 值创建一行

请帮忙, 提前谢谢大家。

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    注意:至少Name 属性应该存在于检索到的所有 AD 用户中,因此即使Manager 为空但为空的用户,您得到一行Manager 专栏。如果您确实需要处理可能并非所有在Users.txt 中命名的用户实际上都存在,请参阅Theo's helpful answer

    最简单的方法是使用calculated property

    Get-ADUser -Identity $_ -Properties Manager | 
      Select-Object Name,  @{ Name='Manager'; 
                              Expression={ if ($_.Manager) { $_.Manager } else { '~' } } }
    

    注意:

    • 定义计算属性的哈希表的键名通常缩写为ne

    • if 语句利用了 空字符串(或 $null)在布尔上下文中计算为 $false 的事实;有关 PowerShell 的隐式到布尔转换的概述,请参阅this answer 的底部。


    PowerShell [Core] 7.0 或更高版本中,您还可以利用ternary operator (<condition> ? <valueIfTrue> : <valueIfFalse>) 进一步缩短命令:

    # PSv7+
    Get-ADUser -Identity $_ -Properties Manager | 
      Select-Object Name,  @{ n='Manager'; e={ $_.Manager ? $_.Manager : '~' } }
    

    注意:如果 $_.Manager 返回 $null 而不是 空字符串 (''),如果没有分配经理,您可以使用 ??,PSv7+ null-coalescing operator 改为:$_.Manager ?? '~'

    【讨论】:

    • 嗨,这似乎有效,如果属性确实为空,但我认为我有一个问题,经理在哪里但被禁用,可以覆盖吗?你也能告诉我应该把 Export-CSV 放在哪里,我想自己把它放在哪里。 ?
    • @sj1986:重新禁用经理:请提出问题(完成后请随时联系我)。回复Export-Csv:这里很难诊断(而且它不是这样做的正确位置),但请确保输出文件的目标目录已经存在,并且您具有在那里创建文件的正确权限。
    • 谢谢,知道如何添加一行,如果其中一个用户,我已插入原始 USERS.TXT 文件不存在?而不是 powersheel 跳到下一行?
    • @sj1986 您可以按如下方式调整 Theo 的答案:在最后一个 else 分支中,添加类似 [pscustomobject] @{ Name = $null; Manager = $null } 的内容 - 这将输出两列都为空的行。
    【解决方案2】:

    一点也不简洁,但这允许您在报告中插入更多感兴趣的属性,并在输入文件中列出的用户不存在时进行一些错误检查:

    $report = foreach ($account in (Get-Content D:\powershell\permmisions\Users.txt)) {
        $user = Get-ADUser -Filter "SamAccountName -eq '$account'" -Properties Manager -ErrorAction SilentlyContinue
        if ($user) {
            if (!$user.Manager) { $mgr = '~' }
            else { 
                # the Manager property is the DistinghuishedName for the manager.
                # if you want that in your report, just do 
                $mgr = $user.Manager
                # if you want the Name for instance of that manager in your report,
                # comment out the above line and do this instead:
                # $mgr = (Get-ADUser -Identity $user.Manager).Name
            }
            # now output an object
            [PsCustomObject]@{
                UserName = $user.Name
                Manager  = $mgr
            }
        }
        else {
            Write-Warning "User '$account' does not exist"
        }
    
    }
    
    # output on screen
    $report | Format-Table -AutoSize
    
    # output to CSV file
    $report | Export-Csv -Path 'D:\Powershell\ADuserinformation\Export-Managers-of-specific-users.csv' -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 2018-05-12
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      相关资源
      最近更新 更多