I'm aiming to create an array of objects. I query AD and send the results to a function which should create a new object then add this to an array of that type of object. It works but not how I would want in that if I pass variables (1,2,3) then only the first parameter in the object gets any values and the next two stay blank:
$Version = "1.0" #Find user by employee Title 22/3/23
Purpose of script: Typical call "Can you tell me who is the Head of these departments? Using this script we will be able to search for phrases such as "Head" or "Manager" etc
#Import Module# import-module Activedirectory
$global:UsersArray = @()
Function AddUserToObjArray ($Nombre, $Department, $Title) {
$UsersObject =New-Object PSObject
$UsersObject | Add-Member -MemberType NoteProperty -Name "Name" -Value ($Nombre)
$UsersObject | Add-Member -MemberType NoteProperty -Name "Department" -Value ($Department)
$UsersObject | Add-Member -MemberType NoteProperty -Name "Title" -Value ($Title)
$global:UsersArray += $UsersObject
}
#Main
cls
$Team = get-aduser -Filter {title -like "* Service Desk *"} -Properties Name, Department, Title #| Select Name, Department, Title #| Select Name, Department, Title
foreach ( $Member in $Team) {
The command below is purely for debugging but does display all the data I want.
write-host ("The person name is ", $Member.Name, "and ", "the team name is ", $Member.Department, ". The position is ", $Member.Title)
AddUserToObjArray($Member.Name, $Member.Department, $Member.Title)
}
foreach ($user in $UsersArray) {
#The command below has a flaw (in my coding) in that all of the data ends up in $user.name as opposed to being spread across all three fields.
write-host ($user.Name, , " is an ", $user.Title, " in ", $user.Department)
}
Tried casting ($Nombre) etc as $ strings but the end result stayed the same. When displaying the resultant array for each element $UsersArray[x].Name gets the entire string and .Department and .Title get nothing.