I am getting multiple columns for multiple employees from an Oracle database. I am creating a psObject array but I have not been able to figure out how to then run through that array to make changes in AD for each person. This is what I am doing:
Then, I will take this information and set attributes in AD.
I have been trying to figure out how to run through the array with multiple values for each person. Right now I’d like to be able to just print out the values as I run through each object.
Would someone please point me in the right direction? I have been trying to figure this out for awhile, and am now finally asking for help!
You need to refer to $_ or $row as you prefer (as that’s what you defined) when constructing the PSObject. I’d also generally recommend against using New-Object for any decently-large dataset – it’s slower than alternatives. One possible way to go:
$result = $SelectDataTable | ForEach-Object {
[PSCustomObject]@{
UserName = $_.GetString(2)
Title = $_.GetString(3)
Department = $_.GetString(4)
}
}
However, depending on what type of object $SelectDataTable is, you may or may not be easily able to iterate over it with that cmdlet directly.
Thanks. I am still not sure how to reference each string from $result. Is it $result.username, $result.Title ? I guess not, as that I don’t think has worked for me.
Thanks Everyone. I ended up using the idea from js in this forum. The important part of the script is shown below. The last part I am struggling with is the fact that I need to have the street address be a combo of some of the variables - I want to do this right in the set-aduser command, but need the address lines to be on different lines. If anyone has an idea, please let me know. I’ll keep at it!
I had tried the cr line feed in between the variables, and that did not work. However, getting your idea about the quotes solved my problem. I was able to tuck the characters in there, thanks!
For anyone else struggling with this, here is what worked:
Argh, now I am stuck on something else. I didn’t realize that in my script if any of the columns are null from SQL it ignores the entire row due to an exception message in powershell - Exception calling “GetString” with “1” argument(s): “Unable to cast object of type ‘System.DBNull’ to type ‘System.String’.” …
I have been trying different things all day and have come up short. Does anyone have any ideas? Thank you!