【问题标题】:Filter Object Data before Export-CSV导出前过滤对象数据-CSV
【发布时间】:2019-02-15 00:14:43
【问题描述】:

我正在编写一个 powershell 脚本,该脚本使用我们的 AD 验证 CSV,找到当前禁用的帐户列表,然后将它们导出到另一个 CSV。我目前能够读取 CSV、比较和导出所有帐户的列表,其中包含有关其状态的真/假标志。我还可以使用另一个脚本过滤该输出 CSV。理想情况下,我想将这些合并到一个只有一个输出 CSV 的脚本中。我只是不知道该怎么做。

我尝试将脚本 2 的“where...”部分移动到脚本 1 中,但没有成功。

脚本 1:

Import-Csv C:\ServerScripts\Compare\students.csv | ForEach-Object {
New-Object -TypeName PSCustomObject -Property @{
    samaccountname = $_.samaccountname
    firstname = $_.firstname
    lastname = $_.lastname
    name = $_.name
    password = $_.password
    email = $_.email
    description = $_.description
    CAMPUS_ID = $_.CAMPUS_ID
    exist = [bool]($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone())
    disabled = [bool]($account.properties.useraccountcontrol[0] -band 2)
}
} | Export-Csv C:\ServerScripts\Compare\output.csv -NoTypeInformation

脚本 2:

Import-Csv C:\ServerScripts\Compare\output.csv | where {$_.disabled -ne "FALSE"} | Export-Csv C:\ServerScripts\Compare\disabled.csv -notypeinfo

两个脚本独立工作,我只需要一种方法将它们组合在一起。

【问题讨论】:

  • 问题先生,您只需要一个包含禁用用户的输出文件吗?
  • 是的,这就是目标。

标签: powershell


【解决方案1】:

在调用Export-Csv之前将您的检查添加到第一个管道中:

Import-Csv C:\ServerScripts\Compare\students.csv | ForEach-Object {
New-Object -TypeName PSCustomObject -Property @{
    samaccountname = $_.samaccountname
    firstname = $_.firstname
    lastname = $_.lastname
    name = $_.name
    password = $_.password
    email = $_.email
    description = $_.description
    CAMPUS_ID = $_.CAMPUS_ID
    exist = [bool]($account=([adsisearcher]"(samaccountname=$($_.samaccountname))").findone())
    disabled = [bool]($account.properties.useraccountcontrol[0] -band 2)
}
} |Where-Object {$_.disabled -eq $true} | Export-Csv C:\ServerScripts\Compare\output.csv -NoTypeInformation

【讨论】:

  • 完美,我尝试过类似的方法,但我认为我的语法不正确。感谢您的帮助!
猜你喜欢
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 2015-11-13
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多