Select-Object filtering

Select-Object simplified

Just a quickie note.

I needed to get some information about servers’ computer objects from AD. Simple query with some properties:

Get-ADComputer Filter * SearchBase 'OU=Servers,OU=Computers,DC=contoso,DC=com' properties 'operatingSystem','whenCreated','whenChanged'

What started simple soon became more… complicated:

Get-ADComputer Filter * SearchBase 'OU=Servers,OU=Computers,DC=contoso,DC=com' Properties 'DNSHostName','lastLogon','lastlogontimestamp','operatingSystem','operatingSystemVersion','whenChanged','whenCreated' | Select-Object 'DNSHostName',
@{name='lastLogon';e={[datetime]::FromFileTime($PSItem.'lastLogon') }},
@{name='lastLogontimestamp';e={[datetime]::FromFileTime($PSItem.'lastLogontimestamp') }},
'operatingSystem','operatingSystemVersion','whenCreated','whenChanged','DistinguishedName',
@{name='InUse';e={''}},
@{name='CurrentAccessList';e={''}},
@{name='Applications';e={''}},
@{name='SystemUpdate';e={''}},
@{name='PM';e={''}},
@{name='TO';e={''}}

Somewhere along the way I had to query this from multiple domains using different credentials and finally export it into csv.

Every time I was adding new property or select-object property I had to do it in multiple places. Remember this started as an ad-hoc script, not polished function.

Eventually I moved all properties and select-object into variables. Thing to remember – select-object requires separate elements, not single string.

Now, this is a way better to read through:

$AdComputerFilter = 'DNSHostName','lastLogon','lastlogontimestamp','operatingSystem','operatingSystemVersion','whenChanged','whenCreated'
$SelectObjectFilter = @(
'DNSHostName',
@{name='lastLogon';e={[datetime]::FromFileTime($PSItem.'lastLogon') }},
@{name='lastLogontimestamp';e={[datetime]::FromFileTime($PSItem.'lastLogontimestamp') }},
'operatingSystem','operatingSystemVersion','whenCreated','whenChanged','DistinguishedName',
@{name='InUse';e={''}},
@{name='CurrentAccessList';e={''}},
@{name='Applications';e={''}},
@{name='SystemUpdate';e={''}},
@{name='PM';e={''}},
@{name='TO';e={''}}
)
Get-ADComputer Filter * SearchBase 'OU=Servers,OU=Computers – PL,DC=contoso,DC=com' properties $AdComputerFilter | Select-Object $SelectObjectFilter

Final touch

Also moved all domains into an array:

$AdComputerFilter = 'DNSHostName','lastLogon','lastlogontimestamp','operatingSystem','operatingSystemVersion','whenChanged','whenCreated'
$SelectObjectFilter = @(
'DNSHostName',
@{name='lastLogon';e={[datetime]::FromFileTime($PSItem.'lastLogon') }},
@{name='lastLogontimestamp';e={[datetime]::FromFileTime($PSItem.'lastLogontimestamp') }},
'operatingSystem','operatingSystemVersion','whenCreated','whenChanged','DistinguishedName',
@{name='InUse';e={''}},
@{name='CurrentAccessList';e={''}},
@{name='Applications';e={''}},
@{name='SystemUpdate';e={''}},
@{name='PM';e={''}},
@{name='TO';e={''}}
)
$domains = @(
@{
Name = 'contoso.com'
Properties =@{
Server = 'pdc1.contoso.com'
Credential = Get-Credential
SearchBase ='OU=Servers,OU=Computers,DC=contoso,DC=com'
Filter ='*'
}
},
@{
Name = 'farbikam.com'
Properties =@{
Server = 'pdc1.fabrikam.com'
Credential = Get-Credential
SearchBase ='OU=Servers,OU=Computers,DC=fabrikam,DC=com'
Filter ='*'
}
}
)
$AllComputers = $domains | ForEach-Object {
$domainProperties = $PSItem.Properties
Get-ADComputer @domainProperties Properties $AdComputerFilter | Select-Object $SelectObjectFilter
}
$FolderPath = 'D:\AdminTools\ServersInAD'
New-Item ItemType directory Path $FolderPath force
$Allcomputers| Export-Csv Path (Join-Path $FolderPath ChildPath Allcomputers.csv) Delimiter ',' NoTypeInformation

One thought on “Select-Object filtering

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s