Skip to content

Commit 4909ad0

Browse files
author
Hovsep
committed
Merge pull request #1074 from stankovski/dev
Misc. fixes for install and uninstall
2 parents 1e67494 + ffaf229 commit 4909ad0

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

setup-powershellget/Setup/ShortcutStartup.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,19 @@ This may take some time...
5757
$DefaultPSRepository = "PSGallery"
5858
}
5959

60-
Install-Module AzureRM -Repository $DefaultPSRepository
61-
Write-Output "AzureRM $((Get-InstalledModule -Name AzureRM)[0].Version) installed..."
60+
$_InstallationPolicy = (Get-PSRepository -Name $DefaultPSRepository).InstallationPolicy
61+
try
62+
{
63+
Set-PSRepository -Name $DefaultPSRepository -InstallationPolicy Trusted
64+
65+
Install-Module AzureRM -Repository $DefaultPSRepository
66+
Write-Output "AzureRM $((Get-InstalledModule -Name AzureRM)[0].Version) installed..."
6267

63-
Update-AzureRM -Repository $DefaultPSRepository
68+
Update-AzureRM -Repository $DefaultPSRepository
69+
} finally {
70+
# Clean up
71+
Set-PSRepository -Name $DefaultPSRepository -InstallationPolicy $_InstallationPolicy
72+
}
6473
}
6574
elseif ($Uninstall.IsPresent)
6675
{

tools/AzureRM/AzureRM.psm1

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$AzureMajorVersion = "0";
1+
$AzureMajorVersion = "0"
22

33
$AzureRMModules = @(
44
"AzureRM.ApiManagement",
@@ -42,20 +42,21 @@ function Install-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion,[str
4242
{
4343
$_MinVer = "$MajorVersion.0.0.0"
4444
$_MaxVer = "$MajorVersion.9999.9999.9999"
45+
$script:InstallCounter ++
4546
try {
4647
$_ExistingModule = Get-Module -ListAvailable -Name $Name
4748
$_ModuleAction = "installed"
4849
if ($_ExistingModule -ne $null)
4950
{
50-
Update-Module -Name $Name -MaximumVersion $_MaxVer -ErrorAction Stop
51+
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $_MinVer -MaximumVersion $_MaxVer -Force -ErrorAction Stop
5152
$_ModuleAction = "updated"
5253
}
5354
else
5455
{
5556
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $_MinVer -MaximumVersion $_MaxVer -ErrorAction Stop
5657
}
5758
$v = (Get-InstalledModule -Name $Name -ErrorAction Ignore)[0].Version.ToString()
58-
Write-Output "$Name $v $_ModuleAction..."
59+
Write-Output "$Name $v $_ModuleAction [$script:InstallCounter/$($AzureRMModules.Count + 1)]..."
5960
} catch {
6061
Write-Warning "Skipping $Name package..."
6162
Write-Warning $_
@@ -96,29 +97,22 @@ function Update-AzureRM
9697

9798
Write-Output "Installing AzureRM modules."
9899

99-
$_InstallationPolicy = (Get-PSRepository -Name PSGallery).InstallationPolicy
100+
$_InstallationPolicy = (Get-PSRepository -Name $Repository).InstallationPolicy
101+
$script:InstallCounter = 0
100102

101103
try
102104
{
103-
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
105+
Set-PSRepository -Name $Repository -InstallationPolicy Trusted
104106

105107
Install-ModuleWithVersionCheck "AzureRM.Profile" $MajorVersion $Repository $Scope
106108

107-
# Stop and remove any previous jobs
108-
$AzureRMModules | ForEach {Get-Job -Name "*$_"} | Stop-Job | Remove-Job
109-
110109
# Start new job
111-
$result = $AzureRMModules | ForEach {
112-
Start-Job -Name $_ -ScriptBlock {
113-
Install-ModuleWithVersionCheck $args[0] $args[1] $args[2] $args[3]
114-
} -ArgumentList $_, $MajorVersion, $Repository, $Scope }
115-
116-
# Get results from the jobs
117-
$AzureRMModules | ForEach {Get-Job -Name $_ | Wait-Job | Receive-Job }
110+
$AzureRMModules | ForEach {
111+
Install-ModuleWithVersionCheck $_ $MajorVersion $Repository $Scope
112+
}
118113
} finally {
119114
# Clean up
120-
Set-PSRepository -Name PSGallery -InstallationPolicy $_InstallationPolicy
121-
$AzureRMModules | ForEach {Get-Job -Name $_ | Remove-Job}
115+
Set-PSRepository -Name $Repository -InstallationPolicy $_InstallationPolicy
122116
}
123117
}
124118

@@ -167,11 +161,15 @@ function Uninstall-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion)
167161
$_MatchedModule = Get-InstalledModule -Name $Name -MinimumVersion $_MinVer -MaximumVersion $_MaxVer -ErrorAction Ignore | where {$_.Name -eq $Name}
168162
if ($_MatchedModule -ne $null) {
169163
try {
170-
Remove-Module -Name $_MatchedModule.Name -Force -ErrorAction Ignore
171-
Uninstall-Module -Name $_MatchedModule.Name -RequiredVersion $_MatchedModule.Version -Confirm:$false -ErrorAction Stop
172-
if ((Get-Module -Name $_MatchedModule.Name | where {$_.Version -eq $_MatchedModule.Version}) -eq $null)
164+
Remove-Module -Name $Name -Force -ErrorAction Ignore
165+
Uninstall-Module -Name $Name -MinimumVersion $_MinVer -MaximumVersion $_MaxVer -Confirm:$false -ErrorAction Stop
166+
if ((Get-Module -Name $Name -ListAvailable) -eq $null)
167+
{
168+
Write-Output "$Name uninstalled..."
169+
}
170+
else
173171
{
174-
Write-Output "$Name version $($_MatchedModule.Version) uninstalled..."
172+
Write-Output "$Name partially uninstalled..."
175173
}
176174
} catch {
177175
Write-Warning "Skipping $Name package..."

0 commit comments

Comments
 (0)