Link to home
Start Free TrialLog in
Avatar of creative555
creative555

asked on

Help with the script. Need to export error in one line in excel. list of groups that are missing from AD

Hello,
I am new to powershell. I just need this script below to read input file with groups.txt and export the results with groups that are not found in Active Directory to csv.

So, this script works and if the group exists in AD, then it just displays the group, but if it is missing from AD, then I get a long error message about 5 lines per group that object is not found, etc. I just need to know what groups in excel file do not exist in Active directory.
Could you please take the error code somehow export it so it is just one line and not 5 error lines per group.

Thank you so much!!


$groups = get-content "C:\DHL\Scripts\GroupsComputersScripts\Groups.txt"

ForEach ($g in $groups)
    {

       Get-ADGroup -Identity $g -Properties name | select name
}
Avatar of Tommy_Cooper
Tommy_Cooper
Flag of Antarctica image

Nearly there!  Try using an 'if' statement....
#Create a logfile
$Logfile = 'C:\DHL\Scripts\GroupsComputersScripts\Groups.txt'

#Import your group names
$groups = get-content "C:\DHL\Scripts\GroupsComputersScripts\Groups.txt"

#Step through each group and if it exists do nothing. If it deosn;t log it to the logfile
ForEach ($g in $groups) {
    If (Get-ADGroup -Identity $g -Properties name | select name) {
        Out-Null
        }
    Else {
        Add-Content -Path $Logfile -Value "$G does not exist"
        }
    }

 

Open in new window


As ever - I haven't tested, but this will get you going in the right direction!
Avatar of Mlanda T
$groups = get-content "C:\DHL\Scripts\GroupsComputersScripts\Groups.txt"
$list = ""

ForEach ($g in $groups)
{

    #Check to make sure Active Directory group exists
    $checkGroup = Get-ADGroup -Identity $g -Properties name -ErrorAction SilentlyContinue | select name
 
    if($checkGroup -eq $null) {
        $list += @{$true=$g;$false=", $g"}[[string]::IsNullOrEmpty($list)];
    }

}

$list | Out-File "C:\Temp\MissingGroup.txt"

Open in new window

Avatar of creative555
creative555

ASKER

Hey,
thank you so much
I  tried both scripts and I get nothing in the txt file....(:

For the second script I get this error displayed in powershell but nothing is exported to missing groups.txt


Get-ADGroup : Cannot find an object with identity: 'group-ADM-ContractLogistics-U' under:
'DC=nyc,DC=test,DC=com'.
At C:\NYC\Scripts\GroupsComputersScripts\Get-ADGroupsv5.ps1:8 char:19
+     $checkGroup = Get-ADGroup -Identity $g -Properties name -ErrorAction Silentl ...
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (groupADM-ContractLogistics-U:ADGroup) [Get-
   ADGroup], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: 'testgroup-ADM-ContractLogis
   tics-U' under: 'DC=nyc,DC=test,DC=com'.,Microsoft.ActiveDirectory.Management.Commands.Get  
  ADGroup
 
Get-ADGroup : Cannot find an object with identity: 'ADM-Agile Support' under:
and for the first script the groups.txt file doens't change either...I only get output in the powershell window but not in the txt file.Please help. THank you so much!!
ASKER CERTIFIED SOLUTION
Avatar of Tommy_Cooper
Tommy_Cooper
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could also export the error info directly to a CSV.

$Logfile = 'C:\DHL\Scripts\GroupsComputersScripts\GroupErrorInfo.csv'
$groups = get-content "C:\DHL\Scripts\GroupsComputersScripts\Groups.txt"

ForEach ($g in $groups){
    Get-ADGroup -Identity $g -Properties name -ErrorVariable +GroupErrors
}

$GroupErrors | Export-Csv $Logfile -NoTypeInformation

Open in new window


If there's a subset of the error info you want, you can grab that too.
Thank you so much!! Exactly what I needed :)