none
Storing the variable values from Foreach loop in single variable

    Question

  • I have a situation where i wanted to take users attribute and store the values in Single variable so that i can pipe it out for other function. But i couldn't do that. Help please.

    Here is my code:

    $LicenseUsers = Get-MSOLuser | where {$_.Islicensed -eq "True"}
    $Users = ($licenseusers).Userprincipalname
    foreach ($User in  $users)
    {
    $usagelocation = Get-msoluser -userprincipalname $user | Select Usagelocation
    }

    I want $usagelocation to have values like below instead of showing only the last one value.

    Usagelocation1

    Usagelocation2

    Usagelocation3

    I have tried using $usagelocation+= Which adds all the value in single row instead of multiple values.

    Thursday, January 10, 2019 3:06 PM

Answers

  • please try this. $array will contain your desired users samaccountname  

    $Users = Get-MSOLuser -All
    $array = @()
    foreach ($User in  $users)
    { $ServiceStatus = $user.licenses.ServiceStatus
      $ProvisionName = $ServiceStatus | Where {$_.ServicePlan.ServiceName -eq "MFA_PREMIUM" }
      If($ProvisionName.ProvisioningStatus -eq "Success")
         { $array += $user.Userprincipalname }
    }
    $array

    • Marked as answer by suresh.tippan Friday, January 11, 2019 2:52 PM
    Friday, January 11, 2019 1:25 PM

All replies

  • $LicenseUsers = Get-MSOLuser | where{$_.Islicensed}
    $Users = ($licenseusers).Userprincipalname
    
    $usagelocation = foreach($User in  $users){
         Get-msoluser -userprincipalname $user | Select -expand Usagelocation
    } 
    


    \_(ツ)_/

    Thursday, January 10, 2019 3:48 PM
  • This is really all you need:

    $usagelocation = Get-MSOLuser |
        Where-Object{ $_.Islicensed } |
        Select-Object -ExpandProperty Usagelocation


    \_(ツ)_/

    Thursday, January 10, 2019 3:50 PM
  • Hi,

    Thanks a lot for quick reply. This worked. 

    However the exact situation i have is little different. I am trying to find the users with particular service assigned and have the provisioning status as Success using below code.

    $LicenseUsers = Get-MSOLuser | where {$_.Islicensed -eq "True"}
    $Users = ($licenseusers).Userprincipalname
    $Userprincipalname = foreach ($User in  $users)
    {
    Get-Msoluser -userprincipalname $user | where {$_.$user.Licenses.servicestatus.ServicePlan -eq "MFA_PREMIUM" -AND $_.$user.Licenses.servicestatus.ProvisioningStatus -eq "Success"} | select -expand UserPrincipalname
    }
    $Userprincipalname

    I am expecting $userprincipalname to give each user matching this condition in the format below

    Userprincipalname1

    Userprincipalname2

    Userprincipalname3

    But the output is empty and i am not getting any value. Could you help me how to achieve this? i tried the below code as well

    $Users = Get-MSOLuser -All
    $userprincipalname = foreach ($User in  $users)
    {
    $ServiceStatus = $user.licenses.ServiceStatus
    $ProvisionName = $ServiceStatus | Where {$_.ServicePlan.ServiceName -eq "MFA_PREMIUM" }
    If($ProvisionName.ProvisioningStatus -eq "Success")
    {$user | Select Userprincipalname | Export-CSv Mfausers.csv -Append} 
    }

    $Userprincipalname

    In this i could successfully export the result into CSV. But do not want to export the results and directly store in variable and use it in next function.


    Friday, January 11, 2019 1:12 PM
  • please try this. $array will contain your desired users samaccountname  

    $Users = Get-MSOLuser -All
    $array = @()
    foreach ($User in  $users)
    { $ServiceStatus = $user.licenses.ServiceStatus
      $ProvisionName = $ServiceStatus | Where {$_.ServicePlan.ServiceName -eq "MFA_PREMIUM" }
      If($ProvisionName.ProvisioningStatus -eq "Success")
         { $array += $user.Userprincipalname }
    }
    $array

    • Marked as answer by suresh.tippan Friday, January 11, 2019 2:52 PM
    Friday, January 11, 2019 1:25 PM
  • Thank you asharma.
    Friday, January 11, 2019 2:52 PM