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\GroupsComp utersScrip ts\Groups. txt"
ForEach ($g in $groups)
{
Get-ADGroup -Identity $g -Properties name | select name
}
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\GroupsComp
ForEach ($g in $groups)
{
Get-ADGroup -Identity $g -Properties name | select name
}
$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"
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-ContractLogisti cs-U' under:
'DC=nyc,DC=test,DC=com'.
At C:\NYC\Scripts\GroupsCompu tersScript s\Get-ADGr oupsv5.ps1 :8 char:19
+ $checkGroup = Get-ADGroup -Identity $g -Properties name -ErrorAction Silentl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~
+ CategoryInfo : ObjectNotFound: (groupADM-ContractLogistic s-U:ADGrou p) [Get-
ADGroup], ADIdentityNotFoundExceptio n
+ FullyQualifiedErrorId : Cannot find an object with identity: 'testgroup-ADM-ContractLog is
tics-U' under: 'DC=nyc,DC=test,DC=com'.,M icrosoft.A ctiveDirec tory.Manag ement.Comm ands.Get
ADGroup
Get-ADGroup : Cannot find an object with identity: 'ADM-Agile Support' under:
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-ContractLogisti
'DC=nyc,DC=test,DC=com'.
At C:\NYC\Scripts\GroupsCompu
+ $checkGroup = Get-ADGroup -Identity $g -Properties name -ErrorAction Silentl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (groupADM-ContractLogistic
ADGroup], ADIdentityNotFoundExceptio
+ FullyQualifiedErrorId : Cannot find an object with identity: 'testgroup-ADM-ContractLog
tics-U' under: 'DC=nyc,DC=test,DC=com'.,M
ADGroup
Get-ADGroup : Cannot find an object with identity: 'ADM-Agile Support' under:
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could also export the error info directly to a CSV.
If there's a subset of the error info you want, you can grab that too.
$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
If there's a subset of the error info you want, you can grab that too.
ASKER
Thank you so much!! Exactly what I needed :)
Open in new window
As ever - I haven't tested, but this will get you going in the right direction!