Share via


powershell if/else statement wiith hostname matching in AD

Question

Friday, September 6, 2013 10:29 PM

Hello,

I am having hard time matching the hostname in AD.

the problem is the first if statement. if not match in $destination

the logic is

if $localhostname match in $destination OU exit and write a log

else search in the $ROOTOU and move it to approprate OU ( $destination OU) by matching argument variable. write a log and exit.

thanks.

$argAB = "AB"
$argDM = "DM"
$argEB = "EB"

#$localhostname = "$env:COMPUTERNAME"
$localhostname = "abtxl001"
$ROOTOU = [ADSI]'LDAP://DC=my,DC=domain,DC=com'

#destination OU
$pathab = [ADSI]'LDAP://OU=SUBOU,OU=AB,OU=Computers,OU=NEW,DC=my,DC=domain,DC=com'
$pathdm = [ADSI]'LDAP://OU=SUBOU,OU=DM,OU=Computers,OU=NEW,DC=my,DC=domain,DC=com'
$patheb = [ADSI]'LDAP://OU=SUBOU,OU=EB,OU=Computers,OU=NEW,DC=my,DC=domain,DC=com'

#search Criteria
$searcher = New-Object Directoryservices.DirectorySearcher
$searcher.Filter = "(&(objectcategory=computer)(cn=$localhostname))"
$searcher.SearchRoot = $ROOTOU
$foundone = $searcher.FindOne()
$ADSPath = $foundone.Properties.adspath

#if not in destination OU
if(-not(($ADSPath -match $argAB) -or ($ADSPath -match $argEM) -or ($ADSPath -match $argDM))
                        #if (($ADSPath -match $argAB) -or ($ADSPath -match $argEM) -or ($ADSPath -match $argDM))
                        #     { if ($ADSPath -match $argAB){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($pathab) }
                        #       if ($ADSPath -match $argDM){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($pathdm) }
                        #       if ($ADSPath -match $argEB){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($patheb) }
                        #      Add-Content C:\Windows\Logs\move_to_NEW_OU.log $localhostname found and moved to East Bay Region OU" | Out-Null       
                    #         exit 0            
                        #     }     
                        #       else{  Add-Content C:\Windows\Logs\move_to_NEW_OU.log $localhostname does not exit" | Out-Null
                        #            exit 4
                        #            }
}

else{add-content c:\windows\logs\move_to NEW_OU.log $localhostname is already moved
exit 0
} 

All replies (4)

Saturday, September 7, 2013 6:47 PM âś…Answered

some double quote are misplaced:

if (($ADSPath -match $argAB) -or ($ADSPath -match $argEM) -or ($ADSPath -match $argDM))
     { if ($ADSPath -match $argAB){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($pathab) }
   if ($ADSPath -match $argDM){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($pathdm) }
   if ($ADSPath -match $argEB){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($patheb) }
Add-Content C:\Windows\Logs\move_to_NEW_OU.log "$localhostname found and moved to East Bay Region OU" | Out-Null 
exit 0            
}     
else{  Add-Content C:\Windows\Logs\move_to_NEW_OU.log "$localhostname does not exit" | Out-Null
exit 4
}

Saturday, September 7, 2013 8:53 AM

why not using ADDS cmdlet?

import-module activedirectory

$pathab = 'OU=SUBOU,OU=AB,OU=Computers,OU=NEW,DC=my,DC=domain,DC=com'
$pathdm = 'OU=SUBOU,OU=DM,OU=Computers,OU=NEW,DC=my,DC=domain,DC=com'
$patheb = 'OU=SUBOU,OU=EB,OU=Computers,OU=NEW,DC=my,DC=domain,DC=com'
$localhostname = "$env:COMPUTERNAME"
$ROOTOU = 'DC=my,DC=domain,DC=com'

if (($localhostname.substring(0,2)) -eq "ab") {
    $NeededOu = $pathab
}
elseif (($localhostname.substring(0,2)) -eq "dm") {
    $NeededOu = $pathdm
}
elseif (($localhostname.substring(0,2)) -eq "eb") {
    $NeededOu = $patheb
}
#if the matching i understood isn't necessary you can do 3 get-adcomputer
if (get-adcomputer $localhostname -searchbase $neededou) {
    write-host "$localhostname is already moved" | add-content c:\windows\logs\move_to NEW_OU.log
}
else {
    $AdObj = get-adcomputer $localhostname -searchbase $rootou
    if ($AdObj) {
        $AdObj | Move-ADObject -TargetPath $NeededOu
    }
    else {
        write-host "can't find $localhostname in Root OU" | add-content c:\windows\logs\move_to NEW_OU.log
    }
}

Saturday, September 7, 2013 12:11 PM

No I can't because I have 2003 DC and ADMGS is not installed.

I am migrating winxp to win7 and moving $localhostname from old ou to new ou through sccm TS on the fly.

this block work except error code "4" shown in TS report even name get moved.

it truned out that if name is alread in Destination OU it shown error code4.

if (($ADSPath -match $argAB) -or ($ADSPath -match $argEM) -or ($ADSPath -match $argDM))     { if ($ADSPath -match $argAB){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($pathab) }   if ($ADSPath -match $argDM){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($pathdm) }   if ($ADSPath -match $argEB){ $destination = [ADSI]("$ADSpath");$destination.psbase.moveto($patheb) }Add-Content C:\Windows\Logs\move_to_NEW_OU.log $localhostname found and moved to East Bay Region OU" | Out-Null exit 0            }     else{  Add-Content C:\Windows\Logs\move_to_NEW_OU.log $localhostname does not exit" | Out-Nullexit 4}

So..i am trying the logic with

if($localhostname already in Destination OU){exit the block with error code 0 and write the log}

else{run above logic code}

which i have trouble...

thank you for your help.


Sunday, September 8, 2013 10:26 PM

Thuguet,

thank you for your responsed.

was a typo when i copy into it.

the problem is when the hostname was already in the destination OU before get moved, TS get error code 4 and c:\windows\logs\ folder get access denied.

i add the logic to not check the destination OU and fixed the problem.

thank you again Thuguet.