Skip to content

[#103630362] Fixed uninstaller and added Admin role validation #995

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 2 commits into from
Sep 28, 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
Binary file modified tools/AzureRM/AzureRM.psd1
Binary file not shown.
31 changes: 24 additions & 7 deletions tools/AzureRM/AzureRM.psm1
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#Requires -RunAsAdministrator

$AzureRMProfileVersion = "0.9.8";

$AzureRMModules = @{
"Azure.Storage" = "0.9.8";
"AzureRM.ApiManagement" = "0.9.8";
"AzureRM.Automation" = "0.9.8";
"AzureRM.Backup" = "0.9.8";
Expand All @@ -16,7 +13,6 @@ $AzureRMModules = @{
"AzureRM.KeyVault" = "0.9.8";
"AzureRM.Network" = "0.9.8";
"AzureRM.OperationalInsights" = "0.9.8";
"AzureRM.Profile" = "0.9.8";
"AzureRM.RedisCache" = "0.9.8";
"AzureRM.Resources" = "0.9.8";
"AzureRM.SiteRecovery" = "0.9.8";
Expand All @@ -29,6 +25,19 @@ $AzureRMModules = @{
"AzureRM.Websites" = "0.9.8"
}

function Validate-AdminRights([string]$Scope)
{
if ($Scope -ne "CurrentUser")
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
$isAdmin = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if($isAdmin -eq $false)
{
throw "Administrator rights are required to install Microsoft Azure modules"
}
}
}

function Import-ModuleWithVersionCheck([string]$Name,[string]$MinimumVersion,[string]$Repository,[string]$Scope)
{
$minVer = $MinimumVersion
Expand Down Expand Up @@ -77,6 +86,8 @@ function Update-AzureRM
$Scope = "AllUsers"
}

Validate-AdminRights $Scope

Write-Output "Installing AzureRM modules."

Import-ModuleWithVersionCheck "AzureRM.Profile" $AzureRMProfileVersion $Repository $Scope
Expand All @@ -103,18 +114,24 @@ function Uninstall-AzureRM
[string]
$Repository)

Validate-AdminRights "AllUsers"

Write-Output "Uninstalling AzureRM modules."

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

if ((Get-InstalledModule | where {"AzureRM.Profile" -eq $moduleName}) -ne $null) {
Uninstall-Module -Name "AzureRM.Profile" -ErrorAction Stop
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..."
}
}
Expand Down