Get a list of workstations in Active Directory

There’s really not a lot to this script, but with what I was trying to do I thought this was actually kind of a cool trick.

What I was trying to do was a get a list of all computers in AD with desktop operating systems. Yes, this is part of the migration series we’ve been doing because as it turns out random people have random other desktops sitting under their desk. 🙂

In addition to just pulling the computer objects I also wanted a list of the OS installed and the IP address they had registered in DNS. As you know, AD does not store the IP by itself, so since I wanted this all saved to just one array it required a little bit of trickery.

The first thing we want to do is the actual get-qdcomputer from AD, which is fairly straightforward. Then we want to pipe that to a where clause and filter on the operating system we want. Then save that to an array

$b=get-qadcomputer -includedproperties operatingsystem,lastlogontimestamp|where-object {$_.operatingsystem -notlike '*Server*' -and $_.operatingsystem -notlike $Null -and $_.operatingsystem -notlike '*ontap*'}

This next line is where the actual “trick” comes in. I was tickled pink by the ease of doing this after all the issues I was having with adding IP to the results above. The key was to set a new array (or the same one) and just select the attributes I want. In this case, name, operatingsystem, and lastlogontimestamp. I wanted the timestamp so that I could see the last time the machine had booted on the network. Then the real key is I also told it to select the attribute ipaddress. This attribute doesn’t actually exist in the above array, but because I’m selecting it here, it actually creates that row in the new array.

$b=$b|select name,operatingsystem,lastlogontimestamp,ipaddress

Then in the next section I’m executing a flushdns at the OS level and creating a new array. The new array just keeps track of the machines that we can’t ping. For the purposes of this script it’s really not used. So then we go thru each item in $b, reset some variables, then do a wmi call out to DNS to do an nslookup on the computer name. If it gets a results it adds that IP address back into the original array. Then we just output the array.

& ipconfig /flushdns
$NoPing=@()

foreach ($item in $b){
	$c=$item.name
	$a=$null
	$a=[System.Net.Dns]::GetHostAddresses($c)
	if (!$a){$NoPing+=$c}
	else {
		$item.ipaddress=$a
	}
}

And here it is all together. You can do whatever you want with the results.

$erroractionpreference="silentlycontinue"
$b=get-qadcomputer -includedproperties operatingsystem,lastlogontimestamp|where-object {$_.operatingsystem -notlike '*Server*' -and $_.operatingsystem -notlike $Null -and $_.operatingsystem -notlike '*ontap*'}

$b=$b|select name,operatingsystem,lastlogontimestamp,ipaddress

& ipconfig /flushdns
$NoPing=@()

foreach ($item in $b){
	$c=$item.name
	$a=$null
	$a=[System.Net.Dns]::GetHostAddresses($c)
	if (!$a){$NoPing+=$c}
	else {
		$item.ipaddress=$a
	}
}

$b|ft name, operatingsystem,lastlogontimestamp,ipaddress

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s