Hey guys :)
PS noobie here.
I got this script (stolen - didnt make it myself, just changed it a bit) that lists members in a specific active directory group in a xls (excel workbook)
It lists the names, usernames(samaccountname), company and department.
I've managed to create several sheets and and to get the output in the first sheet.. But I want to get some of the output into different sheets, based on what company or department the user have.
Example:
One users department field contains "RUS" and another contains "SYA".
I want the "RUS"-user to be listed in both sheet 1 and sheet 2, and I want the "SYA"-user listed in both sheet 1 and 3.
Is it possible? If it's easier, I want the "RUS"-user to be listed in sheet 2 and the "SYA"-user to be listed in sheet 3.
Here is my script so far:
Add-PSSnapin Quest.ActiveRoles.ADManagement $snapins=@("Quest.ActiveRoles.ADManagement") #we'll be using Quest's Ad cmdlets to get group membership foreach ($snapin in $snapins){ if (get-pssnapin $snapin -ea "silentlycontinue") { Write-Debug "$(Get-Date -format T): PSsnapin $snapin is loaded" } elseif (get-pssnapin $snapin -registered -ea "silentlycontinue") { Write-Host "$(Get-Date -format T): PSsnapin $snapin is registered but not loaded. Loading now." Add-PSSnapin $snapin } else { Write-Host "$(Get-Date -format T): PSSnapin $snapin not found, cannot continue. You must have the snapin to continue." -ForegroundColor Red break } } $excel = New-Object -c excel.application -vb:0 $excel.visible = $true $excel.DisplayAlerts = $False $workbook = $excel.workbooks.add() # Create workbook with one worksheet $adgroups = @(Get-QADGroup G_App_VPN_TS -DontUseDefaultIncludedProperties -IncludedProperties name -SizeLimit 0 | sort name -Descending) $i = 6 foreach ($adgroup in $adgroups) { if ($i -gt 1){ [void]$workbook.Sheets.add() # adds a new Worksheet (the [void] stops all the informational output.) [void]$workbook.Sheets.add() [void]$workbook.Sheets.add() [void]$workbook.Sheets.add() [void]$workbook.Sheets.add() } else { $i++ } $groupname = $adgroup.Name $worksheetname = $groupname $worksheetname2 = "HNR - Helse Nordmøre og Romsdal" $worksheetname3 = "HNT - Helse Nord-Trøndelag HF" $worksheetname4 = "HSM - Helse Sunnmøre og Romsdal" $worksheetname5 = "RHF - Helse Midt-Norge RHF" $worksheetname6 = "RUS = Rusbehandling" $worksheetname7 = "SYA - Sykehusapoteket" $worksheetname8 = "Stolav - St. Olavs Hospital" if ($worksheetname.Length -gt 31) {$worksheetname = $worksheetname.Substring(0,31) } $worksheetname = $worksheetname.Replace(":","_") $worksheetname = $worksheetname.Replace("\","_") $worksheetname = $worksheetname.Replace("?","_") $worksheetname = $worksheetname.Replace("*","_") $worksheetname = $worksheetname.Replace("[","_") $worksheetname = $worksheetname.Replace("]","_") $worksheetname = $worksheetname.Replace("/","_")
$worksheet = $workbook.sheets.item(1) # Defines the sheets $worksheet2 = $workbook.sheets.item(2) $worksheet3 = $workbook.sheets.item(3) $worksheet4 = $workbook.sheets.item(4) $worksheet5 = $workbook.sheets.item(5) $worksheet6 = $workbook.sheets.item(6) $worksheet7 = $workbook.sheets.item(7) $worksheet8 = $workbook.sheets.item(8) $worksheet.name = $worksheetname # Defines name og each sheet $worksheet2.name = $worksheetname2 $worksheet3.name = $worksheetname3 $worksheet4.name = $worksheetname4 $worksheet5.name = $worksheetname5 $worksheet6.name = $worksheetname6 $worksheet7.name = $worksheetname7 $worksheet8.name = $worksheetname8 $groupmembers = @(Get-QADGroupMember -SizeLimit 0 -Identity $adgroup -DontUseDefaultIncludedProperties -IncludedProperties Displayname,SamAccountName,Company,department,description | Sort company) if ($groupmembers.count -eq 0){ $worksheet.Cells.Item(1,1) = "No members in $groupname" $workbookrange = $worksheet.UsedRange [Void]$workbookrange.EntireColumn.AutoFit() } else { $worksheet.Cells.Item(1,1) = "Navn" # Defines norwegian headers in the first sheet $worksheet.Cells.Item(1,2) = "Brukernavn" $worksheet.Cells.Item(1,3) = "Kunde" $worksheet.Cells.Item(1,4) = "Avdeling" $worksheet.Cells.Item(1,5) = "Beskrivelse" $worksheet.Cells.Item(1,6) = "Pris" $workbookrange = $worksheet.UsedRange $intRow = 2 foreach ($groupmember in $groupmembers){ # Defines headers (kinda) $worksheet.Cells.Item($introw,1) = $groupmember.Displayname $worksheet.Cells.Item($introw,2) = $groupmember.SamAccountName $worksheet.Cells.Item($introw,3) = $groupmember.Company $worksheet.Cells.Item($introw,4) = $groupmember.department $worksheet.Cells.Item($introw,5) = $groupmember.description $introw++ [Void]$workbookrange.EntireColumn.AutoFit() } } } ##OUTPUT PART $dagensdato = ((get-date).tostring('dd-MM-yyyy')) # Dagensdato = todays date. I'm inlcuding todays date it in the output filename $fil1 = "hkfakturering-$dagensdato" $file = "\\hemitbibl\brukere\kariab\skript\hjemmekontofakturering\$fil1-output11.xlsx" # workbook's fullpath Write-Host $file $workbook.close($true, $file) # close and save the workbook $excel.quit() # clear up the resources remove-variable excel # clear up the resources [gc]::collect() # clear up the resources
In addition to this I want to exclude certain users. I want to exclude all users where campany contains the word "test". Can you help me with this as well?
I got a vbscript that does this, but I dont want to be oldschool, I want powershell. Is there anybody who knows how to translate this into powershell?
For Each strMember in arrMemberOf Set objUser = GetObject("LDAP://" & strMember) company = Lcase(objUser.company) if company = "fellesnøkkel" Or instr(company, "test") Or objUser.AccountDisabled = True Then 'Skal ikke være med if instr(company,"test") then WScript.echo company & " : " & objUser.distinguishedName
Thanks for all your help :)