Skip to content

Updated logic for Install/Uninstall AzureRM and added Import-AzureRM #1066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions setup-powershellget/Setup/ShortcutStartup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ This may take some time...

Import-Module PowerShellGet

Install-Module AzureRM
$DefaultPSRepository = $env:DefaultPSRepository
if ([string]::IsNullOrWhiteSpace($DefaultPSRepository))
{
$DefaultPSRepository = "PSGallery"
}

Install-Module AzureRM -Repository $DefaultPSRepository
Write-Output "AzureRM $((Get-InstalledModule -Name AzureRM)[0].Version) installed..."
Update-AzureRM

Update-AzureRM -Repository $DefaultPSRepository
} else {
cd c:\
$welcomeMessage = @"
Expand All @@ -73,6 +80,6 @@ catch
Write-Output "An error occured during installation."
Write-Output $error
Write-Output "Press any key..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

Binary file modified tools/AzureRM/AzureRM.psd1
Binary file not shown.
188 changes: 120 additions & 68 deletions tools/AzureRM/AzureRM.psm1
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
$AzureRMProfileVersion = "0.9.8";

$AzureRMModules = @{
"AzureRM.ApiManagement" = "0.9.8";
"AzureRM.Automation" = "0.9.8";
"AzureRM.Backup" = "0.9.8";
"AzureRM.Batch" = "0.9.8";
"AzureRM.Compute" = "0.9.8";
"AzureRM.DataFactories" = "0.9.8";
"AzureRM.Dns" = "0.9.8";
"AzureRM.HDInsight" = "0.9.8";
"AzureRM.Insights" = "0.9.8";
"AzureRM.KeyVault" = "0.9.8";
"AzureRM.Network" = "0.9.8";
"AzureRM.OperationalInsights" = "0.9.8";
"AzureRM.RedisCache" = "0.9.8";
"AzureRM.Resources" = "0.9.8";
"AzureRM.SiteRecovery" = "0.9.8";
"AzureRM.Sql" = "0.9.8";
"AzureRM.Storage" = "0.9.8";
"AzureRM.StreamAnalytics" = "0.9.8";
"AzureRM.Tags" = "0.9.8";
"AzureRM.TrafficManager" = "0.9.8";
"AzureRM.UsageAggregates" = "0.9.8";
"AzureRM.Websites" = "0.9.8"
}

function Validate-AdminRights([string]$Scope)
$AzureMajorVersion = "0";

$AzureRMModules = @(
"AzureRM.ApiManagement",
"AzureRM.Automation",
"AzureRM.Backup",
"AzureRM.Batch",
"AzureRM.Compute",
"AzureRM.DataFactories",
"AzureRM.Dns",
"AzureRM.HDInsight",
"AzureRM.Insights",
"AzureRM.KeyVault",
"AzureRM.Network",
"AzureRM.OperationalInsights",
"AzureRM.RedisCache",
"AzureRM.Resources",
"AzureRM.SiteRecovery",
"AzureRM.Sql",
"AzureRM.Storage",
"AzureRM.StreamAnalytics",
"AzureRM.Tags",
"AzureRM.TrafficManager",
"AzureRM.UsageAggregates",
"AzureRM.Websites"
)

function Test-AdminRights([string]$Scope)
{
if ($Scope -ne "CurrentUser")
{
Expand All @@ -38,18 +38,13 @@ function Validate-AdminRights([string]$Scope)
}
}

function Import-ModuleWithVersionCheck([string]$Name,[string]$MinimumVersion,[string]$Repository,[string]$Scope)
function Install-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion,[string]$Repository,[string]$Scope)
{
$minVer = $MinimumVersion
$maxVer = "$($minVer.Split(".")[0]).9999.0"
$minVer = "$MajorVersion.0.0.0"
$maxVer = "$MajorVersion.9999.9999.9999"
try {
if ([string]::IsNullOrWhiteSpace($Repository))
{
Install-Module -Name $Name -Scope $Scope -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Stop
} else {
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Stop
}
$v = (Get-InstalledModule -Name $Name)[0].Version.ToString()
Install-Module -Name $Name -Repository $Repository -Scope $Scope -MinimumVersion $minVer -MaximumVersion $maxVer -Confirm:$false -Force -ErrorAction Stop
$v = (Get-InstalledModule -Name $Name -ErrorAction Ignore)[0].Version.ToString()
Write-Output "$Name $v installed..."
} catch {
Write-Warning "Skipping $Name package..."
Expand All @@ -62,7 +57,10 @@ function Import-ModuleWithVersionCheck([string]$Name,[string]$MinimumVersion,[st
Install Azure Resource Manager cmdlet modules

.Description
Installs all the available Azure Resource Manager cmdlet modules.
Installs all the available Azure Resource Manager cmdlet modules that have a matching major version.

.Parameter MajorVersion
Specifies the major version.

.Parameter Repository
Limit the search for "AzureRM" cmdlets in a specific repository.
Expand All @@ -75,65 +73,119 @@ function Update-AzureRM
param(
[Parameter(Position=0, Mandatory = $false)]
[string]
$Repository,
$MajorVersion = $AzureMajorVersion,
[Parameter(Position=1, Mandatory = $false)]
[string]
$Repository = "PSGallery",
[Parameter(Position=2, Mandatory = $false)]
[ValidateSet("CurrentUser","AllUsers")]
[string]
$Scope)
$Scope = "AllUsers")

if ([string]::IsNullOrWhiteSpace($Scope))
{
$Scope = "AllUsers"
}

Validate-AdminRights $Scope
Test-AdminRights $Scope

Write-Output "Installing AzureRM modules."

Import-ModuleWithVersionCheck "AzureRM.Profile" $AzureRMProfileVersion $Repository $Scope
Install-ModuleWithVersionCheck "AzureRM.Profile" $MajorVersion $Repository $Scope

# Stop and remove any previous jobs
$AzureRMModules | ForEach {Get-Job -Name "*$_"} | Stop-Job | Remove-Job

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

$AzureRMModules.Keys | ForEach {Get-Job -Name $_ | Wait-Job | Receive-Job }
# Get results from the jobs
$AzureRMModules | ForEach {Get-Job -Name $_ | Wait-Job | Receive-Job }

# Clean up
$AzureRMModules | ForEach {Get-Job -Name $_ | Remove-Job}
}

<#
.Synopsis
Import Azure Resource Manager cmdlet modules

.Description
Imports all the Azure Resource Manager cmdlet modules that have a matching major version.

.Parameter MajorVersion
Specifies the major version.
#>
function Import-AzureRM
{
param(
[Parameter(Position=0, Mandatory = $false)]
[string]
$MajorVersion = $AzureMajorVersion)

Write-Output "Importing AzureRM modules."

$minVer = "$MajorVersion.0.0.0"
$maxVer = "$MajorVersion.9999.9999.9999"

$AzureRMModules | ForEach {
$moduleName = $_
$matchedModule = Get-InstalledModule -Name $moduleName -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Ignore | where {$_.Name -eq $moduleName}
if ($matchedModule -ne $null) {
try {
Import-Module -Name $matchedModule.Name -RequiredVersion $matchedModule.Version -ErrorAction Stop
Write-Output "$moduleName imported..."
} catch {
Write-Warning "Skipping $Name module..."
Write-Warning $_
}
}
}
}

function Uninstall-ModuleWithVersionCheck([string]$Name,[string]$MajorVersion)
{
$minVer = "$MajorVersion.0.0.0"
$maxVer = "$MajorVersion.9999.9999.9999"
# This is a workaround for a bug in PowerShellGet that uses "start with" matching for module name
$matchedModule = Get-InstalledModule -Name $Name -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Ignore | where {$_.Name -eq $Name}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were we using Get-InstalledModule or Get-Module -ListAvailable?

if ($matchedModule -ne $null) {
try {
Remove-Module -Name $matchedModule.Name -Force -ErrorAction Ignore
Uninstall-Module -Name $matchedModule.Name -RequiredVersion $matchedModule.Version -Confirm:$false -ErrorAction Stop
Write-Output "$Name uninstalled..."
} catch {
Write-Warning "Skipping $Name package..."
Write-Warning $_
}
}
}

<#
.Synopsis
Remove Azure Resource Manager cmdlet modules

.Description
Removes all installed Azure Resource Manager cmdlet modules.
Removes all installed Azure Resource Manager cmdlet modules that have a matching major version.

.Parameter MajorVersion
Specifies the major version.
#>
function Uninstall-AzureRM
{
param(
[Parameter(Position=0, Mandatory = $false)]
[string]
$Repository)
$MajorVersion = $AzureMajorVersion)

Validate-AdminRights "AllUsers"
Test-AdminRights "AllUsers"

Write-Output "Uninstalling AzureRM modules."

$AzureRMModules.Keys | ForEach {
$AzureRMModules | ForEach {
$moduleName = $_
if ((Get-InstalledModule | where {$_.Name -eq $moduleName}) -ne $null) {
$minVer = $AzureRMModules[$_]
$maxVer = "$($minVer.Split(".")[0]).9999.0"
Uninstall-Module -Name $_ -MinimumVersion $minVer -MaximumVersion $maxVer -ErrorAction Stop
Write-Output "$moduleName uninstalled..."
}
Uninstall-ModuleWithVersionCheck $_ $MajorVersion
}

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

New-Alias -Name Install-AzureRM -Value Update-AzureRM
Expand Down