Created
September 13, 2016 15:10
-
-
Save anonymous/20a6354cdcc884c6b3b3f3c2bb4efe79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(get-module MSOnline -ListAvailable){ | |
import-module MSOnline | |
} | |
else { | |
write-error "Azure AD PowerShell module not installed!" | |
} | |
# Connect to O365 | |
try { | |
Connect-MsolService | |
} | |
catch { | |
write-error "An error occurred connecting to MSOnline : $($_)" | |
} | |
## Get all O365 Account SKU Data | |
$skuData = new-object System.Collections.Hashtable | |
$allSKU = Get-MsolAccountSku | |
foreach ($sku in $allSKU){ | |
# | |
$temp = new-object PSObject | |
$temp | add-member -NotePropertyName SkuID -NotePropertyValue $sku.SkuId | |
$temp | add-member -NotePropertyName SkuPartNumber -NotePropertyValue $sku.SkuPartNumber | |
$temp | add-member -NotePropertyName Total -NotePropertyValue $sku.ActiveUnits | |
$temp | add-member -NotePropertyName Unassigned -NotePropertyValue ([int]$sku.ActiveUnits-[int]$sku.ConsumedUnits) | |
$temp | add-member -NotePropertyName Warning -NotePropertyValue $sku.WarningUnits | |
$temp | Add-Member -NotePropertyName Target -NotePropertyValue $sku.TargetClass | |
$skuData.Add($sku.SkuPartNumber,$temp) | |
} | |
## Get all O365 SKU Assignments | |
$allUsers = Get-MsolUser -All -EnabledFilter EnabledOnly | Where-Object -FilterScript {($_.UserPrincipalName -like "*@tld.org")} | |
$skuAssignments = new-object PSObject | |
foreach ($sku in $skuData.Keys){ | |
$skuAssignments | Add-Member -NotePropertyName $sku -NotePropertyValue (new-object System.Collections.ArrayList) | |
foreach ($user in $allUsers){ | |
$assignedUsers = new-object System.Collections.ArrayList | |
# Create temp object to hold results | |
foreach ($license in $user.Licenses){ | |
if ($license.AccountSku.SkuPartNumber -eq $sku){ | |
# | |
$assignedUsers += $user.UserPrincipalName | |
} | |
} | |
$skuAssignments."$($sku)" += $assignedUsers | |
} | |
} | |
# SCSM Server Information | |
$scsmServer = "SCSM SERVER" | |
$SMDefaultComputer = $scsmServer | |
# User Objects | |
$userClass = get-scsmclass System.Domain.User$ | |
$userObjects = get-scsmobject -class $userClass | |
# License Objects | |
$licenseClass = get-scsmclass Cireson.AssetManagement.Licence$ | |
$licenseTypeTemp = get-scsmenumeration Cireson.AssetManagement.LicenceTypeEnum.Temporary$ | |
# License Relationships | |
$licenseUsersRel = Get-SCSMRelationshipClass Cireson.AssetManagement.LicenceHasAssignedUsers$ | |
foreach ($sku in $skuData.Keys){ | |
# | |
$data = $skuData.$sku | |
if ($data.Target -eq "User"){ | |
[string]$licenseName = "Office 365 Plan - $($data.SkuPartNumber) - $($data.SkuID)" | |
$licenseType = $licenseTypeTemp | |
[bool]$licenseCountUsers = $true | |
[bool]$licenseCountComputers = $false | |
[int]$AllocatedSeats = $data.Total | |
[hashtable]$properties = @{ "Name"=$licenseName;"AllocatedSeats"=$AllocatedSeats;"Type"=$licenseTypeTemp;"CountAssignedUsers"=$licenseCountUsers;"CountAssignedComputers"=$licenseCountComputers } | |
$obj = get-scsmobject -Class $licenseClass -Filter "DisplayName -eq $($licenseName)" | |
if ($obj){ | |
# Update | |
Write-Verbose "License objects exists. Updating..." | |
# Remove 'Name' key from hashtable for update actions... | |
$properties.Remove("Name") | |
Set-SCSMObject -SMObject $obj -PropertyHashtable $properties #-Verbose | |
# Update assignments | |
$userAssignments = $skuAssignments.$sku | |
foreach ($user in $userAssignments){ | |
# | |
$userObj = $userObjects | where-object -Property UPN -EQ -Value $user | |
New-SCSMRelationshipObject -Relationship $licenseUsersRel -Source $obj -Target $userObj -Bulk #-Verbose | |
} | |
} | |
elseif (!$obj){ | |
# Create | |
Write-Verbose "License objects doesn't exist. Creating..." | |
try { | |
New-SCSMObject -Class $licenseClass -PropertyHashtable $properties -Verbose | |
} | |
catch { | |
Write-Error "An error occurred creating License $($licenseName) : $($_)" -ErrorAction Continue | |
} | |
} | |
} | |
#break | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment