【问题标题】:pipe cmdlet output into a variable and a csv file将 cmdlet 输出通过管道传输到变量和 csv 文件中
【发布时间】:2018-04-18 00:03:22
【问题描述】:

目前我正在我的 PowerShell 脚本中执行此操作:

$ServiceTagsPath=$filePath + '\DellAPIWarrantyLIST.csv'

write-host 'get all computer names from Active Directory...for Windows 7...'
Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
    Where-Object {$_.name -like "*-*"} |
    Where-Object {$_.name -NotLike "V7-*"} |
    Where-Object {$_.name -NotLike "*-NONE"} |
    Where-Object {$_.name -NotLike "*-ONCALL"} |
    Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
    Where-Object {$_.name -NotLike "SC-WIN7-1"} |
    Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} |
    Select-Object -property Name , LastLogonDate | export-csv $ServiceTagsPath -NoTypeInformation -Force 

$computers= Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
    Where-Object {$_.name -like "*-*"} |
    Where-Object {$_.name -NotLike "V7-*"} |
    Where-Object {$_.name -NotLike "*-NONE"} |
    Where-Object {$_.name -NotLike "*-ONCALL"} |
    Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
    Where-Object {$_.name -NotLike "SC-WIN7-1"} |
    Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} |
    Select-Object -Expand Name                           

Write-Host $computers.Length + ' computers found in Active Directory...'

第一个给我一个包含 2 列和大约 1500 条记录的 csv 文件,第二个给我一个数组变量,我在对 API 的 Web 服务调用中使用它...

但是有可能一步完成吗? 有没有办法同时完成这两项工作,以及拥有一个包含 2 列的 csv 文件,显示计算机名称和 LastLogondate,我将有一个仅包含计算机名称的数组?

【问题讨论】:

  • 为什么不只使用一个 Get-ADComputer 查询($computers,去掉 Select-Object 语句),然后用它做任何你需要做的事情。搜索 AD 2x 只是为了以不同的方式输出它似乎没有必要。

标签: powershell


【解决方案1】:

查询您想要的内容比检索所有内容并使用Where-Object 进行事后过滤要高效得多。你也不需要-Properties *。示例:

$outputFilename = Join-Path $filePath "DellAPIWarrantyLIST.csv"
Get-ADComputer -LDAPFilter "(&(operatingSystem=*Windows 7*)(name=*-*)(!name=*-none)(!name=*-oncall)(!name=*-blackbaud)(!name=sc-win7-1)(!name=ut-swclient-01))" -Property LastLogonDate |
  Select-Object Name,LastLogonDate |
  Export-Csv $outputFilename -NoTypeInformation
$outputCount = (Import-Csv $outputFilename | Measure-Object).Count
Write-Host ("Found {0} computer(s)" -f $outputCount)
if ( $outputCount -eq 0 ) {
  Remove-Item $outputFilename
}

【讨论】:

  • 这要快得多,先过滤而不是后过滤。
  • @BillStewart:感谢您,非常感谢您学习新知识!
  • @Bill_Stewart:修改LDAP Query返回所有操作系统Windows 7及以上是否容易?
【解决方案2】:

简单地将Get-ADComputer的结果all赋给一个变量(不使用Select-Object):

$computers = Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
    Where-Object {$_.name -like "*-*"} |
    Where-Object {$_.name -NotLike "V7-*"} |
    Where-Object {$_.name -NotLike "*-NONE"} |
    Where-Object {$_.name -NotLike "*-ONCALL"} |
    Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
    Where-Object {$_.name -NotLike "SC-WIN7-1"} |
    Where-Object {$_.name -NotLike "UT-SWCLIENT-01"}

然后将该变量用作两个命令的输入:

$ServiceTagsPath = "$filePath\DellAPIWarrantyLIST.csv"
$computers | Select-Object -Property Name,LastLogonDate | Export-Csv $ServiceTagsPath -NoTypeInformation -Force

$computer_names = $computers | Select-Object -ExpandProperty Name

【讨论】:

  • Where-Object 比在查询中过滤要慢。您也不需要-Properties *,因为Select-Object 只选择NameLastLogonDate 属性。
【解决方案3】:
Import-Module ActiveDirectory

$ServiceTagsPath=$filePath + '\DellAPIWarrantyLIST.csv'

Write-Host 'Getting all Windows 7 Computer Names from Active Directory. Please wait...'

$computers= Get-ADComputer -properties * -filter {(operatingsystem -like "*Windows 7*")} |
    Where-Object {$_.name -like "*-*"} |
    Where-Object {$_.name -NotLike "V7-*"} |
    Where-Object {$_.name -NotLike "*-NONE"} |
    Where-Object {$_.name -NotLike "*-ONCALL"} |
    Where-Object {$_.name -NotLike "*-BLACKBAUD"} |
    Where-Object {$_.name -NotLike "SC-WIN7-1"} |
    Where-Object {$_.name -NotLike "UT-SWCLIENT-01"}

#console output
$computers | select Name
#csv output
$computers | Select Name, LastlogonDate | Export-Csv $ServiceTagsPath -NoTypeInformation

Write-Host $computers.Length + ' computers found in AD...'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多