Skip to content

Commit a6ca3e7

Browse files
committed
Merge pull request #141 from Azure/dev
.
2 parents ad1aa1c + 4e22d6e commit a6ca3e7

File tree

4 files changed

+66
-31
lines changed

4 files changed

+66
-31
lines changed

setup-powershellget/Setup/ShortcutStartup.ps1

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313
# ----------------------------------------------------------------------------------
14-
[CmdletBinding()]
14+
[CmdletBinding(DefaultParametersetName="none")]
1515
Param(
16-
[Parameter(Mandatory=$False, HelpMessage="Use Install parameter to install Azure modules from PowerShell Gallery.")]
17-
[switch]$Install
16+
[Parameter(Mandatory=$True, HelpMessage="Use Install parameter to install Azure modules from PowerShell Gallery.", ParameterSetName="install")]
17+
[switch]$Install,
18+
[Parameter(Mandatory=$True, HelpMessage="Use Uninstall parameter to uninstall Azure modules from PowerShell Gallery.", ParameterSetName="uninstall")]
19+
[switch]$Uninstall
1820
)
1921

2022
function EnsureRegistryPath
@@ -38,7 +40,7 @@ try {
3840
Write-Output @"
3941
4042
Finalizing installation of Azure PowerShell.
41-
Installing Azure Modules from PowerShell Gallery.
43+
Installing AzureRM Modules from PowerShell Gallery.
4244
This may take some time...
4345
"@
4446
$env:PSModulePath = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules;$env:ProgramFiles\WindowsPowerShell\Modules;$env:SystemRoot\system32\WindowsPowerShell\v1.0\Modules\"
@@ -59,7 +61,20 @@ This may take some time...
5961
Write-Output "AzureRM $((Get-InstalledModule -Name AzureRM)[0].Version) installed..."
6062

6163
Update-AzureRM -Repository $DefaultPSRepository
62-
} else {
64+
}
65+
elseif ($Uninstall.IsPresent)
66+
{
67+
Write-Output @"
68+
69+
Finalizing uninstallation of Azure PowerShell.
70+
This may take some time...
71+
"@
72+
$env:PSModulePath = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules;$env:ProgramFiles\WindowsPowerShell\Modules;$env:SystemRoot\system32\WindowsPowerShell\v1.0\Modules\"
73+
74+
Uninstall-AzureRM
75+
}
76+
else
77+
{
6378
cd c:\
6479
$welcomeMessage = @"
6580
For a list of all Azure RM cmdlets type 'help azurerm'.

setup-powershellget/azurecmd.wxs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@
9797
<CustomAction Id="UpdatePSShortcut" BinaryKey="CustomActions" DllEntry="UpdatePSShortcut" Execute ="deferred" Impersonate="no"/>
9898
<CustomAction Id="SetExecutionPolicy" Property="POWERSHELLEXE" ExeCommand="-NoLogo -NoProfile -Command &quot;Set-ExecutionPolicy RemoteSigned -Force&quot;" Execute="deferred" Impersonate="no" Return="check"/>
9999
<CustomAction Id="RunModuleInstallScript" Property="POWERSHELLEXE" ExeCommand="-NoLogo -NoProfile -Command &quot;. \&quot;[PowerShellFolder]\ShortcutStartup.ps1\&quot; -Install&quot;" Execute="deferred" Impersonate="no" Return="check"/>
100+
<CustomAction Id="RunModuleUninstallScript" Property="POWERSHELLEXE" ExeCommand="-NoLogo -NoProfile -Command &quot;. \&quot;[PowerShellFolder]\ShortcutStartup.ps1\&quot; -Uninstall&quot;" Execute="deferred" Impersonate="no" Return="check"/>
100101

101102
<InstallExecuteSequence>
102103
<Custom Action="SetCustomActionDataValue" After="CreateShortcuts">NOT Installed</Custom>
103104
<Custom Action="UpdatePSShortcut" After="SetCustomActionDataValue">NOT Installed</Custom>
104105
<Custom Action="SetExecutionPolicy" After="UpdatePSShortcut">NOT Installed</Custom>
105106
<Custom Action="RunModuleInstallScript" After="SetExecutionPolicy">NOT Installed</Custom>
107+
<Custom Action="RunModuleUninstallScript" After="InstallInitialize">(NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")</Custom>
106108
</InstallExecuteSequence>
107109

108110
<WixVariable Id="WixUILicenseRtf" Value="$(var.ProjectDir)\media\License.rtf" />

tools/AzureRM/AzureRM.psd1

0 Bytes
Binary file not shown.

tools/AzureRM/AzureRM.psm1

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,29 @@ function Test-AdminRights([string]$Scope)
3333
$isAdmin = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
3434
if($isAdmin -eq $false)
3535
{
36-
throw "Administrator rights are required to install Microsoft Azure modules"
36+
throw "Administrator rights are required to install or uninstall Microsoft Azure modules"
3737
}
3838
}
3939
}
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)