Skip to content

Instantly share code, notes, and snippets.

@danjpadgett
Created February 15, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danjpadgett/53d5178ef2530eb1075f967301ac695a to your computer and use it in GitHub Desktop.
Save danjpadgett/53d5178ef2530eb1075f967301ac695a to your computer and use it in GitHub Desktop.
Get-CmSiteCode
$result = @()
$time = 1
$searchbase = 'OU=WORKSTATIONS,DC=COMPANY,DC=com'
$list = (Get-ADComputer -SearchBase $searchbase -Filter 'Enabled -eq $true').name
function Get-ComputerSite($i)
{
$site = nltest /server:$i /dsgetsite 2>$null
if($LASTEXITCODE -eq 0){ $site[0] }
}
Foreach ($i in $list)
{
$resData = New-Object System.Object
$resData | Add-Member -type NoteProperty -name ComputerName -value $i
if (Test-Connection -ComputerName $i -Quiet -Count 1)
{
try {
$resData | Add-Member -type NoteProperty -name SiteCode -value ((Invoke-WMIMethod -ComputerName $i –Namespace root\ccm –Class SMS_Client –Name GetAssignedSite -ErrorAction Stop).sSiteCode)
$resData | Add-Member -type NoteProperty -name ADSite -value (Get-ComputerSite $i) }
catch {$resData | Add-Member -type NoteProperty -name SiteCode -value ("Connection Failed : $($error[0].Exception.Message)") -Force}
}
else {$resData | Add-Member -type NoteProperty -name SiteCode -value ('Connection Failed : Ping Failed')}
$result += $resData
Write-Progress -activity "Checking Site Codes for $($list.length) clients" -status "Percent checked: " -PercentComplete (($time / $list.length) * 100)
$time++
}
$result | sort SiteCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment