Get-ComputersNearMe
Today I learned a little bit about how to get to some of the data stored within special shell folders. You can get a list of all of the easy-to-get-to special folders here. I noticed one folder in particular that a bunch of systems administrators I know want to read, and that’s the Nearby Computers page. Here’s a quick 23-line function that will go and get all of the computers from the Nearby Computers page, ping them, and return the Win32_PingStatus objects.
function Get-ComputersNearMe
{
<#
.Synopsis
Returns the Computers Near Me
.Description
Returns all of the computers listed in the "Computers Near Me" or "Access the computers and devices tht are on your network" shell
namespace. Uses the Shell.Application API to get the computers folder, and then uses Test-Connection to return the machines and their
response times.
.Example
Get-ComputersNearMe
#>
param()
$computersFolder = 0x12
$shell = New-Object -ComObject Shell.Application
$shell.NameSpace($computersFolder).Items() |
Where-Object { $_.Path -like "\\*" } |
ForEach-Object {
Test-Connection $_.Name -AsJob
} |
Wait-Job |
Receive-Job
}
As you can see, the whole first half of the script is the help. $computersFolder is set to one of the magic constants listed on the MSDN page, and then I simply use the COM Object Shell.Application (which should be familiar to anyone that’s done a decent amount of VBScript) to get the folder. Two types of items will be returned from this: UNC paths to a machine and links to specific devices (like a media player library). I filter out only the UNC paths with Where-Object, and then I do Test-Connection –AsJob to try to ping the machines. The last bit of the script waits for all of the pings to come back, and returns the results to the user. Since Win32_PingStatus already has a formatter that displays the source, destination, IPV4 address, IPV6 address, and time to ping, so there’s really nothing else I want to add.
Get-ComputersNearMe should be easy enough to use in small businesses, workgroups, or wherever else you find yourself using that screen.
Hope this helps,
James