Tested version

This commit is contained in:
Alexander (Sasha) Nosov
2024-06-15 20:05:07 -07:00
committed by GitHub
parent 58a15954a2
commit fa6bd5dbdd
@@ -1,82 +1,33 @@
#
# This script performes a scheduled activation of a SQL Server p-core license.
#
# The script accepts the following command line parameters:
#
# -LicenseID (The specific resource URI)
# -UseInRunbook (True to use Azure Runbook)
#
<#
.DESCRIPTION
This runbook activates a SQL Server license using the Managed Identity
The runbook accepts the following parameters:
-LicenseID (The license resource URI)
.NOTES
AUTHOR: Alexander (Sasha) Nosov
LASTEDIT: June 15, 2024
#>
param (
[Parameter (Mandatory= $true)]
[string] $LicenseId,
[Parameter (Mandatory= $true)]
[bool] $UseInRunbook
)
function CheckModule ($m) {
# This function ensures that the specified module is imported into the session
# If module is already imported - do nothing
if (!(Get-Module | Where-Object {$_.Name -eq $m})) {
# If module is not imported, but available on disk then import
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
Import-Module $m
}
else {
# If module is not imported, not available on disk, but is in online gallery then install and import
if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
Import-Module $m
}
else {
# If module is not imported, not available and not in online gallery then abort
write-host "Module $m not imported, not available and not in online gallery, exiting."
EXIT 1
}
}
}
}
#
# Suppress warnings
#
Update-AzConfig -DisplayBreakingChangeWarning $false
#The following block is required for runbooks only
if ($UseInRunbook){
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave Scope Process
$connection = Get-AutomationConnection -Name AzureRunAsConnection
# Wrap authentication in retry logic for transient network failures
$logonAttempt = 0
while(!($connectionResult) -and ($logonAttempt -le 10))
{
$LogonAttempt++
# Logging in to Azure...
$connectionResult = Connect-AzAccount `
-ServicePrincipal `
-Tenant $connection.TenantID `
-ApplicationId $connection.ApplicationID `
-CertificateThumbprint $connection.CertificateThumbprint
Start-Sleep -Seconds 5
}
}else{
# Ensure that the required modules are imported
# In Runbooks these modules must be added to the automation account manually
$requiredModules = @(
"Az.Accounts",
"Az.Resources"
)
$requiredModules | Foreach-Object {CheckModule $_}
# Logging in to Azure..."
try
{
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
$currentLicense = Get-AzResource -ResourceId $LicenseId