##################################### # # Student O365 Full Import For IDB # ##################################### # # Global Settings #Start new session $studLog = "C:\LOG\studLog.txt" $invokeResult = Invoke-Command -ComputerName localhost -ArgumentList $studLog -ScriptBlock { param($studLog) "$(Get-Date -format 'u'): Script start" | Out-File $studLog -append $tenant = "etfkstuddemo2.onmicrosoft.com" $admin = "admin@" + $tenant $password=ConvertTo-SecureString "pass@word1" -AsPlainText -Force $mycreds = New-Object -TypeName System.Management.Automation.PSCredential $admin,$password $threshold = 5001 # # ##################################### Connect-MsolService -Credential $mycreds "$(Get-Date -format 'u'): Get users start" | Out-File $studLog -append $iter = 1 while ($iter -lt 5) { $O365Users = $null [Array]$O365Users = Get-MsolUser -All -ErrorVariable err -WarningVariable warn $usercount = $O365Users.length #$logfile="C:\LOG\O365studcounts.txt" #$file="C:\LOG\O365Stud.txt" #get-date | Out-file $file #get-date | Out-file $logfile -append #"Number of users "| Out-file $file -append #$usercount | Out-file $file -append #"Number of users "| Out-file $logfile -append #$usercount | Out-file $logfile -append if ($usercount -gt $threshold) { $iter = 6 } $iter++ sleep 20 } "$(Get-Date -format 'u'): Get users end" | Out-File $studLog -append if ($usercount -lt $threshold) { Throw "The MSOL-GetUser command did not get everyone warning: $warn error: $err" } "$(Get-Date -format 'u'): Process users start" | Out-File $studLog -append $results = @() Foreach($msoluser in $O365Users) { $UsrAlias = $msoluser.UserPrincipalName.ToLower().replace("@etfkstuddemo2.onmicrosoft.com","") $lic = $msoluser.Licenses.AccountSku.SkuPartNumber -join ", " $entity = @{} $entity['ID'] = $UsrAlias; if($lic){ $entity['License'] = $lic; $disoptarr = ($msoluser.Licenses.ServiceStatus).Where({$_.ProvisioningStatus -eq "Disabled"}).ServicePlan.ServiceName; $entity['DisabledOptions'] = $disoptarr -join ", " } $entity['isLicensed'] = $msoluser.isLicensed $results += $entity $out="$UsrAlias,$lic,$entity['DisabledOptions'],$entity['isLicensed']" #$out | Out-file $file -append } "$(Get-Date -format 'u'): Process users end" | Out-File $studLog -append return $results } "$(Get-Date -format 'u'): Build entities start" | Out-File $studLog -append Foreach($r in $invokeResult) { $entity = $entities.Create() Foreach($enum in $r.GetEnumerator()) { $entity[$enum.key] = $enum.value } $entity.Commit() } "$(Get-Date -format 'u'): Build entities end" | Out-File $studLog -append "$(Get-Date -format 'u'): Script end" | Out-File $studLog -append