Skip to content

Instantly share code, notes, and snippets.

@sunnyc7
Created November 27, 2019 15:43
Show Gist options
  • Select an option

  • Save sunnyc7/b1a05ca198b23799ecd317b09ccd47ea to your computer and use it in GitHub Desktop.

Select an option

Save sunnyc7/b1a05ca198b23799ecd317b09ccd47ea to your computer and use it in GitHub Desktop.
This is a perennial issue when you are trying to run inventory based on AD, and not count all the Cluster objects. I hope this helps someone
Function Test-IfServernameIsCluster {
<#
.Synopsis
Test if an AD Object is a Cluster
.INPUT
Input Object requires PsComputerName, ServicePrincipalName AD Attributes
$test = get-qadcomputer VDICLU001 -IncludedProperties 'ServicePrincipalName' | select Name,ServicePrincipalName
$test = get-qadcomputer VDICLU001 -OSName "Windows Server*" -SizeLimit -1 -IncludedProperties 'ServicePrincipalName' | select Name,ServicePrincipalName
.EXAMPLE
$item = get-qadcomputer -OSName "Windows Server*" -SizeLimit -1 -IncludedProperties 'ServicePrincipalName' | select Name,ServicePrincipalName
Test-IfServernameIsCluster -computerObj $item
.OUTPUTS
Name IsCluster SPN
---- --------- ---
VDICLU001 CLUSTER {MSServerCluster/VDICLU001, MSServerClusterMgmtAPI/VDICLU001, MSClusterVirtualServer/VDICLU001, HOST/VDICLU001...}
#>
[OutputType([Bool])]
[cmdletbinding()]
param(
$computerObj
)
begin {
#ClusterSPN Test Checker
$cluSPNString = '^MSClusterVirtualServer'
}
process {
if ($computerObj.servicePrincipalName) {
$spnArray = $computerObj.servicePrincipalName -split ","
# Split SPN's and check each SPN for $cluSPNString. There maybe multiple matches
$clusterTest = foreach ($item in $spnArray) {
#Write-Host -ForegroundColor Cyan -Object $item
if ($item -match $cluSPNString) {
#$Matches[0]
$true
}
else {
$false
}
}
}
# CHCECK IF any one of the SPN's is True. This is tricky, as multiple SPN's can return true
if ($true -in $clusterTest) {
"CLUSTER"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment