目前我在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列的csv文件,大约1500条记录,第二条给我一个数组变量,我在API调用的web服务中使用...
但是可以一步完成这两项吗? 有没有办法在一个中完成这两个,以及有一个2列的csv文件,显示计算机名称和LastLogondate,我有一个只有计算机名称的数组?
答案 0 :(得分:4)
查询所需内容而不是检索所有内容并使用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
}
答案 1 :(得分:1)
只需将Get-ADComputer
的结果全部分配给变量(不使用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
答案 2 :(得分:1)
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...'