Created
September 14, 2016 15:05
-
-
Save anonymous/2a55a41c250c161767e06f8742b4cf8a 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
workflow MSOL-SyncLicenses { | |
# Get SCSM Server | |
$scsmServer = "<SCSM Workflow Server>" | |
$msolCredential = Get-AutomationPSCredential -Name '<SERVICE ACCOUNT>' | |
$data = InlineScript { | |
# | |
if(get-module MSOnline -ListAvailable){ | |
import-module MSOnline | |
} | |
else { | |
write-error "Azure AD PowerShell module not installed!" | |
} | |
## Variables | |
#if (!$msolCredential){ | |
# $msolCredential = get-credential -Message "Please supply an Azure Active Directory credential:" | |
#} | |
## Connect to MSOL | |
try { | |
Connect-MsolService -credential $using:msolCredential | |
} | |
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 | |
$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 = $using:scsmServer | |
$SMDefaultComputer = $scsmServer | |
# User Objects | |
$userClass = get-scsmclass System.Domain.User$ | |
$userObjects = get-scsmobject -class $userClass #-Filter "Enabled -eq $true" | |
# License Objects | |
$licenseClass = get-scsmclass Cireson.AssetManagement.Licence$ | |
$licenseTypeTemp = get-scsmenumeration Cireson.AssetManagement.LicenceTypeEnum.Temporary$ | |
# License Relationships | |
$licenseUsersRel = Get-SCSMRelationshipClass Cireson.AssetManagement.LicenceHasAssignedUsers$ | |
# Existing License - User Assignments | |
$existingLicenseAssignments = Get-SCSMRelationshipObject -Relationship $licenseUsersRel | |
foreach ($obj in $existingLicenseAssignments){ | |
if ($obj.SourceObject.DisplayName -like "*Office 365 Plan*"){ | |
# Remove the user assignment. Should be repopulated below if still exists. | |
Remove-SCSMRelationshipObject -SMObject $obj -Verbose | |
} | |
} | |
foreach ($sku in $skuData.Keys){ | |
# | |
Write-Output "[INFO] Processing $sku" | |
$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 | |
if ($userObj -ne $null){ | |
try { | |
New-SCSMRelationshipObject -Relationship $licenseUsersRel -Source $obj -Target $userObj -Bulk #-Verbose | |
} | |
catch { | |
write-error "An error occurred creating the relationship from $obj.DisplayName to $userObj.DisplayName :: $($_)" -ErrorAction Continue | |
} | |
} | |
} | |
} | |
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 | |
} | |
} | |
$data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment