paulomarquesc / AzureRmStorageTable

MIT License
40 stars 28 forks source link

Change Get-Alias -Name to Get-Alias | Where-Object Name #70

Closed mvaneijken closed 3 years ago

mvaneijken commented 3 years ago

Describe the PR Request Import-Module AzTable fills the $Error variable with System.Management.Automation.ItemNotFoundException exceptions like: Get-Alias : This command cannot find a matching alias because an alias with the name 'Remove-AzureStorageTableRow' does not exist.. The error sources in file AzureRmStorageTableCoreHelper.psm1

Although the -ErrorAction "SilentlyContinue" is used in that file, the error is still recorded in the $Error variable.

When using this module in Azure Automation it still fails reporting this error.

I would propose to make the following change: Replace the lines containing If (-not (Get-Alias -Name <AliasName> -ErrorAction SilentlyContinue)) to If (-not (Get-Alias | Where-Object Name -eq "<AliasName>" ))

Error Message

Get-Alias : This command cannot find a matching alias because an alias with the name 'Remove-AzureStorageTableRow' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:901 char:11
+ If (-not (Get-Alias -Name Remove-AzureStorageTableRow -ErrorAction Si ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-AzureStorageTableRow:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Update-AzureStorageTableRow' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:896 char:11
+ If (-not (Get-Alias -Name Update-AzureStorageTableRow -ErrorAction Si ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Update-AzureStorageTableRow:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Get-AzureStorageTableRow' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:891 char:11
+ If (-not (Get-Alias -Name Get-AzureStorageTableRow -ErrorAction Silen ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureStorageTableRow:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Get-AzureStorageTableRowByCustomFilter' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:886 char:11
+ If (-not (Get-Alias -Name Get-AzureStorageTableRowByCustomFilter -Err ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureStorageTableRowByCustomFilter:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Get-AzureStorageTableRowByColumnName' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:881 char:11
+ If (-not (Get-Alias -Name Get-AzureStorageTableRowByColumnName -Error ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureStorageTableRowByColumnName:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Get-AzureStorageTableRowByPartitionKeyRowKey' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:876 char:11
+ If (-not (Get-Alias -Name Get-AzureStorageTableRowByPartitionKeyRowKe ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureStorag...titionKeyRowKey:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Get-AzureStorageTableRowByPartitionKey' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:871 char:11
+ If (-not (Get-Alias -Name Get-AzureStorageTableRowByPartitionKey -Err ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureStorageTableRowByPartitionKey:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Get-AzureStorageTableRowAll' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:866 char:11
+ If (-not (Get-Alias -Name Get-AzureStorageTableRowAll -ErrorAction Si ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureStorageTableRowAll:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Get-AzureStorageTableTable' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:861 char:11
+ If (-not (Get-Alias -Name Get-AzureStorageTableTable -ErrorAction Sil ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureStorageTableTable:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Add-AzureStorageTableRow' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:856 char:11
+ If (-not (Get-Alias -Name Add-AzureStorageTableRow -ErrorAction Silen ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Add-AzureStorageTableRow:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

Get-Alias : This command cannot find a matching alias because an alias with the name 'Add-StorageTableRow' does not exist.
At c:\Users\user\Documents\WindowsPowerShell\Modules\AzTable\2.1.0\AzureRmStorageTableCoreHelper.psm1:851 char:11
+ If (-not (Get-Alias -Name Add-StorageTableRow -ErrorAction SilentlyCo ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Add-StorageTableRow:String) [Get-Alias], ItemNotFoundException
    + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

PowerShell Version

Name                           Value                                                                                                                                                                                                        
----                           -----                                                                                                                                                                                                        
PSVersion                      5.1.19041.1237                                                                                                                                                                                               
PSEdition                      Desktop                                                                                                                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                      
BuildVersion                   10.0.19041.1237                                                                                                                                                                                              
CLRVersion                     4.0.30319.42000                                                                                                                                                                                              
WSManStackVersion              3.0                                                                                                                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                          
SerializationVersion           1.1.0.1                                                                                                                                                                                                      

More information

paulomarquesc commented 3 years ago

Hi @mvaneijken, thanks for finding this out, good catch.

I tested a better alternative which removing the if statements completely and adding -ErrorAction SilentlyContinue to the New-Alias directly, this one does fill up $Error, not even in the event of unloading and loading the module back:

New-Alias -Name Add-StorageTableRow -Value Add-AzTableRow -ErrorAction SilentlyContinue
New-Alias -Name Add-AzureStorageTableRow -Value Add-AzTableRow -ErrorAction SilentlyContinue
New-Alias -Name Get-AzureStorageTableTable -Value Get-AzTableTable -ErrorAction SilentlyContinue
New-Alias -Name Get-AzureStorageTableRowAll -Value Get-AzTableRowAll -ErrorAction SilentlyContinue
New-Alias -Name Get-AzureStorageTableRowByPartitionKey -Value Get-AzTableRowByPartitionKey -ErrorAction SilentlyContinue
New-Alias -Name Get-AzureStorageTableRowByPartitionKeyRowKey -Value Get-AzTableRowByPartitionKeyRowKey -ErrorAction SilentlyContinue
New-Alias -Name Get-AzureStorageTableRowByColumnName -Value Get-AzTableRowByColumnName -ErrorAction SilentlyContinue
New-Alias -Name Get-AzureStorageTableRowByCustomFilter -Value Get-AzTableRowByCustomFilter -ErrorAction SilentlyContinue
New-Alias -Name Get-AzureStorageTableRow -Value Get-AzTableRow -ErrorAction SilentlyContinue
New-Alias -Name Update-AzureStorageTableRow -Value Update-AzTableRow -ErrorAction SilentlyContinue
New-Alias -Name Remove-AzureStorageTableRow -Value Remove-AzTableRow -ErrorAction SilentlyContinue

This is the result:

PS C:\Users\user> import-module aztable                                                                                                                                                   
PS C:\Users\user> $Error                                                                                                                                                                  
PS C:\Users\user> import-module aztable                                                                                                                                                   
PS C:\Users\user> $error                                                                                                                                                                  
PS C:\Users\user> Remove-Module aztable                                                                                                                                                   
PS C:\Users\user> get-module                                                                                                                                                              

ModuleType Version    Name                                ExportedCommands                                                                                                                    
---------- -------    ----                                ----------------                                                                                                                    
Script     0.7.0      Az.Profile                          {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount...}                                                         
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}                                                                  
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}                                           
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}                                                                           
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}                                                     
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler...}                          

PS C:\Users\user> import-module aztable                                                                                                                                                   
PS C:\Users\user> $error                                                                                                                                                                  
PS C:\Users\user> get-module                                                                                                                                                              

ModuleType Version    Name                                ExportedCommands                                                                                                                    
---------- -------    ----                                ----------------                                                                                                                    
Script     0.7.0      Az.Profile                          {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount...}                                                         
Manifest   2.1.0      aztable                             {Add-AzADGroupMember, Add-AzRmStorageContainerLegalHold, Add-AzStorageAccountNetworkRule, Disable-AzStorageDeleteRetentionPolicy...}
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}                                                                  
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}                                           
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}                                                                           
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}                                                     
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler...}                          

If you want still want to contribute, please apply only these changes to your PR and we can go from there.

mvaneijken commented 3 years ago

That's also a possibility indeed. The goal of this PR was to preserve the routines were built-in because they are there probably for a reason.

I'll test it and update the PR later in the next couple days.

mvaneijken commented 3 years ago

Tested and OK:

import-module .\AzureRmStorageTable.psd1
get-module AzureRmStorageTable

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   2.1.0      AzureRmStorageTable                 {Add-AzADGroupMember, Add-AzRmStorageContainerLegalHold, A...

$Error

import-module .\AzureRmStorageTable.psd1
get-module AzureRmStorageTable

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   2.1.0      AzureRmStorageTable                 {Add-AzADGroupMember, Add-AzRmStorageContainerLegalHold, A...

$error

Remove-Module AzureRmStorageTable
get-module AzureRmStorageTable
$error

import-module .\AzureRmStorageTable.psd1 -force
get-module AzureRmStorageTable

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   2.1.0      AzureRmStorageTable                 {Add-AzADGroupMember, Add-AzRmStorageContainerLegalHold, A...

$Error

import-module .\AzureRmStorageTable.psd1 -force
get-module AzureRmStorageTable

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   2.1.0      AzureRmStorageTable                 {Add-AzADGroupMember, Add-AzRmStorageContainerLegalHold, A...

$error

Remove-Module AzureRmStorageTable
get-module AzureRmStorageTable
$Error 
paulomarquesc commented 3 years ago

Thanks @mvaneijken, this is now merged.