From e569d415aa3ea3dfa9ce87f62c43ebb05b4470bd Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Thu, 13 Jul 2023 15:21:37 -0700 Subject: [PATCH 1/2] Optimized LoadModule --- .../modify-license-type.ps1 | 51 ++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 01673d28..65d74402 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -39,31 +39,58 @@ param ( [switch] $Force ) -function CheckModule ($m) { - # This function ensures that the specified module is imported into the session - # If module is already imported - do nothing +function LoadModule +{ + param ( + [parameter(Mandatory = $true)][string] $name + ) - 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 + $retVal = $true + + if (!(Get-Module -Name $name)) + { + $retVal = Get-Module -ListAvailable | Where-Object {$_.Name -eq $name) + + if ($retVal) + { + try + { + Import-Module $name -ErrorAction SilentlyContinue + } + catch + { + write-host "The request to lload module $($name) failed with the following error:" + write-host $_.Exception.Message + $retVal = $false + } } 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 + if (Find-Module -Name $name) { + Install-Module -Name $name -Force -Verbose -Scope CurrentUser + try + { + Import-Module $name -ErrorAction SilentlyContinue + } + catch + { + write-host "The request to lload module $($name) failed with the following error:" + write-host $_.Exception.Message + $retVal = $false + } } 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." + write-host "Module $($name) not imported, not available and not in online gallery, exiting." EXIT 1 } } } + + return $retVal } function ConvertTo-Hashtable { @@ -117,7 +144,7 @@ $requiredModules = @( "Az.ConnectedMachine", "Az.ResourceGraph" ) -$requiredModules | Foreach-Object {CheckModule $_} +$requiredModules | Foreach-Object {LoadModule $_} # Subscriptions to scan From 7eaf3bd368ba71b66b029083c7efef00b4cb7ff9 Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Thu, 13 Jul 2023 15:29:48 -0700 Subject: [PATCH 2/2] Optimized LoadModule --- .../modify-license-type.ps1 | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 index 65d74402..b0104cd4 100644 --- a/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1 @@ -39,7 +39,46 @@ param ( [switch] $Force ) +function ConvertTo-Hashtable { + [CmdletBinding()] + [OutputType('hashtable')] + param ( + [Parameter(ValueFromPipeline)] + $InputObject + ) + process { + ## Return null if the input is null. This can happen when calling the function + ## recursively and a property is null + if ($null -eq $InputObject) { + return $null + } + ## Check if the input is an array or collection. If so, we also need to convert + ## those types into hash tables as well. This function will convert all child + ## objects into hash tables (if applicable) + if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) { + $collection = @( + foreach ($object in $InputObject) { + ConvertTo-Hashtable -InputObject $object + } + ) + ## Return the array but don't enumerate it because the object may be pretty complex + Write-Output -NoEnumerate $collection + } elseif ($InputObject -is [psobject]) { + ## If the object has properties that need enumeration, cxonvert it to its own hash table and return it + $hash = @{} + foreach ($property in $InputObject.PSObject.Properties) { + $hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value + } + $hash + } else { + ## If the object isn't an array, collection, or other object, it's already a hash table + ## So just return it. + $InputObject + } + } +} +# This function checks if the specified module is imported into the session and if not installes and/or imports it function LoadModule { param ( @@ -93,45 +132,6 @@ function LoadModule return $retVal } -function ConvertTo-Hashtable { - [CmdletBinding()] - [OutputType('hashtable')] - param ( - [Parameter(ValueFromPipeline)] - $InputObject - ) - process { - ## Return null if the input is null. This can happen when calling the function - ## recursively and a property is null - if ($null -eq $InputObject) { - return $null - } - ## Check if the input is an array or collection. If so, we also need to convert - ## those types into hash tables as well. This function will convert all child - ## objects into hash tables (if applicable) - if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) { - $collection = @( - foreach ($object in $InputObject) { - ConvertTo-Hashtable -InputObject $object - } - ) - ## Return the array but don't enumerate it because the object may be pretty complex - Write-Output -NoEnumerate $collection - } elseif ($InputObject -is [psobject]) { - ## If the object has properties that need enumeration, cxonvert it to its own hash table and return it - $hash = @{} - foreach ($property in $InputObject.PSObject.Properties) { - $hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value - } - $hash - } else { - ## If the object isn't an array, collection, or other object, it's already a hash table - ## So just return it. - $InputObject - } - } -} - # # Suppress warnings #