I have a fairly simple script that needs to check around 20,000 AD Groups for their membership count. That all works fine, I can take the list of groups run it through the script and for the most entries it works fine. However I was getting some errors that I couldn't figure out and hopefully someone here can point me in the right direction.
I am using the DN of the object to query AD and for around 10% it fails, but when I copy the DN from the file, paste it into a command window and run the command manually it works fine. Some more checking and it seems that when I read an offending line into my variable there is a line break in the middle for some reason.
When looking at the value of the variable I get the following:
Working Example - "CN=ABC, OU=Location, OU=Distribution Lists, DC=Domain, DC=COM"
Error Example - "CN=ABC, OU=Location, OU=Distribution
Lists, DC=Domain, DC=COM"
It seems to insert a return in-between Distribution and Lists on certain entries in the file. I have tried deleting the character in-between and replacing it with a space but I get the same result.
Could it be the length? I am still looking for a common factor but any suggestions would be great.
Thanks
Updated with requested content.
$Groups = Import-Csv C:\Temp\DLName.csv
write-host ($Groups).Count
$i=1
foreach ($Group in $Groups)
{
$GroupInfo = Get-ADGroupMembersRecursive -Groups $Group.Name
$MembersCount = ($GroupInfo | Measure-Object).Count
$MembersList = $GroupInfo | Select Name -ExcludeProperty Name
$FriendlyName = Get-ADGroup -Identity $Group.Name
$Export = $FriendlyName.Name + ", " + $MembersCount
$Export | Out-File C:\Temp\DLMembers.csv -Append
Write-host $FriendlyName "," $MembersCount
$i
$i++
}
Entry 1 and 3 work 2 doesn't, but the formatting here seems to have wrapped the entries.
Name
"CN=Company - DL Name1,OU=Country1 Distribution Lists,OU=Europe,OU=Acc,DC=Domain,DC=Domain,DC=com"
"CN=Company - DL Name2,OU=Country2 Distribution Lists,OU=Europe,OU=Acc,DC=Domain,DC=Domain,DC=com"
"CN=Company - DL Name3,OU=Country3 Distribution Lists,OU=America,OU=Acc,DC=Domain,DC=Domain,DC=com"
Top pic is the failure second pic works.
List Creation:
$SearchScope = "OU=OUName,DC=Domain,DC=Domain,DC=com"
$SearchFilter = {GroupCategory -eq 'Distribution'}
$Groups = Get-ADGroup -SearchBase $SearchScope -Filter
$SearchFilter | Sort-Object Name
foreach ($Group in $Groups)
{
$Group.DistinguishedName | Select Name -ExpandProperty Name
$Group.DistinguishedName | Out-File C:\Temp\DLName.csv -Append
}
Do not use a self-combined comma separated string and Out-File to create CSV files, because that will get you into trouble when fields happen to contain the delimiter character like in this case the comma (which will lead to mis-aligned data).
Your List Creation code should be like this:
$SearchBase = "OU=OUName,DC=Domain,DC=Domain,DC=com"
$SearchFilter = "GroupCategory -eq 'Distribution'"
Get-ADGroup -SearchBase $SearchBase -Filter $SearchFilter |
Sort-Object Name | Select-Object Name, DistinguishedName |
Export-Csv -Path 'C:\Temp\DLName.csv' -NoTypeInformation
Then you can use that csv later to do:
$Groups = Import-Csv -Path 'C:\Temp\DLName.csv'
Write-Host $Groups.Count
$result = foreach ($Group in $Groups) {
$GroupInfo = Get-ADGroupMember -Identity $Group.DistinguishedName -Recursive
# unnecessary.. $MembersCount = ($GroupInfo | Measure-Object).Count
# unused.. $MembersList = $GroupInfo.Name
# unnecessary.. $FriendlyName = Get-ADGroup -Identity $Group.Name
# output an object with the wanted properties
[PsCustomObject]#{
GroupName = $Group.Name
MemberCount = #($GroupInfo).Count # #() in case there is only one member in the group
}
}
# show on screen
$result | Format-Table -AutoSize
# output to CSV file
$result | Export-Csv -Path 'C:\Temp\DLMembers.csv' -NoTypeInformation
As you can see, I'm not using your custom function Get-ADGroupMembersRecursive because I have no idea what that outputs.. Also, there is no need for that because you can use the Get-ADGroupMember cmdlet with the -Recursive switch added
I have the following working script:
# This script Extracts , Active Drirectory Groups the user is currently a memeber-of
$users = Get-Content "C:\powershell\Permmisions\users.txt"
foreach ($user in $users){ Get-ADPrincipalGroupMembership $user | select name | Out-File C:\Powershell\1.csv }
The problem is, Each line containing a group name, in the created CSV file,
Contain extra spaces charcters which i have to delete. is there a way to extract the following information to CSV or TXT , without the extra spaces i get?
Thanks.
You just need to substitute out-file with export-csv as below:
# This script Extracts , Active Drirectory Groups the user is currently a memeber-of
$users = Get-Content "C:\powershell\Permmisions\users.txt"
foreach ($user in $users){
Get-ADPrincipalGroupMembership $user | select name | export-csv C:\Powershell\1.csv -notypeinfo -append
}
Facing issue in output-file commandlet while extracting user list from given list of groups. I need each time a new text file to be generated in a Text format with the name of the groups given in the content list and data which is generated each time should be in that text file.
$grp= Get-Content 'C:\Users\p731400a\Desktop\groups.txt'
$grpdata= #()
$grpname=#()
foreach($grps in $grp)
{
$grps
$data= get-qadgroupmember $grps | Select-Object samaccountname | ft -AutoSize
$grpname +=$grps
$grpdata+= $data
}
Out-File -FilePath C:\Users\p731400a\Desktop\$grps.txt -InputObject $grpdata
If in the input file the group names are stored each on a separate line, this should do it:
# read the input file as string array and loop through the list
Get-Content 'C:\Users\p731400a\Desktop\groups.txt' | ForEach-Object {
# the $_ automatic variable here represents one group name at a time
# add the `-Indirect` switch to get group members recursively when using 'Get-QADGroupMember'
$members = Get-QADGroupMember $_ -Type 'user' -Indirect |
Select-Object -ExpandProperty SamAccountName
# or use
# $members = Get-ADGroupMember -Identity $_ -Recursive |
# Where-Object { $_.objectClass -eq 'user' } |
# Select-Object -ExpandProperty SamAccountName
#output on screen:
Write-Host ("{0}`r`n{1}" -f $_, ('-' * $_.Length)) -ForegroundColor Yellow
$members
#output to file:
$members | Add-Content -Path "C:\Users\p731400a\Desktop\$_.txt"
}
Hope that helps
I am trying to get a script together to rename about 40 security groups. I have imported them all into a csv in column A and put the name I need them changed to in column B. here is what I have so far.
Import-Csv C:\test.csv | ForEach-Object{
$item = $_;
Get-ADGroup -LDAPFilter "(&(sAMAccountName=$($_.OriginalName)))" | Set-ADGroup -OriginalName $item.Renameto
}
Thank you very much for all your help!
Import-Csv C:\test.csv | ForEach-Object{Rename-ADObject -Identity $_.ColumnAHeader -NewName $_.ColumnBHeader}
If possible, use the DistinguishedName in Column A. Otherwise you may have to use the partition parameter to specify the groups location.
You may have to remove Protect Object from accidental deletion. If so, try this:
Import-Csv C:\test.csv | ForEach-Object{
Set-ADObject -Identity $_.ColumnAHeader -ProtectedFromAccidentalDeletion:$false
Rename-ADObject -Identity $_.ColumnAHeader -NewName $_.ColumnBHeader -PassThru | Set-ADObject -ProtectedFromAccidentalDeletion:$true
}
I've put together a simple script to output a list of computers that are members of AD groups for WSUS Patching. The problem is that when I use this list of computer names to compare in Excel, there are a load of spaces/tab in the results. I don't want to have to manually edit the results, so I was wondering if there was a more graceful way to do this within the script?
$groups = Get-Content D:\WSUS\grouplist.txt
$result =#()
foreach($group in $groups){$result += Get-ADGroupMember $Group | select name}
$result | Out-File D:\WSUS\WSUS-All-AD-Members.txt
I've had a look at other options, but can't seem to get something that works.
Thanks a lot
Fixed it, added the -ExpandProperty option....
$groups = Get-Content D:\WSUS\grouplist.txt
$result =#()
foreach($group in $groups){$result += Get-ADGroupMember $Group | select -ExpandProperty name}
$result | Out-File D:\WSUS\WSUS-All-AD-Members.txt