I'm trying to automate some Office 365 user creation stuff and am getting stuck on a simple formatting syntax for displaying values that will be changing. This piece of code searches for any users with a null value for the Usage Location field in Azure's directory and stores them into a file (which is used multiple times later, and I'd like to keep it in a file for archiving purposes as well)... All I want to display to the user running the script is a simple table with Firstname, Lastname, and UPN... (not sam, needs to be UPN see the subdomain value). But the results will only be processed if the UPN contains "@STU.DOMAIN.COM" ... Here's what I have right now:
Get-MsolUser -MaxResults 4000 | Where {$_.usagelocation -eq $null}| Export-Csv C:\Test.csv
Import-CSV C:\Test.csv | ForEach-Object {
$fname= $_.FirstName
$lname= $_.LastName
$UPN= $_.UserPrincipalName
if ($UPN.Contains("@STU.DOMAIN.COM")) {
New-Object -Typename PSObject -Property @{
"User Principal Name" = $UPN
"Last Name" = $lname
"First Name" = $fname
}
}
}
I have no real idea why, but it works when I run the tiny part of code here in an ISE window, but it does not work in a normal powershell window while I'm connected to MSOL and the rest of the script is in play...? I guess I'm looking for someone with actual experience to show me the proper way to format this task... Anyone got some wisdom to share please?
Thanks!
Update:
Hi Frode - this doesn't appear to separate them into groups. The "User Principal Name" variable is their whole username@UPN so it doesn't group it by domain and subdomain. The output looks like this:
User Principal Name: User1@DOMAIN.COM
First Name Last Name User Principal Name
---------- --------- -------------------
User1 Last1 User1@DOMAIN.COM
User Principal Name: User2@STU.DOMAIN.COM
First Name Last Name User Principal Name
---------- --------- -------------------
User2 Last2 User2@STU.DOMAIN.COM
How do I get just the @STU.DOMAIN.COM or @DOMAIN.COM of the variable to sort by?
Update 2:
I managed to figure out a way to strip off the suffix of the UPN and display that, but it still is quirky. It lists clumps of users of different UPNs randomly... Here's the code I modified from Frode:
$users |
Select-Object @{n="First Name";e={$_.FirstName}}, @{n="Last Name";e={$_.LastName}}, @{n="Domain/UPN";e={$_.UserPrincipalName.split("@")[1]}} |
Format-Table -AutoSize -GroupBy "Domain/UPN" | Out-String | Write-Host
But the output is a bit weird... it looks like this:
Domain/UPN: STU.DOMAIN.COM
First Name Last Name Domain/UPN
---------- --------- ----------
Dede Lastname STU.DOMAIN.COM
Domain/UPN: DOMAIN.COM
First Name Last Name Domain/UPN
---------- --------- ----------
Thomas Lastname DOMAIN.COM
Wells Lastname DOMAIN.COM
Amy Lastname DOMAIN.COM
Tiffany Lastname DOMAIN.COM
Sara Lastname DOMAIN.COM
Amy Lastname DOMAIN.COM
Domain/UPN: STU.DOMAIN.COM
First Name Last Name Domain/UPN
---------- --------- ----------
Jair Lastname STU.DOMAIN.COM
Domain/UPN: DOMAIN.COM
First Name Last Name Domain/UPN
---------- --------- ----------
Cathy Lastname DOMAIN.COM
Paul Lastname DOMAIN.COM
Ridgewood Lastname DOMAIN.COM
Patty Lastname DOMAIN.COM
Linda Lastname DOMAIN.COM
Chris Lastname DOMAIN.COM
Cami Lastname DOMAIN.COM
Kent Lastname DOMAIN.COM
Michael Lastname DOMAIN.COM
Stephanie Lastname DOMAIN.COM
Amie Lastname DOMAIN.COM
Patrick Lastname DOMAIN.COM
LaVonne Lastname DOMAIN.COM
De Lastname DOMAIN.COM
Joann Lastname DOMAIN.COM
Chuck Lastname DOMAIN.COM
Kathy Lastname DOMAIN.COM
Sue Lastname DOMAIN.COM
Tech Lastname DOMAIN.COM
Theresa Lastname DOMAIN.COM
Kristin Lastname DOMAIN.COM
Toni Lastname DOMAIN.COM
Sabrina Lastname DOMAIN.COM
Is there a method to parse them and group them together in one big lump by domain/UPN suffix?
Thanks again in advance!!