Skip to content

Commit fab05c1

Browse files
committed
Updated logic for Install/Uninstall AzureRM and added Import-AzureRM
1 parent fd3793d commit fab05c1

File tree

3 files changed

+130
-71
lines changed

3 files changed

+130
-71
lines changed

setup-powershellget/Setup/ShortcutStartup.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ This may take some time...
4949

5050
Import-Module PowerShellGet
5151

52-
Install-Module AzureRM
52+
$DefaultPSRepository = $env:DefaultPSRepository
53+
if ([string]::IsNullOrWhiteSpace($DefaultPSRepository))
54+
{
55+
$DefaultPSRepository = "PSGallery"
56+
}
57+
58+
Install-Module AzureRM -Repository $DefaultPSRepository
5359
Write-Output "AzureRM $((Get-InstalledModule -Name AzureRM)[0].Version) installed..."
54-
Update-AzureRM
60+
61+
Update-AzureRM -Repository $DefaultPSRepository
5562
} else {
5663
cd c:\
5764
$welcomeMessage = @"
@@ -73,6 +80,6 @@ catch
7380
Write-Output "An error occured during installation."
7481
Write-Output $error
7582
Write-Output "Press any key..."
76-
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
83+
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
7784
}
7885

tools/AzureRM/AzureRM.psd1

0 Bytes
Binary file not shown.

tools/AzureRM/AzureRM.psm1

Lines changed: 120 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
$AzureRMProfileVersion = "0.9.8";
2-
3-
$AzureRMModules = @{
4-
"AzureRM.ApiManagement" = "0.9.8";
5-
"AzureRM.Automation" = "0.9.8";
6-
"AzureRM.Backup" = "0.9.8";
7-
"AzureRM.Batch" = "0.9.8";
8-
"AzureRM.Compute" = "0.9.8";
9-
"AzureRM.DataFactories" = "0.9.8";
10-
"AzureRM.Dns" = "0.9.8";
11-
"AzureRM.HDInsight" = "0.9.8";
12-
"AzureRM.Insights" = "0.9.8";
13-
"AzureRM.KeyVault" = "0.9.8";
14-
"AzureRM.Network" = "0.9.8";
15-
"AzureRM.OperationalInsights" = "0.9.8";
16-
"AzureRM.RedisCache" = "0.9.8";
17-
"AzureRM.Resources" = "0.9.8";
18-
"AzureRM.SiteRecovery" = "0.9.8";
19-
"AzureRM.Sql" = "0.9.8";
20-
"AzureRM.Storage" = "0.9.8";
21-
"AzureRM.StreamAnalytics" = "0.9.8";
22-
"AzureRM.Tags" = "0.9.8";
23-
"AzureRM.TrafficManager" = "0.9.8";
24-
"AzureRM.UsageAggregates" = "0.9.8";
25-
"AzureRM.Websites" = "0.9.8"
26-
}
27-
28-
function Validate-AdminRights([string]$Scope)
1+
$AzureMajorVersion = "0";
2+
3+
$AzureRMModules = @(
4+
"AzureRM.ApiManagement",
5+
"AzureRM.Automation",
6+
"AzureRM.Backup",
7+
"AzureRM.Batch",
8+
"AzureRM.Compute",
9+
"AzureRM.DataFactories",
10+
"AzureRM.Dns",
11+
"AzureRM.HDInsight",
12+
"AzureRM.Insights",
13+
"AzureRM.KeyVault",
14+
"AzureRM.Network",
15+
"AzureRM.OperationalInsights",
16+
"AzureRM.RedisCache",
17+
"AzureRM.Resources",
18+
"AzureRM.SiteRecovery",
19+
"AzureRM.Sql",
20+
"AzureRM.Storage",
21+
"AzureRM.StreamAnalytics",
22+
"AzureRM.Tags",
23+
"AzureRM.TrafficManager",
24+
"AzureRM.UsageAggregates",
25+
"AzureRM.Websites"
26+
)
27+
28+
function Test-AdminRights([string]$Scope)
2929
{
3030
if ($Scope -ne "CurrentUser")
3131
{
@@ -38,18 +38,13 @@ function Validate-AdminRights([string]$Scope)
3838
}
3939
}
4040

