Results Out Of Order

Frequent Contributor

I have a short function:


Write-Host "Missing ATP_ENTERPRISE license"
Get-MsolUser -All | sort DisplayName | ? {$_.Licenses.AccountSku.SkuPartNumber -contains "ENTERPRISEPACK"} | ? {$_.Licenses.AccountSku.SkuPartNumber -notcontains "ATP_ENTERPRISE"}
Write-Host "Missing ENTERPRISEPACK license"
Get-MsolUser -All | sort DisplayName | ? {$_.Licenses.AccountSku.SkuPartNumber -contains "ATP_ENTERPRISE"} | ? {$_.Licenses.AccountSku.SkuPartNumber -notcontains "ENTERPRISEPACK"}


When I run it, it displays the Write-Host lines ("Missing ATP_ENTERPRISE license", "Missing ENTERPRISEPACK license") then it says Press Any Key To Continue for the pause statement.  When I press return, it then displays the missing ATP_ENTERPRISE license results. 


I have this script in a function which I call from the same script in a menu system I set up. So, at the end of the script I have a menu and when they choose a number of the menu item it runs the function.  The results appear after the Clear-Host command gets run.  I assume somehow the results are waiting to be passed back to the main part of the script.  If I want the results to be presented within the Function, what do I need to do?:


While ($Main -ne "EXIT")
Write-Host "Office 365 Reports"
Write-Host " 8) Missing ATP_ENTERPRISE License"
Write-Host "LOGOFF) Logout and Exit"
Write-Host "EXIT) Exit"
$Main=Read-Host "Option"
if ($Main -eq "8") {MissingATP}
if ($Main -eq "LOGOFF") {Get-PSSession | Remove-PSSession}
if ($Main -eq "EXIT") {EXIT}



Office 365 Reports
UserPrincipalName                   DisplayName       isLicensed
-----------------                   -----------       ---------- Smith, Joe True     Jones, Mary True

** Mailboxes **

 1) Enable Auditing
 2) License Report
 3) Mailbox Report
 4) Inbox Rules and Delegates Report
 5) Email Forwarding Report
 6) Last Login Report
 7) Skype Policies
 8) Missing ATP_ENTERPRISE License

LOGOFF) Logout and Exit
EXIT) Exit



3 Replies
Any ideas?
Best Response confirmed by Zeff Wheelock (Frequent Contributor)

Hi Zeff,


You can output the results of Get-Msoluser to a variable then use conditionals to display those results. Here is a rough example:


function GetFunc {
$ATP = Get-MsolUser -All | ? {$_.Licenses.AccountSku.SkuPartNumber -contains "ENTERPRISEPACK"} | ? {$_.Licenses.AccountSku.SkuPartNumber -notcontains "ATP_ENTERPRISE"}
$Enterprise = Get-MsolUser -All | ? {$_.Licenses.AccountSku.SkuPartNumber -contains "ATP_ENTERPRISE"} | ? {$_.Licenses.AccountSku.SkuPartNumber -notcontains "ENTERPRISEPACK"}
if($ATP -ne $null){
Write-Host "-> Missing ATP_ENTERPRISE license"
ForEach ($User in $ATP) {Write-Host $User.UserPrincipalName}
if($Enterprise -ne $null){
Write-Host "-> Missing ENTERPRISEPACK license"
ForEach ($User in $Enterprise) {Write-Host $User.UserPrincipalName}


This is just the first thing that came to mind, there is probably a more elegant solution out there. Let me know if this helps!

That seemed to work well!  Thanks!