Select -expandproperty and other attributes

This topic contains 8 replies, has 2 voices, and was last updated by  Jon 4 weeks, 1 day ago.

  • Author
    Posts
  • #84299

    Jon
    Participant

    So I know I can do this and get the expanded properties of a value

    get-msoluser -userprincipalname test@test.com  | select -expandproperty licenses

    However I want to run through all of my users and see what they are licensed for. I need to use -expand property because some of the users are licensed for multiple items and it doesn't show them all if I don't expand the property. But I can't expand the property and get say UPN so I can link up the username to the licenses.

    What's the best way to get both the expanded property and the single valued attribute?

  • #84302

    Don Jones
    Keymaster

    You can't really do both at the same time. You'll want to get your users and then enumerate them in a ForEach. That'll get you your user information; within the ForEach, you can expand out licenses on a per-user basis.

    It'd be helpful to know what kind of output you're thinking of – I'd be able to offer something more concrete.

  • #84310

    Jon
    Participant

    I figured that was the case.

    I just want an output of userprincipalname and licenses, here is an example of what one user looks like if I run this:

    get-msoluser -All | select licenses, userprincipalname

    Licenses UserPrincipalName
    ———- ———————–
    {domain:MCOMEETADV_GOV, domain:VISIOCLIENT_GOV, Domain:PROJECTCLIENT_GOV, ... username@domain.com

  • #84313

    Don Jones
    Keymaster

    So, I'm guessing what you don't like about the current output is the {} syntax used to show a collection of licenses.

    I would go through each user, and expand their licenses. For each license, I'd output a new object having the UPN as one property, and a single license as another property. That will get you something a bit more like what you want.

  • #84316

    Jon
    Participant

    The {} is fine, it's that if there are more than 3 licenses it cuts off the rest of them. I'd actually prefer the whole collection expanded to each individual UPN but I am fine with doing something like this:

    UPN License
    —— —————
    test@domain.com domain:MCOMEETADV_GOV
    test@domain.com domain:VISIOCLIENT_GOV
    test@domain.com Domain:PROJECTCLIENT_GOV

    I think I have some code I hijacked from the internet to give me AD group memberships report that does something similar to this, I will look into it. I'm still very new to custom objects, at least ones that require foreach statements 😉

  • #84317

    Don Jones
    Keymaster

    Yeah, so keep in mind that PowerShell wants to deal with objects. It's not a report generator. You could use Group-Object or something to re-group things by user. But the output above is what you're going to need to be happy with ;).

    # Assuming $users has your users
    # Assuming $users.upn is the UPN, $users.licenses is licenses
    ForEach ($user in $users) {
     ForEach ($license in $user.licenses) {
      $props = @{'UPN'=$user.upn
                 'License' = $license}
      New-Object -Type PSObject -Prop $props
     }
    }
    

    Something vaguely like that.

  • #84325

    Jon
    Participant

    Thanks Don, that helped a lot. For clarification is the $license variable taking whatever value is in $user.licenses.accountskuid?

    For anyone else who might run into this in the future here is what I did to get everything working:

    $Users = Get-Msoluser -all 
    
    ForEach ($user in $users) {
     ForEach ($license in $user.licenses.accountskuid) {
      $props = @{'UPN'=$user.userprincipalname
                 'License' = $license}
      New-Object -Type PSObject -Prop $props
     }
    }
    • #84326

      Don Jones
      Keymaster

      No, it's taking whatever is in $licenses, which would be $user.licenses. You'd need to further enumerate accountskuid or any sub-properties.

    • #84329

      Jon
      Participant

      Ok, that's what I thought. In my code I enumerated further with the accountskuid and got exactly what I needed.

      Thanks again for your help.

You must be logged in to reply to this topic.