41-
function Import-ModuleWithVersionCheck([string]$Name,[string]$MinimumVersion,[string]$Repository,[string]$Scope)
41+
function Install-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion,[string]$Repository,[string]$Scope)
4242
{
43-
$minVer = $MinimumVersion
44-
$maxVer = "$($minVer.Split(".")[0]).9999.0"
43+
$minVer = "$MajorVersion.0.0.0"
44+
$maxVer = "$MajorVersion.9999.9999.9999"
4545
try {
46-
if ([string]::IsNullOrWhiteSpace($Repository))
47-
{
48-
Install-Module -Name $Name -Scope $Scope -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Stop
49-
} else {
50-
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Stop
51-
}
52-
$v = (Get-InstalledModule -Name $Name)[0].Version.ToString()
46+
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $minVer -MaximumVersion $maxVer -Confirm:$false -Force -ErrorAction Stop
47+
$v = (Get-InstalledModule -Name $Name -ErrorAction Ignore)[0].Version.ToString()
5348
Write-Output "$Name $v installed..."
5449
} catch {
5550
Write-Warning "Skipping $Name package..."
@@ -62,7 +57,10 @@ function Import-ModuleWithVersionCheck([string]$Name,[string]$MinimumVersion,[st
6257
Install Azure Resource Manager cmdlet modules
6358
6459
.Description
65-
Installs all the available Azure Resource Manager cmdlet modules.
60+
Installs all the available Azure Resource Manager cmdlet modules that have a matching major version.
61+
62+
.Parameter MajorVersion
63+
Specifies the major version.
6664
6765
.Parameter Repository
6866
Limit the search for "AzureRM" cmdlets in a specific repository.
@@ -75,65 +73,119 @@ function Update-AzureRM
7573
param(
7674
[Parameter(Position=0, Mandatory = $false)]
7775
[string]
78-
$Repository,
76+
$MajorVersion = $AzureMajorVersion,
7977
[Parameter(Position=1, Mandatory = $false)]
78+
[string]
79+
$Repository = "PSGallery",
80+
[Parameter(Position=2, Mandatory = $false)]
8081
[ValidateSet("CurrentUser","AllUsers")]
8182
[string]
82-
$Scope)
83+
$Scope = "AllUsers")
8384

84-
if ([string]::IsNullOrWhiteSpace($Scope))
85-
{
86-
$Scope = "AllUsers"
87-
}
88-
89-
Validate-AdminRights $Scope
85+
Test-AdminRights $Scope
9086

9187
Write-Output "Installing AzureRM modules."
9288

93-
Import-ModuleWithVersionCheck "AzureRM.Profile" $AzureRMProfileVersion $Repository $Scope
89+
Install-ModuleWithVersionCheck "AzureRM.Profile" $MajorVersion $Repository $Scope
90+
91+
# Stop and remove any previous jobs
92+
$AzureRMModules | ForEach {Get-Job -Name "*$_"} | Stop-Job | Remove-Job
9493

95-
$result = $AzureRMModules.Keys | ForEach {
94+
# Start new job
95+
$result = $AzureRMModules | ForEach {
9696
Start-Job -Name $_ -ScriptBlock {
97-
Import-ModuleWithVersionCheck $args[0] $args[1] $args[2] $args[3]
98-
} -ArgumentList $_, $AzureRMModules[$_], $Repository, $Scope }
97+
Install-ModuleWithVersionCheck $args[0] $args[1] $args[2] $args[3]
98+
} -ArgumentList $_, $MajorVersion, $Repository, $Scope }
9999

100-
$AzureRMModules.Keys | ForEach {Get-Job -Name $_ | Wait-Job | Receive-Job }
100+
# Get results from the jobs
101+
$AzureRMModules | ForEach {Get-Job -Name $_ | Wait-Job | Receive-Job }
102+
103+
# Clean up
104+
$AzureRMModules | ForEach {Get-Job -Name $_ | Remove-Job}
105+
}
106+
107+
<#
108+
.Synopsis
109+
Import Azure Resource Manager cmdlet modules
110+
111+
.Description
112+
Imports all the Azure Resource Manager cmdlet modules that have a matching major version.
113+
114+
.Parameter MajorVersion
115+
Specifies the major version.
116+
#>
117+
function Import-AzureRM
118+
{
119+
param(
120+
[Parameter(Position=0, Mandatory = $false)]
121+
[string]
122+
$MajorVersion = $AzureMajorVersion)
123+
124+
Write-Output "Importing AzureRM modules."
125+
126+
$minVer = "$MajorVersion.0.0.0"
127+
$maxVer = "$MajorVersion.9999.9999.9999"
128+
129+
$AzureRMModules | ForEach {
130+
$moduleName = $_
131+
$matchedModule = Get-InstalledModule -Name $moduleName -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Ignore | where {$_.Name -eq $moduleName}
132+
if ($matchedModule -ne $null) {
133+
try {
134+
Import-Module -Name $matchedModule.Name -RequiredVersion $matchedModule.Version -ErrorAction Stop
135+
Write-Output "$moduleName imported..."
136+
} catch {
137+
Write-Warning "Skipping $Name module..."
138+
Write-Warning $_
139+
}
140+
}
141+
}
142+
}
143+
144+
function Uninstall-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion)
145+
{
146+
$minVer = "$MajorVersion.0.0.0"
147+
$maxVer = "$MajorVersion.9999.9999.9999"
148+
# This is a workaround for a bug in PowerShellGet that uses "start with" matching for module name
149+
$matchedModule = Get-InstalledModule -Name $Name -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Ignore | where {$_.Name -eq $Name}
150+
if ($matchedModule -ne $null) {
151+
try {
152+
Remove-Module -Name $matchedModule.Name -Force -ErrorAction Ignore
153+
Uninstall-Module -Name $matchedModule.Name -RequiredVersion $matchedModule.Version -Confirm:$false -ErrorAction Stop
154+
Write-Output "$Name uninstalled..."
155+
} catch {
156+
Write-Warning "Skipping $Name package..."
157+
Write-Warning $_
158+
}
159+
}
101160
}
102161

103162
<#
104163
.Synopsis
105164
Remove Azure Resource Manager cmdlet modules
106165
107166
.Description
108-
Removes all installed Azure Resource Manager cmdlet modules.
167+
Removes all installed Azure Resource Manager cmdlet modules that have a matching major version.
168+
169+
.Parameter MajorVersion
170+
Specifies the major version.
109171
#>
110172
function Uninstall-AzureRM
111173
{
112174
param(
113175
[Parameter(Position=0, Mandatory = $false)]
114176
[string]
115-
$Repository)
177+
$MajorVersion = $AzureMajorVersion)
116178

117-
Validate-AdminRights "AllUsers"
179+
Test-AdminRights "AllUsers"
118180

119181
Write-Output "Uninstalling AzureRM modules."
120182

121-
$AzureRMModules.Keys | ForEach {
183+
$AzureRMModules | ForEach {
122184
$moduleName = $_
123-
if ((Get-InstalledModule | where {$_.Name -eq $moduleName}) -ne $null) {
124-
$minVer = $AzureRMModules[$_]
125-
$maxVer = "$($minVer.Split(".")[0]).9999.0"
126-
Uninstall-Module -Name $_ -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Stop
127-
Write-Output "$moduleName uninstalled..."
128-
}
185+
Uninstall-ModuleWithVersionCheck $_ $MajorVersion
129186
}
130187

131-
if ((Get-InstalledModule | where {$_.Name -eq "AzureRM.Profile"}) -ne $null) {
132-
$minVer = $AzureRMProfileVersion
133-
$maxVer = "$($minVer.Split(".")[0]).9999.0"
134-
Uninstall-Module -Name "AzureRM.Profile" -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Stop
135-
Write-Output "AzureRM.Profile uninstalled..."
136-
}
188+
Uninstall-ModuleWithVersionCheck "AzureRM.Profile" $MajorVersion
137189
}
138190

139191
New-Alias -Name Install-AzureRM -Value Update-AzureRM

0 commit comments

Comments
 (0)