In one of my posts I described dealing with obsolete computer records, now it’s time to get a hold of the ones which are unpingable.
This was actually inspired by a real request in PowerGUI forums but I thought I would duplicate the answer here as well. What Ben requested in the forums was a PowerShell one-liner to bulk-move the inaccessible computers to a specific OU.
As you might guess what we need is a pipeline from Get-QADComputer to Move-QADObject with some kind of where -not pingable in between. Luckily thanks to our fellow MVP Brandon we have the script doing the ping.
So the one-liner is really very simple:
Get-QADComputer | where { -not (Test-Port($_.Name)) } | Move-QADObject -NewParentContainer ps64.local/Recycled
Add -WhatIf if you don’t want to perform the actual move. Add -Confirm if you want to be prompted for each computer record.
You can get the Test-Port from Brandon’s post over here. Copy/paste it into your PowerShell command-line before running the oneliner.
Tags: AD, AD cmdlets, Active Directory, Blogosphere, Examples, PowerShell, one-liner, oneliner
Subscribe by email




While I appreciate the trackback, I should warn that Test-Port isn’t really a ping. It just makes a TCP Socket connection to a given port (135 RPC by default.) If you want a ping I use the function below for that. It is an actual ICMP ping. Test-Port is great if your company blocks pings or you want to specify a timeout, also the port is configurable.
function Ping-Server {
Param([string]$server)
$pingresult = Get-WmiObject win32_pingstatus -f “address=’$Server’”
if($pingresult.statuscode -eq 0) {$true} else {$false}
}
You may wnat to check out my Select-Alive function. It acts like a filter, making the where-object part unnecessary. Your example turns into this:
Get-QADComputer | select-alive | Move-QADObject -NewParentContainer ps64.local/Recycled
http://halr9000.com/article/447
Hmm, my script might need slight modifications in order to work with $_.name. It expects $_ to be a parseable string. Seems like Guarhoth (http://thepowershellguy.com/blogs/gaurhoth/) dealt with a similar situation by checking the object type in a switch statement and pulling out the right property names as needed. I’ll have to do that.