0

Currently I am doing this in my PowerShell script:

$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...'

The first one gives me a csv file with 2 columns, and about 1500 records, the second one gives me an array variable which I use in web-service calls to an API...

But would it be possible to do both in one step? is there a way to do both of these in one so as well as having a csv file with 2 columns, showing the Computer Name and LastLogondate, I'd have an array with just the computer names?

1
  • 1
    Why not just use one Get-ADComputer query ($computers, take off the Select-Object statement) and then do whatever you need to with that. Searching AD 2x just to output it differently seems unnecessary. Commented Apr 17, 2018 at 15:10

3 Answers 3

4

It is much more efficient to query for what you want instead of retrieving everything and filtering after-the-fact with Where-Object. You also don't need -Properties *. Example:

$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
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is much faster, filter prior instead of after.
@BillStewart: Thanks for this, much appreciated to learn something new!
@Bill_Stewart: Is it easy to modify the LDAP Query to return all operating Systems Windows 7 and above?
1

Simply assign all the results of Get-ADComputer to a variable (without using 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"}

Then use that variable as the input for your two commands:

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

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

1 Comment

Where-Object is slower than filtering in the query. You also don't need -Properties * since Select-Object is only selecting the Name and LastLogonDate properties.
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...'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.