
close
close
Want to know about the security benefits of Microsoft's E5 license?
In an earlier article I demonstrated some techniques for creating and using lists of computer names. I hope you found that article helpful. Today, I want to show you another way to handle property name problems, as well as other ways for building lists dynamically from Active Directory or DNS.
In the last article I explained the importance of aligning the property name from any imported list, such as a CSV file, with the parameter name of cmdlets that you intend to use. For most cmdlets, this means ensuring that any object has a property name of Computername. But suppose you have a CSV file that uses something else? I have an alternate version of the CSV file I used last time that does just that.
$computers = Import-CSV C:\scripts\computers-alternate.csv
$computers[0..3]
Importing a list with an incompatible property name (Image Credit: Jeff Hicks)
$computers[0..3] |
Select @{Name="Computername";Expression={$_.Server}} |
Get-Service bits |
Format-Table Machinename,Name,Status -AutoSize
Using a custom property to rename (Image Credit: Jeff Hicks)
$computers = Get-Content C:\scripts\computers-alternate.csv | Select-Object -Skip 1 | ConvertFrom-Csv -Header "Computername","IP","Notes"
Instead of directly importing the CSV file, I am getting the file contents using Get-Content. I am then skipping the first line, which in my file is the header line. You have to know in advance what your file looks like. This leaves me with a bunch of CSV lines without a header, which I can now add back with ConvertFrom-Csv. But now I have a collection of computer objects with the right property name.
Converted CSV data (Image Credit: Jeff Hicks)
Another option and one I see come up quite often in forums, is building a list from Active Directory. The easiest way is to use the Microsoft Active Directory module, which you can get when you install the latest version of Remote Server Administration Tools (RSAT) on your desktop. I don’t want this to become a tutorial on using Get-ADComputer so take a few minutes to read the help and examples, especially if you want to limit your search to a particular OU. What is important for us is what type of object we get.
Sample AD Computer object (Image Credit: Jeff Hicks)
Get-ADComputer -filter "name -like 'CHI-*'" -ResultSetSize 3 |
Add-Member -MemberType AliasProperty -Name Computername -Value Name -force -PassThru
I’m only selecting a small number of computer accounts for the sake of my demonstration. Be sure to include the –Passthru parameter, otherwise PowerShell won’t write anything to the pipeline.
Inserting an alias property with Add-Member (Image Credit: Jeff Hicks)
$computers = Get-ADComputer -filter "name -like 'CHI-*'" -ResultSetSize 5 | Select @{Name="Computername";Expression={$_.name}}
Renamed single property list (Image Credit: Jeff Hicks)
$computers = (Get-ADComputer -filter "name -like 'CHI-*'" -ResultSetSize 5).name
Creating a simple list from Active Directory (Image Credit: Jeff Hicks)
The last option, again assuming you have RSAT installed is to use the DNSServer module and Get-DnsServerResourceRecord. It is always helpful to see the results first.
Get-DnsServerResourceRecord -ComputerName chi-dc04 -ZoneName globomantics.local -RRType A
Using Get-DnsServerResourceRecord (Image Credit: Jeff Hicks)
(Get-DnsServerResourceRecord -ComputerName chi-dc04 -ZoneName globomantics.local -RRType A).Where({$_.hostname -match "^chi-"})
Filtering DNS records with the Where() method (Image Credit: Jeff Hicks)
$computers = (Get-DnsServerResourceRecord -ComputerName chi-dc04 -ZoneName globomantics.local -RRType A).Where({$_.hostname -match "^chi-"}) |
Select-Object @{Name="Computername";Expression={$_.Hostname}}
Creating a DNS list with a renamed property (Image Credit: Jeff Hicks)
$computers = (Get-DnsServerResourceRecord -ComputerName chi-dc04 -ZoneName globomantics.local -RRType A).Where({$_.hostname -match "^chi-"}).Hostname
Creating a DNS name list (Image Credit: Jeff Hicks)
More in PowerShell
Most popular on petri