3333 Optional. Enables the ESU policy if the value is "Yes" or disables it if the value is "No". To enable, the license type must be "Paid" or "PAYG"
3434
3535. PARAMETER Force
36- Optional. Forces the chnahge of the license type to the specified value on all installed extensions. If not forced, the changes will apply only to the extensions where the license type is undefined.
36+ Optional. Forces the change of the license type to the specified value on all installed extensions. If not forced, the changes will apply only to the extensions where the license type is undefined.
3737
3838. PARAMETER ExclusionTags
3939 Optional. If specified, excludes the resources that have this tag assigned.
@@ -166,10 +166,19 @@ if ($UseManagedIdentity) {
166166
167167# Ensure the required modules are imported
168168try {
169- Import-Module AzureAD - UseWindowsPowerShell
169+ # Check if Microsoft.Graph module is installed
170+ if (-not (Get-Module - ListAvailable - Name Microsoft.Graph)) {
171+ Write-Host " Microsoft.Graph module not found. Installing..."
172+ Install-Module - Name Microsoft.Graph - Scope CurrentUser - Force
173+ }else {
174+ Write-Host " Microsoft.Graph module is already installed."
175+ }
176+
177+ # Import the module
178+ Import-Module Microsoft.Graph
170179}
171180catch {
172- Write-Output " Can't import module AzureAD "
181+ Write-Output " Can't import module Microsoft.Graph "
173182}
174183try {
175184 Import-Module Az.Accounts
@@ -233,51 +242,38 @@ foreach ($sub in $subscriptions) {
233242 {continue }
234243 }
235244
236- # Consent tag enforcement on the CSP subscriptions
237- # Add or update ConsentToRecurringPAYG setting if applicable
238- if ($ConsentToRecurringPAYG -eq " Yes" ) {
239- $isPayg = ($LicenseType -eq " PAYG" ) -or ($settings [" LicenseType" ] -eq " PAYG" )
240- if ($isPayg ) {
241- if (-not $settings.ContainsKey (" ConsentToRecurringPAYG" ) -or -not $settings [" ConsentToRecurringPAYG" ][" Consented" ]) {
242- $settings [" ConsentToRecurringPAYG" ] = @ {
243- " Consented" = $true ;
244- " ConsentTimestamp" = [DateTime ]::UtcNow.ToString(' yyyy-MM-ddTHH:mm:ss.fffZ' )
245- }
246- $WriteSettings = $true
247- }
248- }
249- }
250-
251245 Write-Output " Collecting list of resources to update"
252246
253247 $query = "
254248 resources
255- | where type == " microsoft.hybridcompute / machines "
256- | where properties.detectedProperties.mssqldiscovered == 'true '
257- "
249+ | where subscriptionId =~ ' $ ( $sub .Id ) '
250+ | where type == 'microsoft.hybridcompute/machines '
251+ | where properties.detectedProperties.mssqldiscovered == 'true' "
258252 if ($ResourceGroup ) {
259- $query += " | where resourceGroup =~ '$ ( $ResourceGroup ) '"
253+ $query += "
254+ | where resourceGroup =~ '$ResourceGroup '"
260255 }
261256
262257 if ($machineNames.Count -gt 0 ) {
263- $machineFilter = $machineNames | ForEach-Object { " '$_ '" } | -join " , "
258+ $machineFilter = ( $machineNames | ForEach-Object { " '$_ '" }) -join " , "
264259 $query += " | where name in~ ($machineFilter )"
265- }
260+ }
266261
267262 $query += "
268263 | extend machineId = tolower(tostring(id))
269- | project machineId, Machine_name = name
264+ | project machineId, machineName = name
270265 | join kind= inner (
271266 resources
272- | where type == " microsoft.hybridcompute/ machines/ extensions"
267+ | where subscriptionId =~ '$ ( $sub.Id ) '
268+ | where type == 'microsoft.hybridcompute/machines/extensions'
273269 | where properties.publisher =~ 'Microsoft.AzureData'
274270 | where properties.provisioningState == 'Succeeded'
275271 | where properties.settings.LicenseType!='$LicenseType '
276272 | extend extensionName = name
277273 | extend extensionPublisher = properties.publisher
278274 | extend extensionType = properties.type
279275 | parse id with '/subscriptions/' subscriptionId '/resourceGroups/' resourceGroup '/providers/Microsoft.HybridCompute/machines/' machineName '/extensions/' extensionName
280- ) on $left .machineName == $right .machineName
276+ ) on ` $ left.machineName == ` $ right.machineName
281277 | project machineName, extensionName, resourceGroup, location, subscriptionId, extensionPublisher, extensionType
282278 "
283279
@@ -289,8 +285,6 @@ foreach ($sub in $subscriptions) {
289285
290286 while ($count -gt 0 ) {
291287 $count -= 1
292- Write-Output " VM-$ ( $count ) "
293- write-Output " VM - $ ( $resources [$count ].MachineName) "
294288 $setID = @ {
295289 MachineName = $resources [$count ].MachineName
296290 Name = $resources [$count ].extensionName
@@ -301,7 +295,7 @@ foreach ($sub in $subscriptions) {
301295 ExtensionType = $resources [$count ].extensionType
302296 }
303297
304- write-Output " VM - $ ( $setID.MachineName ) "
298+ write-Output " MachineName - $ ( $setID.MachineName ) "
305299 write-Output " ResourceGroup - $ ( $setID.ResourceGroup ) "
306300 write-Output " Location - $ ( $setID.Location ) "
307301 write-Output " SubscriptionId - $ ( $setID.SubscriptionId ) "
@@ -310,19 +304,7 @@ foreach ($sub in $subscriptions) {
310304 # Get connected machine info
311305 $sqlvm = Get-AzConnectedMachine - Name $setID.MachineName - ResourceGroup $setID.ResourceGroup | Select-Object Name, Tags, Status
312306
313- # Collect data before modification
314- $modifiedResources += [PSCustomObject ]@ {
315- TenantID = $TenantId
316- SubID = $setID.SubscriptionId
317- ResourceName = $setID.MachineName
318- ResourceType = $setID.ExtensionType
319- Status = $sqlvm.Status
320- OriginalLicenseType = $settings.LicenseType
321- ResourceGroup = $setID.ResourceGroup
322- Location = $setID.Location
323- # Cores <To be added>
324- }
325-
307+
326308 $excludedByTags = $false
327309 foreach ($tag in $tagTable.Keys ){
328310 if ($sqlvm.Tags.ContainsKey ($tag ))
@@ -339,16 +321,27 @@ foreach ($sub in $subscriptions) {
339321
340322
341323 $WriteSettings = $false
342- $settings = $resources [$count ].properties.settings | ConvertTo-Json | ConvertFrom-Json
343324 $ext = Get-AzConnectedMachineExtension - Name $setID.Name - ResourceGroupName $setID.ResourceGroup - MachineName $setID.MachineName
325+
326+ # Collect data before modification
327+ $modifiedResources += [PSCustomObject ]@ {
328+ TenantID = $TenantId
329+ SubID = $setID.SubscriptionId
330+ ResourceName = $setID.MachineName
331+ ResourceType = $setID.ExtensionType
332+ Status = $sqlvm.Status
333+ OriginalLicenseType = $ext.Setting [" LicenseType" ]
334+ ResourceGroup = $setID.ResourceGroup
335+ Location = $setID.Location
336+ # Cores <To be added>
337+ }
338+
344339 if ($ext.ProvisioningState -ne " Succeeded" ) {
345340 write-Output " Extension is not in a valid state. Skipping..."
346341 {continue }
347342 } else {
348- $LO_Allowed = (! $settings [" enableExtendedSecurityUpdates" ] -and ! $EnableESU ) -or ($EnableESU -eq " No" )
343+ $LO_Allowed = (! $ext .Setting [" enableExtendedSecurityUpdates" ] -and ! $EnableESU ) -or ($EnableESU -eq " No" )
349344
350- write-Output " LicenseType - $ ( $settings.LicenseType ) "
351-
352345 if ($LicenseType ) {
353346 if (($LicenseType -eq " LicenseOnly" ) -and ! $LO_Allowed ) {
354347 write-Output " ESU must be disabled before license type can be set to $ ( $LicenseType ) "
0 commit comments