I am pulling two queries from Active Directory and assigning them to different variables. I'd like to combine these variables into a single $Output and export it to a CSV file. I'm running into 2 issues:
- Some of the users don't have an assigned manager so I'm getting errors back when the script runs. I'm also not sure if the 2 variables will line up properly, as there will now be blank values in the $Managers output.
- I can't seem to get the data to output to a CSV file successfully. At the moment I'm getting the CSV file but each column has "System.Object[]" as the output.
This is what I've written up thus far:
# Retrieve a list of active user accounts with a defined list of attributes and assign them to a variable $Users = (Get-ADUser -Filter {Enabled -eq "True"} -Properties DisplayName, GivenName, Surname, Title, TelephoneNumber, StreetAddress, City, State, PostalCode, Country, Mail, UserPrincipalName, Department, Manager | Select DisplayName, GivenName, Surname, Title, TelephoneNumber, StreetAddress, City, State, PostalCode, Country, Mail, UserPrincipalName, Department, Manager) # Create an array to store the output $Output = @() # Based on the initial query, retrieve manager specific attributes and assign them to a veritable $Managers = ForEach ($Manager in $Users) {Get-ADUser -Identity $Manager.manager -Properties DisplayName, UserPrincipalName, Mail | select DisplayName, UserPrincipalName, Mail} # Create a new object to store this information for CSV export $OutputItem = New-Object Object; $OutputItem | Add-Member NoteProperty "Display Name" $Users.DisplayName; $OutputItem | Add-Member NoteProperty "First Name" $Users.GivenName; $OutputItem | Add-Member NoteProperty "Last Name" $Users.Surname; $OutputItem | Add-Member NoteProperty "Title" $Users.Title; $OutputItem | Add-Member NoteProperty "Telephone Number" $Users.TelephoneNumber; $OutputItem | Add-Member NoteProperty "Street Address" $Users.StreetAddress; $OutputItem | Add-Member NoteProperty "City" $Users.City; $OutputItem | Add-Member NoteProperty "State" $Users.State; $OutputItem | Add-Member NoteProperty "Zip Code" $Users.PostalCode; $OutputItem | Add-Member NoteProperty "COuntry" $Users.Country; $OutputItem | Add-Member NoteProperty "Email Address" $Users.Mail; $OutputItem | Add-Member NoteProperty "UPN" $Users.UserPrincipalName; $OutputItem | Add-Member NoteProperty "Department" $Users.Department; $OutputItem | Add-Member NoteProperty "Manager" $Managers.DisplayName; $OutputItem | Add-Member NoteProperty "Manager UPN" $Managers.UserPrincipalName; $OutputItem | Add-Member NoteProperty "Manager Email Address" $Managers.Mail; # Add the object to our array of output objects $Output += $OutputItem; # Export the results to CSV $Output | Export-CSV "Output.csv" -NoTypeInformation
Appreciate any help =)