Link to home
Start Free TrialLog in
Avatar of Ron Shorts
Ron ShortsFlag for United States of America

asked on

Script to get users instead of processing through input file help

Hi Experts,

I need some help with getting the script to grab all users with a mailbox or smtp address in the script below.  

Now, I have to first export all users separate, filter out those without mail, and use the file with samaccountname to run the script.

This also takes an enormous amount of time to run, so any suggestions or tips are apprecaited!


I first connect to MSOL, Exchange online in shell, then run:

$Date = Get-Date
$strDate = "{0}{1:d2}{2:d2}_{3:d2}{4:d2}" -f $date.year,$date.month,$date.day,$date.hour,$date.minute

$strOutputFile = "C:\temp\licensereport.csv"

$strInputFile = "C:\temp\Licensing\input.txt"
$strSANs=Get-Content $strInputFile 

$strAccountHeader = "SamAccountName,DisplayName,UserPrincipalName,DistinguishedName,Enabled,LastLogonDate,HomeDirectory ,ScriptPath"

$strOrganizationHeader = "Company,Department,Title,Manager"

$strCommunicationsHeader = "TelephoneNumber,OfficePhone,EmailAddress,msExchRecipientTypeDetails,msRTCSIP-PrimaryUserAddress"

$strMSOL = "IsLicensed,BlockCredential,AccountSKUIds,LicenseReconciliationNeeded"

$strHeader = $strAccountHeader +","+ $strOrganizationHeader +","+ $strCommunicationsHeader +","+ $strMSOL

    Add-Content $strOutputFile $strHeader
    Write-output $strHeader

foreach ($strSAN in $strSANs)
{
$ADUser = $null
$ADUser = Get-ADUser -Identity $strSAN -properties *

$strAccount = $null
$strAccount = $strSAN +","+ '"'+$ADUser.DisplayName+'"' +","+ $ADUser.UserPrincipalName +","+'"' + $ADUser.DistinguishedName + '"' +","+ $ADUser.Enabled +","+ $ADUser.LastLogonDate +","+ $ADUser.HomeDirectory +","+ $ADUser.ScriptPath

$strOrganization = $null
$strOrganization = '"' + $ADUser.Company +'"' +","+ '"' + $ADUser.Department + '"' +","+ '"' + $ADUser.Title + '"' +","+ '"'+$ADUser.Manager+'"'

$strCommunications = $null
$strCommunications = $ADUser.TelephoneNumber +","+ $ADUser.OfficePhone +","+ $ADUser.EmailAddress +","+ $ADUser.msExchRecipientTypeDetails +","+ $ADUser.'msRTCSIP-PrimaryUserAddress'

$strADUser = $strAccount +","+ $strOrganization +","+ $strCommunications 

$strUPN = $null
$strUPN = $aduser.UserPrincipalName


if ($strUPN -eq $null) {
    Add-Content $strOutputFile $strADUser
    Write-output $strADUser
} else
{
$msolusers = Get-MsolUser -All
$msolusers = $null
$msolusers = Get-MsolUser -UserPrincipalName $strUPN
if ($msolusers -eq $null) {
    Add-Content $strOutputFile $strSAN
    Write-output $strSAN
}
foreach ($msoluser in $msolusers) {
	$strAccountSKUIds = $null
    $strAccountSKUIds = $msoluser.Licenses.AccountSkuId -join ","
	$strAccountSKUIds = '"' + $strAccountSKUIds + '"'

    $strout = $strADUser +","+ $msoluser.IsLicensed +","+ $msoluser.BlockCredential +","+ $strAccountSKUIds +","+ $msoluser.LicenseReconciliationNeeded
    Add-Content $strOutputFile $strout
    Write-output $strout
    }

}

}

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

you obviously have modified your script as you have to
Connect-msolservice before any call to get-msoluser
Avatar of Ron Shorts

ASKER

@David - yes, I make the connection before running this script.  Thanks
Have a look at my article from http://emoreau.com/Entries/Articles/2016/11/PowerShell-Get-O365-mailboxes-statistics.aspx

It shows the licence of each user.
@Eric - excellent write up and script.  I have upwards of 30k users though and this is more info than I need and would take a long time to run I believe.  My main goal is to straighten out licensing as we are doing licensing based on group membership in a hybrid environment and have multiple licenses assigned, some mailboxes without licenses that have a mailbox etc.,  Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Coralon
Coralon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial