Welcome › Forums › General PowerShell Q&A › 2 ForEach loops with output to SQL Server DB
- This topic has 7 replies, 4 voices, and was last updated 8 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
May 5, 2020 at 6:22 am #225588
I have a butchered version of Dan Belcher’s ‘Active Directory to SQL’
Which gets a list of ADGroups , Filtered in one loop Then Gets the ADGroupMembers for each group and output to SQL Server database.
Getting the ADGroups , works , Getting the ADGroupMembers works however when writing to the SQL ServerDatabase it inserts each Group as expected but the members list is always the GroupMembers from the last Group.
If I have 4 Groups each with varying number of members the output always show 4 groups all with the same member list.
here is an abridged version of the script
PowerShell123456789101112131415161718192021222324252627282930<#Setting up object variablesto be used for AD lookup#>$TIME = [datetime]::today.adddays(-$DAYS)$Groups = (Get-AdGroup -filter * | Where {$_.name -like "*I&O*"} )$Arrayofmembers = Get-ADGroupMember -identity $Group.name -recursive | Get-ADUser -Properties samaccountname, displayname, objectclass, name, enabled, UserPrincipalName<#Connect and cleanup the AD tableConnection remains open for writting#><#Begin loop through the ADARRAY forVariables and begin inserting Rows to table#>ForEach($Group in $Groups){foreach ($Member in $Arrayofmembers) {$GroupName = $Group.Name$Name = $Member.name$ObjectClass=$Member.Objectclass#$DisplayName = $Member.Displayname$DisplayName = $Member.DisplayName.replace("'","''")$sam = $Member.samaccountname$email = $Member.UserPrincipalName.replace("'","''")$enabled = $Member.Enabled$GroupName$Name$ObjectClass$DisplayName$sam$Enabled$email}}-
This topic was modified 8 months, 3 weeks ago by
GlynLamb75.
-
This topic was modified 8 months, 3 weeks ago by
-
May 5, 2020 at 6:24 am #225594
Glyn, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.
When you post code or error messages or sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
Thanks in advance. -
May 5, 2020 at 7:02 am #225612
Seems you just need to move your assignment of members inside the groups loop?
PowerShell1234567891011121314151617181920212223242526272829<#Setting up object variablesto be used for AD lookup#>$TIME = [datetime]::today.adddays(-$DAYS)$Groups = (Get-AdGroup -filter * | Where {$_.name -like "*I&O*"} )<#Connect and cleanup the AD tableConnection remains open for writting#><#Begin loop through the ADARRAY forVariables and begin inserting Rows to table#>ForEach($Group in $Groups){$Arrayofmembers = Get-ADGroupMember -identity $Group.name -recursive | Get-ADUser -Properties samaccountname, displayname, objectclass, name, enabled, UserPrincipalNameforeach ($Member in $Arrayofmembers) {$Name = $Member.name$ObjectClass=$Member.Objectclass#$DisplayName = $Member.Displayname$DisplayName = $Member.DisplayName.replace("'","''")$sam = $Member.samaccountname$email = $Member.UserPrincipalName.replace("'","''")$enabled = $Member.Enabled$GroupName$Name$ObjectClass$DisplayName$sam$Enabled$email}} -
May 5, 2020 at 7:28 am #225615
Not exactly sure what you mean
do you mean
PowerShell123456789101112131415161718192021222324ForEach($Group in $Groups){$GroupName = $Group.Name$Name = $Member.name$ObjectClass=$Member.Objectclass#$DisplayName = $Member.Displayname$DisplayName = $Member.DisplayName.replace("'","''")$sam = $Member.samaccountname$email = $Member.UserPrincipalName.replace("'","''")$enabled = $Member.Enabled$GroupName$Name$ObjectClass$DisplayName$sam$Enabled$email$X = $X + 1foreach ($Member in $Arrayofmembers) {}} -
May 5, 2020 at 7:33 am #225618
Sorry, I reread you post and I think you have solved it.
Thanks
-
May 5, 2020 at 4:26 pm #225771
Good. Yeah I think you had everything you needed, just needed to move the $arrayofmembers assignment inside the group loop. I wasn’t sure what the variables at the bottom were, I assumed placeholder for where you write to SQL.
-
May 5, 2020 at 6:56 pm #225801
Also, put the output in the PSObject:
PowerShell123456789101112131415161718192021222324<#Setting up object variablesto be used for AD lookup#>$TIME = [datetime]::today.adddays(-$DAYS)$Groups = (Get-AdGroup -filter * | Where {$_.name -like "*I&O*"} )<#Connect and cleanup the AD tableConnection remains open for writting#><#Begin loop through the ADARRAY forVariables and begin inserting Rows to table#>$results = ForEach($Group in $Groups){$Arrayofmembers = Get-ADGroupMember -identity $Group.name -recursive | Get-ADUser -Properties samaccountname, displayname, objectclass, name, enabled, UserPrincipalNameforeach ($Member in $Arrayofmembers) {[PSCustomObject]@{Name = $Member.nameObjectClass = $Member.ObjectclassDisplayName = $Member.DisplayName.replace("'","''")Sam = $Member.samaccountnameEmail = $Member.UserPrincipalName.replace("'","''")Enabled = $Member.Enabled}}}$results -
May 6, 2020 at 3:10 am #225867
Thanks for your responses. All fixed and working
-
-
AuthorPosts
- The topic ‘2 ForEach loops with output to SQL Server DB’ is closed to new replies.