What site a computer belongs to...
-
Friday, January 25, 2013 7:56 PM
I need to create a csv file listing which site a remote computer belongs to (approx 2500 PC's). I know how to get a list of computers using get-adcomputer, and I found this that when run from my local pc to get its site -
[System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().name (not sure how this works).I cannot figure how to run my list of computers against the above command.
All Replies
-
Friday, January 25, 2013 8:04 PM
It's not super straight forward. Shay Levy has posted on this before. See here: http://www.powershellcommunity.org/Forums/tabid/54/aft/4505/Default.aspx
Hope that helps.
G. Samuel Hays
-
Friday, January 25, 2013 8:12 PMThats where I came across the above, but can't figure out how to run a list against it.
-
Friday, January 25, 2013 8:14 PM
That command is calling a .Net Framework static method locally and cannot be run on another computer unless you have a ps-session open. He gives the following function as an alternative:
function Get-ADComputerSite($ComputerName)
{
$site = nltest /server:$ComputerName /dsgetsite
if($LASTEXITCODE -eq 0){ $site[0] }
}That's probably want you'll want to use.
G. Samuel Hays
-
Friday, January 25, 2013 10:01 PM
You can also incorporate a WMI query. Simple example:
Get-WmiObject -Namespace root\rsop\computer -Class RSOP_Session | select site
Mike Crowley | MVP
My Blog -- Planet Technologies

-
Friday, January 25, 2013 10:55 PM
Get-WmiObject -Namespace root\rsop\computer -Class RSOP_Session | select site
The above is very powerful. Just combine with Get-ADComputer and it will do the job:
$computers = Get-ADComputer -filter * -SearchBase 'OU=MyOU,DC=MyDomain,DC=COM' -ResultSetSize $null | Select -ExpandProperty 'name' Get-WmiObject -Namespace root\rsop\computer -Class RSOP_Session -ComputerName $computers | Select __Server, Site
It may be slow when it runs into offline or dead computers.
-
Monday, January 28, 2013 7:39 PM
I wanted to see if I could get away from using WMI, I have a script that runs against Win32_NTDomain, but I have a a handful of machines that fail on accessing WMI (approx 50 out of 2500).
If I can't avoid using WMI, I will have to capture the error and record the computer names (I also test the PC with Test-Connection, and record the name of the computer if the PING fails)
$colItems = get-wmiobject -class "Win32_NTDomain" -namespace "root\CIMV2" -computername $computer -EA SilentlyContinue
This is my script if interested or have tips, I am freshly into powershell and am using examples and lots of reading.
#Create Log File $date = (get-date).ToString('MMddyyy') $file = New-Item -type file "$date.csv" -Force $Computers = get-adcomputer -SearchBase "OU=OUName,DC=Domain,DC=Domain,DC=Domain,DC=Domain" -Filter 'OperatingSystem -NotLike "*server*"' | %{$_.name} Write-Host $Computers foreach ($computer in $computers) { if(test-connection $computer -count 1 -quiet) { Write-Host "$computer UP" -foregroundColor GREEN $colItems = get-wmiobject -class "Win32_NTDomain" -namespace "root\CIMV2" -computername $computer -EA SilentlyContinue $Networks = get-wmiobject -class "win32_networkadapterconfiguration" -namespace "root\cimv2"-computername $computer | ? {$_.IPEnabled} -EA SilentlyContinue ForEach ($nobjItem in $Networks) { $ipaddress = $nobjItem.ipaddress[0] Write-Host $ipaddress } ForEach ($objItem in $colItems) { write-host $computer $objItem.ClientSiteName add-content $file -Value "$computer,$ipaddress,$($objItem.ClientSiteName)" -encoding ascii } } else { Write-Host "$computer DOWN" -foregroundColor RED add-content $file "$computer, DOWN or Not Reachable" } }
-
Monday, January 28, 2013 8:29 PM
Here's an idea, it may probably save you some time running WMI on every single machine.
The key is, all AD subnets are defined and associated with some AD Site.
So to get all the subnets and their sites:
Get-ADObject -Filter:{objectClass -eq 'subnet'} -SearchBase:'CN=Subnet,CN=Sites,CN=Configuration,DC=MyDomain,DC=COM' -Properties SiteObject | Select Name, @{Label='Site';Expression=@{(Get-ADObject $_.SiteObject).Name}}
You'll then get something like this:
Name Site
10.123.234.0/24 Default-First-Site-Name
What you can do is then ping all the computers in your list and compare their IPs against the IPRange (Name).
Of course, ping queries DNS so the accuracy will depend on your scavanging policy, but it sort of evades the potential WMI problem on some machines.
Also your "Test-Connection $computer -Count 1" may miss some computers that are sleeping. I would leave it to 3 or 4 counts just to be sure.- Edited by AverageJoeOfToronto Monday, January 28, 2013 8:54 PM
- Marked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Monday, February 04, 2013 2:18 AM

