Skip to content

Commit 3e64ed9

Browse files
Sherweb code changes
1 parent 755297b commit 3e64ed9

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

Modules/CIPPCore/Public/New-CIPPAlertTemplate.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ function New-CIPPAlertTemplate {
3131
if ($InputObject -eq 'sherwebmig') {
3232
$DataHTML = ($Data | ConvertTo-Html | Out-String).Replace('<table>', ' <table class="table-modern">')
3333
$IntroText = "<p>The following licenses have not yet been found at Sherweb, and are expiring within 7 days:</p>$dataHTML"
34+
if ($data.SherwebMig -like '*buy*') {
35+
$introText = "<p>The following licenses have not yet been found at Sherweb, and are expiring within 7 days. We have started the process to automatically buy these licenses:</p>$dataHTML"
36+
}
37+
}
38+
if ($InputObject -eq 'sherwebmigfailcancel') {
39+
$DataHTML = ($Data | ConvertTo-Html | Out-String).Replace('<table>', ' <table class="table-modern">')
40+
$IntroText = "<p>The following licenses have not been cancelled due to an API error at the old provider:</p>$dataHTML"
3441
}
3542
if ($InputObject -eq 'table') {
3643
#data can be a array of strings or a string, if it is, we need to convert it to an object so it shows up nicely, that object will have one header: message.

Modules/CippExtensions/Public/Sherweb/Test-SherwebMigrationAccounts.ps1

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,57 @@ function Test-SherwebMigrationAccounts {
3131

3232
switch -wildcard ($config.migrationMethods) {
3333
'*notify*' {
34+
$Subject = "Sherweb Migration: $($TenantFilter) - $($LicencesToMigrate.Count) licenses to migrate"
3435
$HTMLContent = New-CIPPAlertTemplate -Data $LicencesToMigrate -Format 'html' -InputObject 'sherwebmig'
3536
$JSONContent = New-CIPPAlertTemplate -Data $LicencesToMigrate -Format 'json' -InputObject 'sherwebmig'
3637
Send-CIPPAlert -Type 'email' -Title $Subject -HTMLContent $HTMLContent.htmlcontent -TenantFilter $tenant -APIName 'Alerts'
3738
Send-CIPPAlert -Type 'psa' -Title $Subject -HTMLContent $HTMLContent.htmlcontent -TenantFilter $standardsTenant -APIName 'Alerts'
3839
Send-CIPPAlert -Type 'webhook' -JSONContent $JSONContent -TenantFilter $Tenant -APIName 'Alerts'
3940
}
40-
'buyAndNotify' {
41-
#Buy the licenses at Sherweb using the matching CSV.
41+
'*buy*' {
42+
try {
43+
$PotentialLicenses = Get-SherwebCatalog -TenantFilter $TenantFilter | Where-Object { $_.microsoftSkuId -in $LicencesToMigrate.SkuId -and $_.sku -like "*$($Config.migrateToLicense)" }
44+
if (!$PotentialLicenses) {
45+
throw 'cannot buy new license: no matching license found in catalog'
46+
} else {
47+
$PotentialLicenses | ForEach-Object {
48+
Set-SherwebSubscription -TenantFilter $TenantFilter -SKU $PotentialLicenses.sku -Quantity $LicencesToMigrate.TotalLicensesAtUnknownCSP
49+
}
50+
}
51+
} catch {
52+
$Subject = "Sherweb Migration: $($TenantFilter) - Failed to buy licenses."
53+
$HTMLContent = New-CIPPAlertTemplate -Data $LicencesToMigrate -Format 'html' -InputObject 'sherwebmig'
54+
$JSONContent = New-CIPPAlertTemplate -Data $LicencesToMigrate -Format 'json' -InputObject 'sherwebmig'
55+
Send-CIPPAlert -Type 'email' -Title $Subject -HTMLContent $HTMLContent.htmlcontent -TenantFilter $tenant -APIName 'Alerts'
56+
Send-CIPPAlert -Type 'psa' -Title $Subject -HTMLContent $HTMLContent.htmlcontent -TenantFilter $standardsTenant -APIName 'Alerts'
57+
Send-CIPPAlert -Type 'webhook' -JSONContent $JSONContent -TenantFilter $Tenant -APIName 'Alerts'
58+
}
59+
4260
}
43-
'buyAndCancel' {
44-
#Create HTML report for this tenant. Send to webhook/notifications/etc
45-
#Buy the licenses at Sherweb using the matching CSV.
46-
#Cancel the licenses in old vendor.
61+
'*Cancel' {
62+
try {
63+
$tenantid = (Get-Tenants -TenantFilter $TenantFilter).customerId
64+
$paxBody = @{
65+
client_id = $Config.paxclientId
66+
client_secret = $Config.paxclientSecret
67+
audience = 'https://api.pax8.com'
68+
grant_type = 'client_credentials'
69+
}
70+
$Token = Invoke-RestMethod -Uri 'https://api.pax8.com/v1/token' -Method POST -Headers $headers -ContentType 'application/json' -Body $paxBody
71+
$headers = @{ Authorization = "Bearer $($Token.access_token)" }
72+
$cancelSubList = Invoke-RestMethod -Uri "https://api.pax8.com/v1/subscriptions?page=0&size=10&status=Active&companyId=$($tenantid)" -Method GET -Headers $headers | Where-Object -Property productId -In $LicencesToMigrate.SkuId
73+
$cancelSubList | ForEach-Object {
74+
$response = Invoke-RestMethod -Uri "https://api.pax8.com/v1/subscriptions/$($_.subscriptionId)" -Method DELETE -Headers $headers -ContentType 'application/json' -Body ($body | ConvertTo-Json)
75+
}
76+
77+
} catch {
78+
$Subject = 'Sherweb Migration: Pax Migration failed'
79+
$HTMLContent = New-CIPPAlertTemplate -Data $LicencesToMigrate -Format 'html' -InputObject 'sherwebmigfailpax'
80+
$JSONContent = New-CIPPAlertTemplate -Data $LicencesToMigrate -Format 'json' -InputObject 'sherwebmigfailpax'
81+
Send-CIPPAlert -Type 'email' -Title $Subject -HTMLContent $HTMLContent.htmlcontent -TenantFilter $tenant -APIName 'Alerts'
82+
Send-CIPPAlert -Type 'psa' -Title $Subject -HTMLContent $HTMLContent.htmlcontent -TenantFilter $standardsTenant -APIName 'Alerts'
83+
Send-CIPPAlert -Type 'webhook' -JSONContent $JSONContent -TenantFilter $Tenant -APIName 'Alerts'
84+
}
4785
}
4886

4987
}

0 commit comments

Comments
 (0)