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
}
creative555Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy_CooperCommented:
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!
0
MlandaTCommented:
$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

0
creative555Author Commented:
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:
0
What were the top attacks of Q1 2018?

The Threat Lab team analyzes data from WatchGuard’s Firebox Feed, internal and partner threat intelligence, and a research honeynet, to provide insightful analysis about the top threats on the Internet. Check out our Q1 2018 report for smart, practical security advice today!

creative555Author Commented:
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!!
0
Tommy_CooperCommented:
Oops... My bad!
See line 1 & 2:
#Create a logfile
$Logfile = 'C:\DHL\Scripts\GroupsComputersScripts\Groups.txt'

Open in new window

Line 1 is a comment. Badly written because we're not creating a log directory. Line 2 only declares that the variable '$Logfile' will be located in the place specified. But that is also your input file! so it should read:
#Create a logfile
$Logfile = 'C:\DHL\Scripts\GroupsComputersScripts\MyLog.log'

Open in new window

Remember that the logfile gets written by the 'add-content' command on line 14. This will NOT create a folder structure if it doesn't exist, so make sure you have that in place before running the script. It WILL create a new logfile though, so that doesn't need to exist beforehand (but it MUST be different to your input file!)

On top of that.... I am confused that the 'if' statement is not working as expected. I think this is possibly due to the fact that Get-ADGroup outputs a different type of object to what I am expecting! Maybe a guru can put us straight here?

But the following defo works - I tested this today :) So we do have to check if the group exists and then I'm matching it against the group name in your text file. If the two don't match it will create a log entry.
#Create a logfile
$Logfile = 'C:\Users\s-mipsmon\Documents\Scripts\LogGroups.txt'

#Import your group names
$groups = get-content 'C:\Users\s-mipsmon\Documents\Scripts\Groups.txt'

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

Open in new window


Hope that helps?!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
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.
0
creative555Author Commented:
Thank you so much!! Exactly what I needed :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.