aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
235 stars 78 forks source link

Exception calling Install-AWSToolsModule with -Cleanup #161

Closed ralish closed 3 years ago

ralish commented 4 years ago

Expected Behavior

Invoking Install-AWSToolsModule with the -Cleanup parameter should always work, even if no clean-up needs to be performed, or no AWS.Tools modules have been installed (excluding the Installer).

Current Behavior

In some circumstances invoking Install-AWSToolsModule with the -Cleanup parameter can result in an unhandled exception:

Install-AWSToolsModule -Name S3 -CleanUp -Force -Verbose -Debug
VERBOSE: [Install-AWSToolsModule] ConfirmPreference=High WhatIfPreference=False VerbosePreference=Continue Force=True Name=(S3) RequiredVersion= SkipUpdate=False CleanUp=True
VERBOSE: [Find-AWSToolsModule] ConfirmPreference=High WhatIfPreference=False VerbosePreference=Continue Name=(AWS.Tools.S3)
VERBOSE: [Find-AWSToolsModule] End
VERBOSE: [Install-AWSToolsModule] Installing AWS Tools version 4.0.5.0
VERBOSE: [Install-AWSToolsModule] Searching installed modules
VERBOSE: [Install-AWSToolsModule] Removing already installed modules from the. Final list of modules to install: (AWS.Tools.S3)
VERBOSE: [Install-AWSToolsModule] Create folder for temporary repository C:\Users\<removed>\AppData\Local\Temp\34zf4gxo.qn3
VERBOSE: [Install-AWSToolsModule] Registering temporary repository AWSToolsTemp
VERBOSE: [Install-AWSToolsModule] Downloading modules to temporary repository
VERBOSE: [Install-AWSToolsModule] Downloading modules (AWS.Tools.S3)
VERBOSE: [Install-AWSToolsModule] Downloading module AWS.Tools.S3 to C:\Users\<removed>\AppData\Local\Temp\34zf4gxo.qn3
VERBOSE: [Get-AWSToolsModuleDependenciesAndValidate] ConfirmPreference=None WhatIfPreference=False VerbosePreference=Continue Name=AWS.Tools.S3 Path=C:\Users\<removed>\AppData\Local\Temp\34zf4gxo.qn3\AWS.Tools.S3.4.0.5.0.nupkg
VERBOSE: [Get-AWSToolsModuleDependenciesAndValidate] Manifest signature correctly validated
VERBOSE: [Get-AWSToolsModuleDependenciesAndValidate] Found dependency AWS.Tools.Common
VERBOSE: [Get-AWSToolsModuleDependenciesAndValidate] End
VERBOSE: [Install-AWSToolsModule] Downloading module AWS.Tools.Common to C:\Users\<removed>\AppData\Local\Temp\34zf4gxo.qn3
VERBOSE: [Get-AWSToolsModuleDependenciesAndValidate] ConfirmPreference=None WhatIfPreference=False VerbosePreference=Continue Name=AWS.Tools.Common Path=C:\Users\<removed>\AppData\Local\Temp\34zf4gxo.qn3\AWS.Tools.Common.4.0.5.0.nupkg
VERBOSE: [Get-AWSToolsModuleDependenciesAndValidate] Manifest signature correctly validated
VERBOSE: [Get-AWSToolsModuleDependenciesAndValidate] End
VERBOSE: [Install-AWSToolsModule] Installing modules (AWS.Tools.S3)
Installing module AWS.Tools.S3 version 4.0.5.0
VERBOSE: [Install-AWSToolsModule] Modules install complete
VERBOSE: [Install-AWSToolsModule] Unregistering temporary repository AWSToolsTemp
VERBOSE: [Install-AWSToolsModule] Delete repository folder C:\Users\<removed>\AppData\Local\Temp\34zf4gxo.qn3
VERBOSE: [Uninstall-AWSToolsModule] ConfirmPreference=None WhatIfPreference=False VerbosePreference=Continue Force=False
VERBOSE: [Uninstall-AWSToolsModule] Searching installed modules
The property 'Version' cannot be found on this object. Verify that the property exists.
    + CategoryInfo          : NotSpecified: (:) [Where-Object], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict,Microsoft.PowerShell.Commands.WhereObjectCommand

This appears to be due to the internal Get-AWSToolsModule function not returning any objects if there are no AWS.Tools modules installed. The function filters out the AWS.Tools.Installer module.

The absence of any returned objects means the subsequent code operates on a null object. While the variable is explicitly typed as a PSModuleInfo array, as no objects are ever returned from the function it's never initialised. The subsequent code attempts to iterate a null object and access non-existent properties on the array members.

Possible Solution

Either explicitly check that the array variable isn't null or ensure it's always initialised.

Steps to Reproduce (for bugs)

On a system which does not have any existing AWS.Tools.* modules installed:

Install-Module -Name AWS.Tools.Installer -Force
Install-AWSToolsModule -Name S3 -Cleanup -Force

Context

Habitually using the -Cleanup parameter in Install-AWSToolsModule invocations as well as various script logic in continuous integration scenarios. Manually checking for the presence of any existing AWS.Tools.* modules (minus the installer itself) should not be required.

Your Environment

ralish commented 4 years ago

On further investigation this issue is actually caused by #162. The broken module installations mean the internal Get-AWSToolsModule never returns any results. However, I'm unsure if fixing #162 would fix all possible cases of this issue. Some additional error handling may make sense to add regardless.

ashishdhingra commented 3 years ago

This should be fixed once fix for https://github.com/aws/aws-tools-for-powershell/issues/194 is implemented.

ashishdhingra commented 3 years ago

This should be fixed in AWS.Tools.Installer 1.0.2.1

ThisIsArnab commented 3 years ago

This issue is still present for AWS.Tools.Installer 1.0.2.1. For instance the folder name for EC2 module is missing the 4th revision (last part). It is 4.1.12 instead of 4.1.12.0

ashishdhingra commented 3 years ago

This issue is still present for AWS.Tools.Installer 1.0.2.1. For instance the folder name for EC2 module is missing the 4th revision (last part). It is 4.1.12 instead of 4.1.12.0

@ThisIsArnab This behaviour is sometimes seen on Windows PowerShell 5.0 and we cannot control this behaviour. The issue disappears after certain number of retries.