Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit ff40856

Browse files
lwajswajedyoung
authored andcommitted
Added scopeto Update-Module. Added localized error message (#471)
1 parent 4e8368e commit ff40856

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/PowerShellGet/PSGet.Resource.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ConvertFrom-StringData @'
2222
RequiredVersionAllowedOnlyWithSingleModuleName=The RequiredVersion parameter is allowed only when a single module name is specified as the value of the Name parameter, without any wildcard characters.
2323
MinimumVersionIsGreaterThanMaximumVersion=The specified MinimumVersion '{0}' is greater than the specified MaximumVersion '{1}'.
2424
AllowPrereleaseRequiredToUsePrereleaseStringInVersion=The '-AllowPrerelease' parameter must be specified when using the Prerelease string in MinimumVersion, MaximumVersion, or RequiredVersion.
25+
UpdateModuleAdminPrivilegeRequiredForAllUsersScope=Administrator rights are required to update modules in '{0}'. Log on to the computer with an account that has Administrator rights, and then try again, or update '{1}' by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator).
2526
InstallModuleAdminPrivilegeRequiredForAllUsersScope=Administrator rights are required to install modules in '{0}'. Log on to the computer with an account that has Administrator rights, and then try again, or install '{1}' by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator).
2627
InstallScriptAdminPrivilegeRequiredForAllUsersScope=Administrator rights are required to install scripts in '{0}'. Log on to the computer with an account that has Administrator rights, and then try again, or install '{1}' by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator).
2728
AdministratorRightsNeededOrSpecifyCurrentUserScope=Administrator rights are required to install or update. Log on to the computer with an account that has Administrator rights, and then try again, or install by adding "-Scope CurrentUser" to your command. You can also try running the Windows PowerShell session with elevated rights (Run as Administrator).

src/PowerShellGet/public/psgetfunctions/Update-Module.ps1

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ function Update-Module {
2626
[PSCredential]
2727
$Credential,
2828

29+
[Parameter()]
30+
[ValidateSet("CurrentUser", "AllUsers")]
31+
[string]
32+
$Scope,
33+
2934
[Parameter(ValueFromPipelineByPropertyName = $true)]
3035
[ValidateNotNullOrEmpty()]
3136
[Uri]
@@ -51,6 +56,17 @@ function Update-Module {
5156
Begin {
5257
Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -Proxy $Proxy -ProxyCredential $ProxyCredential
5358

59+
if ($Scope -eq "AllUsers" -and -not (Test-RunningAsElevated)) {
60+
# Throw an error when Update-Module is used as a non-admin user and '-Scope AllUsers'
61+
$message = $LocalizedData.UpdateModuleAdminPrivilegeRequiredForAllUsersScope -f @($script:programFilesModulesPath, $script:MyDocumentsModulesPath)
62+
63+
ThrowError -ExceptionName "System.ArgumentException" `
64+
-ExceptionMessage $message `
65+
-ErrorId "UpdateModuleAdminPrivilegeRequiredForAllUsersScope" `
66+
-CallerPSCmdlet $PSCmdlet `
67+
-ErrorCategory InvalidArgument
68+
}
69+
5470
# Module names already tried in the current pipeline
5571
$moduleNamesInPipeline = @()
5672
}
@@ -135,7 +151,11 @@ function Update-Module {
135151
$PSBoundParameters["PackageManagementProvider"] = $providerName
136152
$PSBoundParameters["InstallUpdate"] = $true
137153

138-
$PSBoundParameters["Scope"] = Get-InstallationScope -PreviousInstallLocation $psgetItemInfo.InstalledLocation -CurrentUserPath $script:MyDocumentsModulesPath
154+
if (-not $Scope) {
155+
$Scope = Get-InstallationScope -PreviousInstallLocation $psgetItemInfo.InstalledLocation -CurrentUserPath $script:MyDocumentsModulesPath
156+
}
157+
158+
$PSBoundParameters["Scope"] = $Scope
139159

140160
$sid = PackageManagement\Install-Package @PSBoundParameters
141161
}

0 commit comments

Comments
 (0)