Skip to content

Commit 7eaf3bd

Browse files
author
Alexander (Sasha) Nosov
committed
Optimized LoadModule
1 parent 4b88660 commit 7eaf3bd

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

samples/manage/azure-arc-enabled-sql-server/modify-license-type/modify-license-type.ps1

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,46 @@ param (
3939
[switch] $Force
4040
)
4141

42+
function ConvertTo-Hashtable {
43+
[CmdletBinding()]
44+
[OutputType('hashtable')]
45+
param (
46+
[Parameter(ValueFromPipeline)]
47+
$InputObject
48+
)
49+
process {
50+
## Return null if the input is null. This can happen when calling the function
51+
## recursively and a property is null
52+
if ($null -eq $InputObject) {
53+
return $null
54+
}
55+
## Check if the input is an array or collection. If so, we also need to convert
56+
## those types into hash tables as well. This function will convert all child
57+
## objects into hash tables (if applicable)
58+
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
59+
$collection = @(
60+
foreach ($object in $InputObject) {
61+
ConvertTo-Hashtable -InputObject $object
62+
}
63+
)
64+
## Return the array but don't enumerate it because the object may be pretty complex
65+
Write-Output -NoEnumerate $collection
66+
} elseif ($InputObject -is [psobject]) {
67+
## If the object has properties that need enumeration, cxonvert it to its own hash table and return it
68+
$hash = @{}
69+
foreach ($property in $InputObject.PSObject.Properties) {
70+
$hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value
71+
}
72+
$hash
73+
} else {
74+
## If the object isn't an array, collection, or other object, it's already a hash table
75+
## So just return it.
76+
$InputObject
77+
}
78+
}
79+
}
4280

81+
# This function checks if the specified module is imported into the session and if not installes and/or imports it
4382
function LoadModule
4483
{
4584
param (
@@ -93,45 +132,6 @@ function LoadModule
93132
return $retVal
94133
}
95134

96-
function ConvertTo-Hashtable {
97-
[CmdletBinding()]
98-
[OutputType('hashtable')]
99-
param (
100-
[Parameter(ValueFromPipeline)]
101-
$InputObject
102-
)
103-
process {
104-
## Return null if the input is null. This can happen when calling the function
105-
## recursively and a property is null
106-
if ($null -eq $InputObject) {
107-
return $null
108-
}
109-
## Check if the input is an array or collection. If so, we also need to convert
110-
## those types into hash tables as well. This function will convert all child
111-
## objects into hash tables (if applicable)
112-
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
113-
$collection = @(
114-
foreach ($object in $InputObject) {
115-
ConvertTo-Hashtable -InputObject $object
116-
}
117-
)
118-
## Return the array but don't enumerate it because the object may be pretty complex
119-
Write-Output -NoEnumerate $collection
120-
} elseif ($InputObject -is [psobject]) {
121-
## If the object has properties that need enumeration, cxonvert it to its own hash table and return it
122-
$hash = @{}
123-
foreach ($property in $InputObject.PSObject.Properties) {
124-
$hash[$property.Name] = ConvertTo-Hashtable -InputObject $property.Value
125-
}
126-
$hash
127-
} else {
128-
## If the object isn't an array, collection, or other object, it's already a hash table
129-
## So just return it.
130-
$InputObject
131-
}
132-
}
133-
}
134-
135135
#
136136
# Suppress warnings
137137
#

0 commit comments

Comments
 (0)