Skip to content

Commit 1b62ca5

Browse files
add autoClaim blocking.
1 parent 7f97482 commit 1b62ca5

1 file changed

Lines changed: 57 additions & 3 deletions

File tree

Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardDisableSelfServiceLicenses.ps1

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function Invoke-CIPPStandardDisableSelfServiceLicenses {
1717
Prevents employees from purchasing Microsoft 365 licenses independently, ensuring all software acquisitions go through proper procurement channels. This maintains budget control, prevents unauthorized spending, and ensures compliance with corporate licensing agreements.
1818
ADDEDCOMPONENT
1919
{"type":"textField","name":"standards.DisableSelfServiceLicenses.Exclusions","label":"License Ids to exclude from this standard","required":false}
20+
{"type":"switch","name":"standards.DisableSelfServiceLicenses.DisableTrials","label":"Disable starting trials on behalf of your organization"}
2021
IMPACT
2122
Medium Impact
2223
ADDEDDATE
@@ -51,7 +52,27 @@ function Invoke-CIPPStandardDisableSelfServiceLicenses {
5152
$exclusions = $settings.Exclusions -split (',')
5253
}
5354

54-
$CurrentValues = $selfServiceItems | Select-Object -Property productName, productId, policyValue
55+
$CurrentValues = [System.Collections.Generic.List[PSCustomObject]]::new()
56+
foreach ($Item in $selfServiceItems) {
57+
$CurrentValues.Add([PSCustomObject]@{
58+
productName = $Item.productName
59+
productId = $Item.productId
60+
policyValue = $Item.policyValue
61+
})
62+
}
63+
64+
if ($Settings.DisableTrials) {
65+
try {
66+
$AutoClaimPolicy = New-GraphGetRequest -scope 'https://admin.microsoft.com/.default' -TenantID $Tenant -Uri 'https://admin.microsoft.com/fd/m365licensing/v1/policies/autoclaim'
67+
$CurrentValues.Add([PSCustomObject]@{
68+
productName = 'Trial Autoclaim'
69+
productId = 'autoclaim'
70+
policyValue = $AutoClaimPolicy.policyValue
71+
})
72+
} catch {
73+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to retrieve trial autoclaim policy: $($_.Exception.Message)" -sev Error
74+
}
75+
}
5576

5677
$ExpectedValues = [System.Collections.Generic.List[PSCustomObject]]::new()
5778

@@ -72,6 +93,14 @@ function Invoke-CIPPStandardDisableSelfServiceLicenses {
7293
})
7394
}
7495

96+
if ($Settings.DisableTrials) {
97+
$ExpectedValues.Add([PSCustomObject]@{
98+
productName = 'Trial Autoclaim'
99+
productId = 'autoclaim'
100+
policyValue = 'Disabled'
101+
})
102+
}
103+
75104
if ($settings.remediate) {
76105

77106
$Compare = Compare-Object -ReferenceObject $ExpectedValues -DifferenceObject $CurrentValues -Property productName, productId, policyValue
@@ -90,7 +119,12 @@ function Invoke-CIPPStandardDisableSelfServiceLicenses {
90119
$currentValue = if ($currentItem) { $currentItem.policyValue } else { "<unknown>" }
91120

92121
$body = @{ policyValue = $Item.policyValue } | ConvertTo-Json -Compress
93-
New-GraphPOSTRequest -scope 'aeb86249-8ea3-49e2-900b-54cc8e308f85/.default' -uri "https://licensing.m365.microsoft.com/v1.0/policies/AllowSelfServicePurchase/products/$($Item.productId)" -tenantid $Tenant -body $body -type PUT
122+
123+
if ($Item.productId -eq 'autoclaim') {
124+
New-GraphPostRequest -scope 'https://admin.microsoft.com/.default' -TenantID $Tenant -Uri 'https://admin.microsoft.com/fd/m365licensing/v1/policies/autoclaim' -Body $body
125+
} else {
126+
New-GraphPOSTRequest -scope 'aeb86249-8ea3-49e2-900b-54cc8e308f85/.default' -uri "https://licensing.m365.microsoft.com/v1.0/policies/AllowSelfServicePurchase/products/$($Item.productId)" -tenantid $Tenant -body $body -type PUT
127+
}
94128

95129
Write-LogMessage -API 'Standards' -tenant $tenant -message "Changed Self Service status for product '$($Item.productName) - $($Item.productId)' from '$currentValue' to '$($Item.policyValue)'" -sev Info
96130
} catch {
@@ -99,7 +133,27 @@ function Invoke-CIPPStandardDisableSelfServiceLicenses {
99133
}
100134
}
101135

102-
$CurrentValues = (New-GraphGETRequest -scope 'aeb86249-8ea3-49e2-900b-54cc8e308f85/.default' -uri 'https://licensing.m365.microsoft.com/v1.0/policies/AllowSelfServicePurchase/products' -tenantid $Tenant).items | Select-Object -Property productName, productId, policyValue
136+
$CurrentValues = [System.Collections.Generic.List[PSCustomObject]]::new()
137+
$refreshedItems = (New-GraphGETRequest -scope 'aeb86249-8ea3-49e2-900b-54cc8e308f85/.default' -uri 'https://licensing.m365.microsoft.com/v1.0/policies/AllowSelfServicePurchase/products' -tenantid $Tenant).items
138+
foreach ($Item in $refreshedItems) {
139+
$CurrentValues.Add([PSCustomObject]@{
140+
productName = $Item.productName
141+
productId = $Item.productId
142+
policyValue = $Item.policyValue
143+
})
144+
}
145+
if ($Settings.DisableTrials) {
146+
try {
147+
$AutoClaimPolicy = New-GraphGetRequest -scope 'https://admin.microsoft.com/.default' -TenantID $Tenant -Uri 'https://admin.microsoft.com/fd/m365licensing/v1/policies/autoclaim'
148+
$CurrentValues.Add([PSCustomObject]@{
149+
productName = 'Trial Autoclaim'
150+
productId = 'autoclaim'
151+
policyValue = $AutoClaimPolicy.policyValue
152+
})
153+
} catch {
154+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to retrieve trial autoclaim policy after remediation: $($_.Exception.Message)" -sev Error
155+
}
156+
}
103157
}
104158

105159
if ($Settings.alert) {

0 commit comments

Comments
 (0)