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

Added scope to Update-Module. Added localized error message #471

Merged
merged 1 commit into from
May 1, 2019
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
1 change: 1 addition & 0 deletions src/PowerShellGet/PSGet.Resource.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ConvertFrom-StringData @'
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.
MinimumVersionIsGreaterThanMaximumVersion=The specified MinimumVersion '{0}' is greater than the specified MaximumVersion '{1}'.
AllowPrereleaseRequiredToUsePrereleaseStringInVersion=The '-AllowPrerelease' parameter must be specified when using the Prerelease string in MinimumVersion, MaximumVersion, or RequiredVersion.
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).
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).
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).
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).
Expand Down
22 changes: 21 additions & 1 deletion src/PowerShellGet/public/psgetfunctions/Update-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function Update-Module {
[PSCredential]
$Credential,

[Parameter()]
[ValidateSet("CurrentUser", "AllUsers")]
[string]
$Scope,

[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[Uri]
Expand All @@ -51,6 +56,17 @@ function Update-Module {
Begin {
Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -Proxy $Proxy -ProxyCredential $ProxyCredential

if ($Scope -eq "AllUsers" -and -not (Test-RunningAsElevated)) {
# Throw an error when Update-Module is used as a non-admin user and '-Scope AllUsers'
$message = $LocalizedData.UpdateModuleAdminPrivilegeRequiredForAllUsersScope -f @($script:programFilesModulesPath, $script:MyDocumentsModulesPath)

ThrowError -ExceptionName "System.ArgumentException" `
-ExceptionMessage $message `
-ErrorId "UpdateModuleAdminPrivilegeRequiredForAllUsersScope" `
-CallerPSCmdlet $PSCmdlet `
-ErrorCategory InvalidArgument
}

# Module names already tried in the current pipeline
$moduleNamesInPipeline = @()
}
Expand Down Expand Up @@ -135,7 +151,11 @@ function Update-Module {
$PSBoundParameters["PackageManagementProvider"] = $providerName
$PSBoundParameters["InstallUpdate"] = $true

$PSBoundParameters["Scope"] = Get-InstallationScope -PreviousInstallLocation $psgetItemInfo.InstalledLocation -CurrentUserPath $script:MyDocumentsModulesPath
if (-not $Scope) {
$Scope = Get-InstallationScope -PreviousInstallLocation $psgetItemInfo.InstalledLocation -CurrentUserPath $script:MyDocumentsModulesPath
}

$PSBoundParameters["Scope"] = $Scope

$sid = PackageManagement\Install-Package @PSBoundParameters
}
Expand Down