Skip to content
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

"Get-InstalledModule" and "Uninstall-Module" errors do not set $? to $false #487

Open
o-l-a-v opened this issue Feb 26, 2020 · 0 comments
Open

Comments

@o-l-a-v
Copy link

o-l-a-v commented Feb 26, 2020

This is using the latest stable version "1.4.6" of "PackageManagement" as of 2020-02-26 on Windows PowerShell 5.1

I've found that when errors happen in "Get-InstalledModule" and "Uninstall-Module" commands, errors is not added to $Error variable, and $? is not set to false unless $ErrorActionPreference is set to 'Stop'. Also, reading the error messages, I see that reported commandlet ran according to the error messages added to the terminal, are "PackageManagement\Uninstall-Package" for "Uninstall-Module" and "PackageManagement\Get-Package" for "Get-InstalledModule". When using these commands, $? is righfully set to $false if they fail, but these commands are not adding error to $Error either.

Code to reproduce (designed to run line per line)

# Settings
## PowerShell Preferences
$ErrorActionPreference = 'Continue' # Default for new PowerShell session



# PackageManagement
## Import
Import-Module -Name 'PackageManagement' -Force

## Output imported version of the module
$(Get-Module -Name 'PackageManagement').'Version'.ToString()



# Assets
## Fictive module
$Name = 'SomeModuleName'
$Version = '1.2.3'



# Get
## Using "Get-InstalledModule"
Get-InstalledModule -Name $Name -RequiredVersion $Version
$?
$Error[0]

## Using "PackageManagement\Get-Package"
PackageManagement\Get-Package -Name $Name -RequiredVersion $Version
$?
$Error[0]



# Uninstall
## Using "Uninstall-Module"
Uninstall-Module -Name $Name -RequiredVersion $Version
$?
$Error[0]

## Using "PackageManagement\Uninstall-Package"
PackageManagement\Uninstall-Package -Name $Name -RequiredVersion $Version
$?
$Error[0]

Output showing wrong cmdlet is reported in the error + $? is not set correctly.

PS C:\Users\olavb> $ErrorActionPreference = 'Continue' # Default for new PowerShell session

PS C:\Users\olavb> Import-Module -Name 'PackageManagement' -Force

PS C:\Users\olavb> $(Get-Module -Name 'PackageManagement').'Version'.ToString()
1.4.6

PS C:\Users\olavb> $Name = 'SomeModuleName'
$Version = '1.2.3'

PS C:\Users\olavb> Get-InstalledModule -Name $Name -RequiredVersion $Version
PackageManagement\Get-Package : No match was found for the specified search criteria and module names 'SomeModuleName'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.3\PSModule.psm1:9423 char:9
+         PackageManagement\Get-Package @PSBoundParameters | Microsoft. ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...lets.GetPackage:GetPackage) [Get-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage
 

PS C:\Users\olavb> $?
True

PS C:\Users\olavb> PackageManagement\Get-Package -Name $Name -RequiredVersion $Version
PackageManagement\Get-Package : No package found for 'SomeModuleName'.
At line:1 char:1
+ PackageManagement\Get-Package -Name $Name -RequiredVersion $Version
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...lets.GetPackage:GetPackage) [Get-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage
 

PS C:\Users\olavb> $?
False

PS C:\Users\olavb> Uninstall-Module -Name $Name -RequiredVersion $Version
PackageManagement\Uninstall-Package : No match was found for the specified search criteria and module names 'SomeModuleName'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.3\PSModule.psm1:12657 char:21
+ ...        $null = PackageManagement\Uninstall-Package @PSBoundParameters
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstall-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.UninstallPackage
 

PS C:\Users\olavb> $?
True

PS C:\Users\olavb> PackageManagement\Uninstall-Package -Name $Name -RequiredVersion $Version
PackageManagement\Uninstall-Package : No package found for 'SomeModuleName'.
At line:1 char:1
+ PackageManagement\Uninstall-Package -Name $Name -RequiredVersion $Ver ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...ninstallPackage:UninstallPackage) [Uninstall-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.UninstallPackage
 

PS C:\Users\olavb> $?
False

PS C:\Users\olavb> 

This all works for the "Find-Package" command.

@o-l-a-v o-l-a-v changed the title "Get-InstalledModule" and "Uninstall-Module" errors is not added to $Error and does not set $? to $false "Get-InstalledModule" and "Uninstall-Module" errors does not set $? to $false Feb 26, 2020
@o-l-a-v o-l-a-v changed the title "Get-InstalledModule" and "Uninstall-Module" errors does not set $? to $false "Get-InstalledModule" and "Uninstall-Module" errors do not set $? to $false Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant