【问题标题】:Powershell exports csv file containing active directory security group dataPowershell 导出包含活动目录安全组数据的 csv 文件
【发布时间】:2016-01-04 22:33:18
【问题描述】:

我有一个启动 PS1 脚本的 bat 文件,PS1 脚本应该转到 AD 并导出安全组中列出的名称。它运行良好并且导出良好,但是数据不正确,几乎就像它正在导出一些我不确定从哪里来的未知数据。这是我的 powershell 脚本:

$uni = Read-host 'Your username'

$SG = Read-host 'Security Group'

start-sleep -s 3

powershell.exe get-adgroupmember "$SG" | export-csv -path "C:\users\$uni\desktop\members.csv"

打开CSV时,显示类似:

TYPE System.String

长度 79 79 63 17 34 79 26 54 1

我需要声明或导入一个 pssession 吗?如果是,活动目录的配置名称是什么?

【问题讨论】:

  • 删除powershell.exe并添加-NoTypeInformation开关:get-adgroupmember "$SG" | export-csv -path "C:\users\$uni\desktop\members.csv" -NoTypeInformation
  • 我尝试了你的建议,但是我在 cmd.exe 中收到一条消息,说明 'get-adgroupmember' 无法识别,所以本质上它是在尝试从 cmd.exe 而不是运行 powershell 命令电源外壳。当我一开始离开 powershell.exe 时,它​​停止了这个错误。
  • 您不是从 cmd.exe 获取的。您会遇到 PowerShell 异常 - 因为 Get-ADGroupmember 不是常用的 cmdlet。您需要先导入 ActiveDirectory 模块(并且可能安装 RSAT 才能做到这一点)。
  • 复制一下,我确实安装了 RSAT,并且安装了我的 AD 模块,但我完全理解您的意思。也许在 PS1 脚本中运行导入?我试过了,powershell.exe import-module ActiveDirectory | get-adgroupmember "$SG" yada yada... 但是得到同样的错误。有没有其他方法可以导入模块?我基本上需要在同一条线上。
  • 你听起来很困惑。首先,就像我说的,您不需要在 PowerShell 脚本中调用 powershell.exe - 删除它。其次,您不应该将输出从Import-Module 传送到Get-ADGroupMember。而是使它们成为单独的命令。或许this example 可以说明我的意思?

标签: powershell csv batch-file active-directory


【解决方案1】:

Alexander Obersht 通过 cmets 回答了我的问题,虽然duct_tape_coder 你也是正确的。谢谢大家的帮助,不胜感激。

【讨论】:

    【解决方案2】:

    您很可能会在数据中获得一组对象。在传递到 Export-CSV 之前,您必须展开或加入数据

    有关一些想法,请参阅以下文章:

    1) http://blog.millersystems.com/powershell-exporting-multi-valued-attributes-via-export-csv-cmdlet/ 2)http://www.msexchange.org/kbase/ExchangeServerTips/ExchangeServer2010/Powershell/WhydoIgetSystem.StringwhenusingGet-MessageTrackingLogandexportingtoaCSV.html

    文章给出了以下例子:

    1) 更改以下内容

    Get-TransportServer | Get-MessageTrackingLog -ResultSize -Start "11/28/2011" -Sender nuno@letsexchange.com | Select * | Export-Csv D:\Reports\Sent_Nuno.csv -NoType
    

    Get-TransportServer | Get-MessageTrackingLog -ResultSize -Start "11/28/2011" -Sender nuno@letsexchange.com | Select {$_.Recipients}, {$_.RecipientStatus}, * | Export-Csv D:\Reports\Sent_Nuno.csv -NoType
    

    2) 更改以下内容

    Get-QADUser seth -IncludeAllProperties | select name, proxyaddresses | Export-Csv .seth-nojoin.csv - See more at: http://blog.millersystems.com/powershell-exporting-multi-valued-attributes-via-export-csv-cmdlet/#sthash.JCccKLui.dpuf
    

    Get-QADUser seth -IncludeAllProperties | select name, @{Name=’proxyAddresses';Expression={[string]::join(“;”, ($_.proxyAddresses))}} | Export-Csv .seth-all_proxyaddresses.csv - See more at: http://blog.millersystems.com/powershell-exporting-multi-valued-attributes-via-export-csv-cmdlet/#sthash.JCccKLui.dpuf
    

    您需要为 get-ADGroupMember cmdlet 导入 ActiveDirectory 模块,但我假设您已经通过了该部分,因为您有输出而不是错误。我目前无法测试,因为我无法导入模块(需要安装 RSAT 或 AD 管理网关服务),但我认为您需要在管道中插入一个选择命令(以枚举有问题的数据字段) 看起来像这样:

    powershell.exe get-adgroupmember "$SG" | Select {$_.nameOfVariableReturningSystemString}, * | export-csv -path "C:\users\$uni\desktop\members.csv
    

    【讨论】:

    • 我试过了,我可能在选择命令中使用了错误的变量,变量会像 AD 中的名称或电子邮件吗?我只是尝试针对直接从 powershell 运行命令时列出的相同“#Type”(如果有意义的话)。我可以更深入,但我觉得这绝对是我需要去的方向。
    • 也是,广告模块已导入
    • 大概您的 CSV 有标题并且列中的数据显示为 System.String?如果是这样,它将是有问题的列标题中的名称(“名称”、“电子邮件”等)。还是您在谈论 CSV 中的第一行?除非您在使用 export-csv 时使用 -notypeinformation,否则您将得到一行包含您导出的变量的类型信息。
    猜你喜欢
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    相关资源
    最近更新 更多