SOLVED

Results Out Of Order

%3CLINGO-SUB%20id%3D%22lingo-sub-259810%22%20slang%3D%22en-US%22%3EResults%20Out%20Of%20Order%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-259810%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20short%20function%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EWrite-Host%20%22Missing%20ATP_ENTERPRISE%20license%22%3CBR%20%2F%3EGet-MsolUser%20-All%20%7C%20sort%20DisplayName%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-contains%20%22ENTERPRISEPACK%22%7D%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-notcontains%20%22ATP_ENTERPRISE%22%7D%3CBR%20%2F%3EWrite-Host%3CBR%20%2F%3EWrite-Host%20%22Missing%20ENTERPRISEPACK%20license%22%3CBR%20%2F%3EGet-MsolUser%20-All%20%7C%20sort%20DisplayName%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-contains%20%22ATP_ENTERPRISE%22%7D%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-notcontains%20%22ENTERPRISEPACK%22%7D%3CBR%20%2F%3EPause%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20run%20it%2C%20it%20displays%20the%20Write-Host%20lines%20(%22Missing%20ATP_ENTERPRISE%20license%22%2C%20%22Missing%20ENTERPRISEPACK%20license%22)%20then%20it%20says%20Press%20Any%20Key%20To%20Continue%20for%20the%20pause%20statement.%26nbsp%3B%20When%20I%20press%20return%2C%20it%20then%20displays%20the%20missing%20ATP_ENTERPRISE%20license%20results.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20this%20script%20in%20a%20function%20which%20I%20call%20from%20the%20same%20script%20in%20a%20menu%20system%20I%20set%20up.%20So%2C%20at%20the%20end%20of%20the%20script%20I%20have%20a%20menu%20and%20when%20they%20choose%20a%20number%20of%20the%20menu%20item%20it%20runs%20the%20function.%26nbsp%3B%20The%20results%20appear%20after%20the%20Clear-Host%20command%20gets%20run.%26nbsp%3B%20I%20assume%20somehow%20the%20results%20are%20waiting%20to%20be%20passed%20back%20to%20the%20main%20part%20of%20the%20script.%26nbsp%3B%20If%20I%20want%20the%20results%20to%20be%20presented%20within%20the%20Function%2C%20what%20do%20I%20need%20to%20do%3F%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EWhile%20(%24Main%20-ne%20%22EXIT%22)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EClear-Host%3B%3CBR%20%2F%3EWrite-Host%20%22Office%20365%20Reports%22%3CBR%20%2F%3EDraw-Box(%22Mailboxes%22)%3CBR%20%2F%3E...%3CBR%20%2F%3EWrite-Host%20%22%208)%3C%2Fimg%3E%20Missing%20ATP_ENTERPRISE%20License%22%3CBR%20%2F%3E%22%22%3CBR%20%2F%3EWrite-Host%20%22LOGOFF)%20Logout%20and%20Exit%22%3CBR%20%2F%3EWrite-Host%20%22EXIT)%20Exit%22%3CBR%20%2F%3E%24Main%3D%22%22%3CBR%20%2F%3E%22%22%3CBR%20%2F%3E%24Main%3DRead-Host%20%22Option%22%3CBR%20%2F%3E...%3CBR%20%2F%3Eif%20(%24Main%20-eq%20%228%22)%20%7BMissingATP%7D%3CBR%20%2F%3Eif%20(%24Main%20-eq%20%22LOGOFF%22)%20%7BGet-PSSession%20%7C%20Remove-PSSession%7D%3CBR%20%2F%3Eif%20(%24Main%20-eq%20%22EXIT%22)%20%7BEXIT%7D%3CBR%20%2F%3E%7D%3C%2FPRE%3E%3CP%3EResults%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EOffice%20365%20Reports%3CBR%20%2F%3EUserPrincipalName%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20DisplayName%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20isLicensed%3CBR%20%2F%3E-----------------%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-----------%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20----------%3CBR%20%2F%3EJoe_Smith%40Domain.com%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Smith%2C%20Joe%20%20%20%20%20%20%20%20True%3CBR%20%2F%3EMary_Jones%40Domain.com%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%20%20%20%20%20%20%20%20%20%20Jones%2C%20Mary%20%20%20%20%20%20%20True%3CBR%20%2F%3E%3CBR%20%2F%3E***************%3CBR%20%2F%3E**%20Mailboxes%20**%3CBR%20%2F%3E***************%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B1)%20Enable%20Auditing%3CBR%20%2F%3E%26nbsp%3B2)%20License%20Report%3CBR%20%2F%3E%26nbsp%3B3)%20Mailbox%20Report%3CBR%20%2F%3E%26nbsp%3B4)%20Inbox%20Rules%20and%20Delegates%20Report%3CBR%20%2F%3E%26nbsp%3B5)%20Email%20Forwarding%20Report%3CBR%20%2F%3E%26nbsp%3B6)%20Last%20Login%20Report%3CBR%20%2F%3E%26nbsp%3B7)%20Skype%20Policies%3CBR%20%2F%3E%26nbsp%3B8)%3C%2Fimg%3E%20Missing%20ATP_ENTERPRISE%20License%3CBR%20%2F%3E%3CBR%20%2F%3ELOGOFF)%20Logout%20and%20Exit%3CBR%20%2F%3EEXIT)%20Exit%3CBR%20%2F%3E%3CBR%20%2F%3EOption%3A%20%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-259810%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-270124%22%20slang%3D%22en-US%22%3ERe%3A%20Results%20Out%20Of%20Order%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-270124%22%20slang%3D%22en-US%22%3E%3CP%3EThat%20seemed%20to%20work%20well!%26nbsp%3B%20Thanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-264957%22%20slang%3D%22en-US%22%3ERe%3A%20Results%20Out%20Of%20Order%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-264957%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Zeff%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20output%20the%20results%20of%20Get-Msoluser%20to%20a%20variable%20then%20use%20conditionals%20to%20display%20those%20results.%20Here%20is%20a%20rough%20example%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Efunction%20GetFunc%20%7B%3CBR%20%2F%3E%20%24ATP%20%3D%20Get-MsolUser%20-All%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-contains%20%22ENTERPRISEPACK%22%7D%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-notcontains%20%22ATP_ENTERPRISE%22%7D%3CBR%20%2F%3E%20%24Enterprise%20%3D%20Get-MsolUser%20-All%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-contains%20%22ATP_ENTERPRISE%22%7D%20%7C%20%3F%20%7B%24_.Licenses.AccountSku.SkuPartNumber%20-notcontains%20%22ENTERPRISEPACK%22%7D%3CBR%20%2F%3E%20if(%24ATP%20-ne%20%24null)%7B%3CBR%20%2F%3E%20Write-Host%20%22-%26gt%3B%20Missing%20ATP_ENTERPRISE%20license%22%3CBR%20%2F%3E%20ForEach%20(%24User%20in%20%24ATP)%20%7BWrite-Host%20%24User.UserPrincipalName%7D%3CBR%20%2F%3E%20Write-Host%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3E%20if(%24Enterprise%20-ne%20%24null)%7B%3CBR%20%2F%3E%20Write-Host%20%22-%26gt%3B%20Missing%20ENTERPRISEPACK%20license%22%3CBR%20%2F%3E%20ForEach%20(%24User%20in%20%24Enterprise)%20%7BWrite-Host%20%24User.UserPrincipalName%7D%3CBR%20%2F%3E%20Write-Host%3CBR%20%2F%3E%20%7D%3CBR%20%2F%3E%20Pause%3CBR%20%2F%3E%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20just%20the%20first%20thing%20that%20came%20to%20mind%2C%20there%20is%20probably%20a%20more%20elegant%20solution%20out%20there.%20Let%20me%20know%20if%20this%20helps!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-263536%22%20slang%3D%22en-US%22%3ERE%3A%20Results%20Out%20Of%20Order%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-263536%22%20slang%3D%22en-US%22%3EAny%20ideas%3F%3C%2FLINGO-BODY%3E
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
Write-Host "Missing ENTERPRISEPACK license"
Get-MsolUser -All | sort DisplayName | ? {$_.Licenses.AccountSku.SkuPartNumber -contains "ATP_ENTERPRISE"} | ? {$_.Licenses.AccountSku.SkuPartNumber -notcontains "ENTERPRISEPACK"}
Pause

 

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")
{
Clear-Host;
Write-Host "Office 365 Reports"
Draw-Box("Mailboxes")
...
Write-Host " 8) Missing ATP_ENTERPRISE License"
""
Write-Host "LOGOFF) Logout and Exit"
Write-Host "EXIT) Exit"
$Main=""
""
$Main=Read-Host "Option"
...
if ($Main -eq "8") {MissingATP}
if ($Main -eq "LOGOFF") {Get-PSSession | Remove-PSSession}
if ($Main -eq "EXIT") {EXIT}
}

Results:

 

Office 365 Reports
UserPrincipalName                   DisplayName       isLicensed
-----------------                   -----------       ----------
Joe_Smith@Domain.com Smith, Joe True
Mary_Jones@Domain.com     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

Option:

 

3 Replies
Any ideas?
best response confirmed by Zeff Wheelock (Frequent Contributor)
Solution

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}
Write-Host
}
if($Enterprise -ne $null){
Write-Host "-> Missing ENTERPRISEPACK license"
ForEach ($User in $Enterprise) {Write-Host $User.UserPrincipalName}
Write-Host
}
Pause
}

 

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!