Need help with PS Script to add Computers to Groups based on Computer Description
hi all,
i'm looking alternative way write script add computer matching filter, adding computer based on it's description, security group. computers start ws being filtered. i'd grab ad description field , add computer ad group based on description. heres have far , works, if statement nesting nasty. in advance!
import-module activedirectory add-pssnapin quest.activeroles.admanagement -ea silentlycontinue $erroractionpreference="silentlycontinue" $workstations = get-adcomputer -filter {(name -like "ws*")} -properties description | select-object name, description | sort-object name foreach ($workstation in $workstations){ if ($workstation.description -like "5icu-rmc-5"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.5icu.rmc"} elseif ($workstation.description -like "mhu-adlt-rmc2"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.mhu.adult.400bldg"} elseif ($workstation.description -like "mhu-bsu-rmc2"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.mhu.bsu.400bldg"} elseif ($workstation.description -like "mhu-gsu-rmc2"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.mhu.gsu.400bldg"} elseif ($workstation.description -like "mhu-chadlt-rmc2"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.mhu.child.400bldg"} elseif ($workstation.description -like "mhu-adlt-rmc2"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.mhu.adult.400bldg"} elseif ($workstation.description -like "sound-400-302"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.soundhospitalist.400bldg"} elseif ($workstation.description -like "is-rmc"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.is.400bldg"} elseif ($workstation.description -like "nsgadm-400-305"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.nursingadmin.400bldg"} elseif ($workstation.description -like "ba-is-400-407"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.businessanalytics.400bldg"} elseif ($workstation.description -like "rmg-ns-400-402"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.neurosurgery.400bldg"} elseif ($workstation.description -like "rmg-pmr-400-400"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.pmr.400bldg"} elseif ($workstation.description -like "ekg-400-500"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.ekg.proctesting.400bldg"} elseif ($workstation.description -like "5rehab-400-5"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.5rehab.400bldg"} elseif ($workstation.description -like "is-info-401-stb"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.informatics.401bldg"} elseif ($workstation.description -like "medrec-401-bsmt"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.medrec.401bldg"} elseif ($workstation.description -like "wrdprc-401-bsmt"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.wordproc.401bldg"} elseif ($workstation.description -like "wound-401-bsmt"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.woundctr.401bldg"} elseif ($workstation.description -like "ambulance-atr"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.ambulance.atr"} elseif ($workstation.description -like "rehabop-atr"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rehabsvcsop.atr"} elseif ($workstation.description -like "rmg-corph-atr"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.corphealth.atr"} elseif ($workstation.description -like "rmg-pul-500-100"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.pulmcare.500bldg"} elseif ($workstation.description -like "rmg-psy-500-200"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.psych.500bldg"} elseif ($workstation.description -like "rmg-nro-500-300"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.neurology.500bldg"} elseif ($workstation.description -like "rmg-pul-500-300"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.rheumotology.500bldg"} elseif ($workstation.description -like "rmg-pul-500-100"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.pulmcare.500bldg"} elseif ($workstation.description -like "aic-rmc-500"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.aic.500bldg"} elseif ($workstation.description -like "cardiac-500-400"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.cardiacrehab.500bldg"} elseif ($workstation.description -like "rmg-card-500"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.cardio.500bldg"} elseif ($workstation.description -like "rmg-cardv-500"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.rmg.cardiov.500bldg"} elseif ($workstation.description -like "ekg-500-bsmt"){add-adprincipalgroupmembership -identity ($workstation.name + "$") -memberof "sg.ekg.500bldg"}}
try this:
import-module activedirectory add-pssnapin quest.activeroles.admanagement -ea silentlycontinue $erroractionpreference="silentlycontinue" function addgroupmembership { param ( [string]$name, [string]$group ) add-adprincipalgroupmembership -identity $name -memberof $group } function description { param ( [string]$description, [string]$name ) switch ($description) { "5icu-rmc-5" { addgroupmembership $name "sg.5icu.rmc" } "mhu-adlt-rmc2" { addgroupmembership $name "sg.mhu.adult.400bldg" } } } $workstations = get-adcomputer -filter {(name -like "ws*")} -properties description | select-object name, description | sort-object name foreach ($workstation in $workstations) { description $workstation.description ($workstation.name + '$') }included couple of if conditions description function switch statement. should able complete rest
if find post has answered question, please mark answer. if find post helpful in anyway, please click vote helpful.
Windows Server > Windows PowerShell
Comments
Post a Comment