Computer Object and Loop Question


hello all.

i looking retrieve computer objects ad, select each objects name , dnshostname attributes , compare them. if name attribute of computer object not same dnshostname attribute, i'd send both of attributes(or if 1 missing, whichever attribute available), text file. understand since dnshostname have fqdn displayed opposed name of machine, i'm show should deem each computer name dnshostname unequal , therefore throw in text file; however, not case , don't know why. output seems name , dnshostname followed "does exist!" , not sure why, considering how set variables!

$cpunames = get-adcomputer -filter * | where{$_.name -and $_.dnshostname} | select -expand name
$dnsnames = get-adcomputer -filter * | where{$_.name -and $_.dnshostname} | select -expand dnshostname

    for($i=0; $i -lt $cpunames.count; $i++) {
        
       $cpunames[$i]
       $dnsnames[$i]
       
           if($cpuname -match $dnsname) {write-host "$cpuname exist!" -foregroundcolor cyan}
                else {"$cpuname | $dnsname" | out-file c:\computernames.txt -append}
}

the about_split gave me solution split strings into arrays based on delimiter ".". following snippet documents whether each computer has name matches first part of dnshostname:

$computers = get-adcomputer -filter * | select dnshostname, name
foreach ($computer in $computers)
{
    $dnsname = ($computer.dnshostname -split {$_ -eq "."})[0]
    $name = $computer.name
    if ($dnsname -eq $name) {"ok: $name"}
    else {"not ok: $name"}
}

-----

the next snippet documents if name matches value of samaccountname atttribute (the netbios name of computer, called "pre-windows 2000" name) without trailing "$" character:

$computers = get-adcomputer -filter * | select name, samaccountname
foreach ($computer in $computers)
{
    $name = $computer.name
    $netbiosname = $name + "$"
    if ($netbiosname -eq $computer.samaccountname) {"ok: $name"}
    else {"not ok: $name"}
}

-----

remember, name property value of cn attribute (the common name of object) , not uniquely identify object in ad.


richard mueller - mvp directory services



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

WIMMount (HSM) causing cluster storage to go redirected (2012r2 DC)

Failed to delete the test record dcdiag-test-record in zone test.com

Azure MFA with Azure AD and RDS