none
Check AD Computer before groups creation RRS feed

  • Question

  • Hi Expert,

    i write a groups creation PS script below, but i how do i add computer check before the group creation and skip group creation if computer not exist on AD.

    check if Server01 exist on AD if yes proceed group creation, if server02 not exist on AD skip group creation and continue check on all computer from the input list

    $DomainInfo = Get-ADDomain
    $DomainName = (Get-ADRootDSE).defaultNamingContext
    $OUr = "OU=Groups,$DomainName"
    $InputGroup = gc .\INPUT.txt
    
    
    foreach ($Group in $InputGroup)
    {
    $GroupName = $Group +"-LocalAdmins"
    if(!$(Get-ADGroup $GroupName)){
    New-ADGroup -Name $GroupName -Description "$Group Server Local Admins" -GroupCategory Security -GroupScope DomainLocal -Path "$OUr" -Verbose
    
    "$GroupName,LocalAdmins" | Out-File $NewServerDetails -Append
    Message "INFO |$GroupName has been created"
    }
    else
    {Write-Host "$GroupName is already exist" -ForegroundColor Red
    Message "Warning | $GroupName is already exist" }
    }}
    
    
    

    Saturday, May 23, 2020 7:59 AM

All replies

  • Hi Expert,

    i write a groups creation PS script below, but i how do i add computer check before the group creation and skip group creation if computer not exist on AD.

    check if Server01 exist on AD if yes proceed group creation, if server02 not exist on AD skip group creation and continue check on all computer from the input list

    $DomainInfo = Get-ADDomain $DomainName = (Get-ADRootDSE).defaultNamingContext $OUr = "OU=Groups,$DomainName" $InputGroup = gc .\INPUT.txt

    foreach ($Group in $InputGroup) { $GroupName = $Group +"-LocalAdmins" if(!$(Get-ADGroup $GroupName)){ New-ADGroup -Name $GroupName -Description "$Group Server Local Admins" -GroupCategory Security -GroupScope DomainLocal -Path "$OUr" -Verbose "$GroupName,LocalAdmins" | Out-File $NewServerDetails -Append Message "INFO |$GroupName has been created" } else {Write-Host "$GroupName is already exist" -ForegroundColor Red Message "Warning | $GroupName is already exist" } }}


    • Merged by jrv Saturday, May 23, 2020 8:11 AM DUPLICATE
    Saturday, May 23, 2020 7:53 AM