Skip to content

Commit 4e22d6e

Browse files
committed
Merge pull request Azure#1070 from stankovski/dev
Fix for file locking with Install-AzureRM
2 parents 62647dc + 6412020 commit 4e22d6e

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

tools/AzureRM/AzureRM.psd1

0 Bytes
Binary file not shown.

tools/AzureRM/AzureRM.psm1

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,22 @@ function Test-AdminRights([string]$Scope)
4040

4141
function Install-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion,[string]$Repository,[string]$Scope)
4242
{
43-
$minVer = "$MajorVersion.0.0.0"
44-
$maxVer = "$MajorVersion.9999.9999.9999"
43+
$_MinVer = "$MajorVersion.0.0.0"
44+
$_MaxVer = "$MajorVersion.9999.9999.9999"
4545
try {
46-
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $minVer -MaximumVersion $maxVer -Confirm:$false -Force -ErrorAction Stop
46+
$_ExistingModule = Get-Module -ListAvailable -Name $Name
47+
$_ModuleAction = "installed"
48+
if ($_ExistingModule -ne $null)
49+
{
50+
Update-Module -Name $Name -MaximumVersion $_MaxVer -ErrorAction Stop
51+
$_ModuleAction = "updated"
52+
}
53+
else
54+
{
55+
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $_MinVer -MaximumVersion $_MaxVer -ErrorAction Stop
56+
}
4757
$v = (Get-InstalledModule -Name $Name -ErrorAction Ignore)[0].Version.ToString()
48-
Write-Output "$Name $v installed..."
58+
Write-Output "$Name $v $_ModuleAction..."
4959
} catch {
5060
Write-Warning "Skipping $Name package..."
5161
Write-Warning $_
@@ -86,22 +96,30 @@ function Update-AzureRM
8696

8797
Write-Output "Installing AzureRM modules."
8898

89-
Install-ModuleWithVersionCheck "AzureRM.Profile" $MajorVersion $Repository $Scope
99+
$_InstallationPolicy = (Get-PSRepository -Name PSGallery).InstallationPolicy
90100

91-
# Stop and remove any previous jobs
92-
$AzureRMModules | ForEach {Get-Job -Name "*$_"} | Stop-Job | Remove-Job
93-
94-
# Start new job
95-
$result = $AzureRMModules | ForEach {
96-
Start-Job -Name $_ -ScriptBlock {
97-
Install-ModuleWithVersionCheck $args[0] $args[1] $args[2] $args[3]
98-
} -ArgumentList $_, $MajorVersion, $Repository, $Scope }
99-
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}
101+
try
102+
{
103+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
104+
105+
Install-ModuleWithVersionCheck "AzureRM.Profile" $MajorVersion $Repository $Scope
106+
107+
# Stop and remove any previous jobs
108+
$AzureRMModules | ForEach {Get-Job -Name "*$_"} | Stop-Job | Remove-Job
109+
110+
# 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 }
118+
} finally {
119+
# Clean up
120+
Set-PSRepository -Name PSGallery -InstallationPolicy $_InstallationPolicy
121+
$AzureRMModules | ForEach {Get-Job -Name $_ | Remove-Job}
122+
}
105123
}
106124

107125
<#
@@ -123,12 +141,12 @@ function Import-AzureRM
123141

124142
Write-Output "Importing AzureRM modules."
125143

126-
$minVer = "$MajorVersion.0.0.0"
127-
$maxVer = "$MajorVersion.9999.9999.9999"
144+
$_MinVer = "$MajorVersion.0.0.0"
145+
$_MaxVer = "$MajorVersion.9999.9999.9999"
128146

129147
$AzureRMModules | ForEach {
130148
$moduleName = $_
131-
$matchedModule = Get-InstalledModule -Name $moduleName -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Ignore | where {$_.Name -eq $moduleName}
149+
$matchedModule = Get-InstalledModule -Name $moduleName -MinimumVersion $_MinVer -MaximumVersion $_MaxVer -ErrorAction Ignore | where {$_.Name -eq $moduleName}
132150
if ($matchedModule -ne $null) {
133151
try {
134152
Import-Module -Name $matchedModule.Name -RequiredVersion $matchedModule.Version -ErrorAction Stop
@@ -143,10 +161,10 @@ function Import-AzureRM
143161

144162
function Uninstall-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion)
145163
{
146-
$minVer = "$MajorVersion.0.0.0"
147-
$maxVer = "$MajorVersion.9999.9999.9999"
164+
$_MinVer = "$MajorVersion.0.0.0"
165+
$_MaxVer = "$MajorVersion.9999.9999.9999"
148166
# 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}
167+
$matchedModule = Get-InstalledModule -Name $Name -MinimumVersion $_MinVer -MaximumVersion $_MaxVer -ErrorAction Ignore | where {$_.Name -eq $Name}
150168
if ($matchedModule -ne $null) {
151169
try {
152170
Remove-Module -Name $matchedModule.Name -Force -ErrorAction Ignore

0 commit comments

Comments
 (0)