8000 Fix for 'Update-Module fails with ModuleAuthenticodeSignature error f… · PowerShell/PowerShellGetv2@0d57992 · GitHub
[go: up one dir, main page]

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

Commit 0d57992

Browse files
committed
Fix for 'Update-Module fails with ModuleAuthenticodeSignature error for modules with signed PSD1' #8
Fix for 'Properties of AdditionalMetadata are case-sensitive' #7 Changed the ErrorAction to Ignore for few cmdlet usages as they should not show up in ErrorVaraible. For example, error returned by 'Get-Command Test-FileCatalog' should be ignored.
1 parent 406e09d commit 0d57992

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

PowerShellGet/PSGet.Resource.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ ConvertFrom-StringData @'
225225
CatalogFileNotFoundInAvailableModule=Catalog file '{0}' is not found in the contents of the previously-installed module '{1}' with the same name.
226226
CatalogFileNotFoundInNewModule=Catalog file '{0}' is not found in the contents of the module '{1}' being installed.
227227
ValidAuthenticodeSignature=Valid authenticode signature found in the catalog file '{0}' for the module '{1}'.
228+
ValidAuthenticodeSignatureInFile=Valid authenticode signature found in the file '{0}' for the module '{1}'.
228229
ValidatingCatalogSignature=Validating the '{0}' module files for catalog signing using the catalog file '{1}'.
229230
AuthenticodeIssuerMatch=Authenticode issuer '{0}' of the new module '{1}' with version '{2}' matches with the authenticode issuer '{3}' of the previously-installed module '{4}' with version '{5}'.
230231
ValidCatalogSignature=The catalog signature in '{0}' of the module '{1}' is valid and matches with the hash generated from the module contents.

PowerShellGet/PSModule.psm1

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ function Publish-Module
12611261
}
12621262
finally
12631263
{
1264-
Microsoft.PowerShell.Management\Remove-Item $tempModulePath -Force -Recurse -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Confirm:$false -WhatIf:$false
1264+
Microsoft.PowerShell.Management\Remove-Item $tempModulePath -Force -Recurse -ErrorAction Ignore -WarningAction SilentlyContinue -Confirm:$false -WhatIf:$false
12651265
}
12661266
}
12671267
}
@@ -2901,7 +2901,7 @@ function Publish-Script
29012901
}
29022902
finally
29032903
{
2904-
Microsoft.PowerShell.Management\Remove-Item $tempScriptPath -Force -Recurse -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -Confirm:$false -WhatIf:$false
2904+
Microsoft.PowerShell.Management\Remove-Item $tempScriptPath -Force -Recurse -ErrorAction Ignore -WarningAction SilentlyContinue -Confirm:$false -WhatIf:$false
29052905
}
29062906
}
29072907
}
@@ -3528,7 +3528,7 @@ function Install-Script
35283528
{
35293529
# Throw an error if there is a command with the same name and -force is not specified.
35303530
$cmd = Microsoft.PowerShell.Core\Get-Command -Name $scriptName `
3531-
-ErrorAction SilentlyContinue `
3531+
-ErrorAction Ignore `
35323532
-WarningAction SilentlyContinue
35333533
if($cmd)
35343534
{
@@ -3606,7 +3606,7 @@ function Install-Script
36063606
if(-not $Force)
36073607
{
36083608
$cmd = Microsoft.PowerShell.Core\Get-Command -Name $psRepositoryItemInfo.Name `
3609-
-ErrorAction SilentlyContinue `
3609+
-ErrorAction Ignore `
36103610
-WarningAction SilentlyContinue
36113611
if($cmd)
36123612
{
@@ -6157,7 +6157,7 @@ function Check-PSGalleryApiAvailability
61576157
# check internet availability first
61586158
$connected = $false
61596159
$microsoftDomain = 'www.microsoft.com'
6160-
if(Get-Command Microsoft.PowerShell.Management\Test-Connection -ErrorAction SilentlyContinue)
6160+
if(Get-Command Microsoft.PowerShell.Management\Test-Connection -ErrorAction Ignore)
61616161
{
61626162
$connected = Microsoft.PowerShell.Management\Test-Connection -ComputerName $microsoftDomain -Count 1 -Quiet
61636163
}
@@ -7147,18 +7147,18 @@ function New-PSGetItemInfo
71477147
}
71487148
}
71497149

7150-
$additionalMetadata = New-Object -TypeName System.Collections.Hashtable
7150+
$additionalMetadata = Microsoft.PowerShell.Utility\New-Object PSCustomObject -Property ([ordered]@{})
71517151
foreach ( $key in $swid.Metadata.Keys.LocalName)
71527152
{
7153-
if (!$additionalMetadata.ContainsKey($key))
7154-
{
7155-
$additionalMetadata.Add($key, (Get-First $swid.Metadata[$key]) )
7156-
}
7153+
Microsoft.PowerShell.Utility\Add-Member -InputObject $additionalMetadata `
7154+
-MemberType NoteProperty `
7155+
-Name $key `
7156+
-Value (Get-First $swid.Metadata[$key])
71577157
}
7158-
7159-
if($additionalMetadata.ContainsKey('ItemType'))
7158+
7159+
if(Get-Member -InputObject $additionalMetadata -Name 'ItemType')
71607160
{
7161-
$Type = $additionalMetadata['ItemType']
7161+
$Type = $additionalMetadata.'ItemType'
71627162
}
71637163
elseif($userTags -contains 'PSModule')
71647164
{
@@ -7420,7 +7420,7 @@ function Install-NuGetClientBinaries
74207420
# Using Get-Command cmdlet, get the location of NuGet.exe if it is available under $env:PATH.
74217421
# NuGet.exe does not work if it is under $env:WINDIR, so skip it from the Get-Command results.
74227422
$nugetCmd = Microsoft.PowerShell.Core\Get-Command -Name $script:NuGetExeName `
7423-
-ErrorAction SilentlyContinue `
7423+
-ErrorAction Ignore `
74247424
-WarningAction SilentlyContinue |
74257425
Microsoft.PowerShell.Core\Where-Object {
74267426
$_.Path -and
@@ -8649,7 +8649,7 @@ function Get-ExportedDscResources
86498649

86508650
$dscResources = @()
86518651

8652-
if(Get-Command -Name Get-DscResource -Module PSDesiredStateConfiguration -ErrorAction SilentlyContinue)
8652+
if(Get-Command -Name Get-DscResource -Module PSDesiredStateConfiguration -ErrorAction Ignore)
86538653
{
86548654
$OldPSModulePath = $env:PSModulePath
86558655

@@ -10582,7 +10582,7 @@ function Install-PackageUtility
1058210582
-not $Force)
1058310583
{
1058410584
$cmd = Microsoft.PowerShell.Core\Get-Command -Name $packageName `
10585-
-ErrorAction SilentlyContinue `
10585+
-ErrorAction Ignore `
1058610586
-WarningAction SilentlyContinue
1058710587
if($cmd)
1058810588
{
@@ -13948,15 +13948,7 @@ function Validate-ModuleAuthenticodeSignature
1394813948
[Switch]
1394913949
$SkipPublisherCheck
1395013950
)
13951-
13952-
$InstalledModuleDetails = $null
13953-
$InstalledModuleInfo = Test-ModuleInstalled -Name $CurrentModuleInfo.Name
13954-
if($InstalledModuleInfo)
13955-
{
13956-
$InstalledModuleDetails = Get-InstalledModuleAuthenticodeSignature -InstalledModuleInfo $InstalledModuleInfo `
13957-
-InstallLocation $InstallLocation
13958-
}
13959-
13951+
1396013952
# Skip the publisher check when -SkipPublisherCheck is specified and
1396113953
# it is not an update operation.
1396213954
if(-not $IsUpdateOperation -and $SkipPublisherCheck)
@@ -13967,12 +13959,21 @@ function Validate-ModuleAuthenticodeSignature
1396713959
return $true
1396813960
}
1396913961

13962+
$InstalledModuleDetails = $null
13963+
$InstalledModuleInfo = Test-ModuleInstalled -Name $CurrentModuleInfo.Name
13964+
if($InstalledModuleInfo)
13965+
{
13966+
$InstalledModuleDetails = Get-InstalledModuleAuthenticodeSignature -InstalledModuleInfo $InstalledModuleInfo `
13967+
-InstallLocation $InstallLocation
13968+
}
13969+
1397013970
# Validate the catalog signature for the current module being installed.
1397113971
$ev = $null
1397213972
$CurrentModuleDetails = ValidateAndGet-AuthenticodeSignature -ModuleInfo $CurrentModuleInfo -ErrorVariable ev
1397313973

1397413974
if($ev)
1397513975
{
13976+
Write-Debug "$ev"
1397613977
return $false
1397713978
}
1397813979

@@ -14122,7 +14123,7 @@ function Validate-ModuleCommandAlreadyAvailable
1412214123
$CommandNamesWithWildcards = $CommandNames | Microsoft.PowerShell.Core\Foreach-Object { "$_*" }
1412314124

1412414125
$AvailableCommand = Microsoft.PowerShell.Core\Get-Command -Name $CommandNamesWithWildcards `
14125-
-ErrorAction SilentlyContinue `
14126+
-ErrorAction Ignore `
1412614127
-WarningAction SilentlyContinue |
1412714128
Microsoft.PowerShell.Core\Where-Object { ($CommandNames -contains $_.Name) -and
1412814129
($_.ModuleName -ne $CurrentModuleInfo.Name) } |
@@ -14159,6 +14160,7 @@ function ValidateAndGet-AuthenticodeSignature
1415914160

1416014161
$ModuleName = $ModuleInfo.Name
1416114162
$ModuleBasePath = $ModuleInfo.ModuleBase
14163+
$ModuleManifestName = "$ModuleName.psd1"
1416214164
$CatalogFileName = "$ModuleName.cat"
1416314165
$CatalogFilePath = Microsoft.PowerShell.Management\Join-Path -Path $ModuleBasePath -ChildPath $CatalogFileName
1416414166

@@ -14183,7 +14185,7 @@ function ValidateAndGet-AuthenticodeSignature
1418314185

1418414186
Write-Verbose -Message ($LocalizedData.ValidAuthenticodeSignature -f @($CatalogFileName, $ModuleName))
1418514187

14186-
if(Get-Command -Name Test-FileCatalog -Module Microsoft.PowerShell.Security -ErrorAction SilentlyContinue)
14188+
if(Get-Command -Name Test-FileCatalog -Module Microsoft.PowerShell.Security -ErrorAction Ignore)
1418714189
{
1418814190
Write-Verbose -Message ($LocalizedData.ValidatingCatalogSignature -f @($ModuleName, $CatalogFileName))
1418914191

@@ -14214,6 +14216,29 @@ function ValidateAndGet-AuthenticodeSignature
1421414216
else
1421514217
{
1421614218
Write-Verbose -Message ($LocalizedData.CatalogFileNotFoundInNewModule -f ($CatalogFileName, $ModuleName))
14219+
14220+
$message = "Using the '{0}' file for getting the authenticode signature." -f ($ModuleManifestName)
14221+
Write-Debug -Message $message
14222+
14223+
$AuthenticodeSignature = Microsoft.PowerShell.Security\Get-AuthenticodeSignature -FilePath $ModuleInfo.Path
14224+
14225+
if($AuthenticodeSignature)
14226+
{
14227+
if($AuthenticodeSignature.Status -eq "Valid")
14228+
{
14229+
Write-Verbose -Message ($LocalizedData.ValidAuthenticodeSignatureInFile -f @($ModuleManifestName, $ModuleName))
14230+
}
14231+
elseif($AuthenticodeSignature.Status -ne "NotSigned")
14232+
{
14233+
$message = $LocalizedData.InvalidModuleAuthenticodeSignature -f ($ModuleName, $ModuleManifestName)
14234+
ThrowError -ExceptionName 'System.InvalidOperationException' `
14235+
-ExceptionMessage $message `
14236+
-ErrorId 'InvalidAuthenticodeSignature' `
14237+
-CallerPSCmdlet $PSCmdlet `
14238+
-ErrorCategory InvalidOperation
14239+
return
14240+
}
14241+
}
1421714242
}
1421814243

1421914244
if($AuthenticodeSignature)

PowerShellGet/PowerShellGet.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@{
22
RootModule = 'PSModule.psm1'
3-
ModuleVersion = '1.1.0.0'
3+
ModuleVersion = '1.1.1.0'
44
GUID = '1d73a601-4a6c-43c5-ba3f-619b18bbb404'
55
Author = 'Microsoft Corporation'
66
CompanyName = 'Microsoft Corporation'

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ branches:
2525
test_script:
2626
- SET PATH=c:\Program Files\WindowsPowerShell\Modules\;%PATH%;
2727
- ps: |
28-
$ModuleVersion = '1.1.0.0'
28+
$ModuleVersion = '1.1.1.0'
2929
$InstallLocation = "$Env:ProgramFiles\WindowsPowerShell\Modules\PowerShellGet"
3030
if($PSVersionTable.PSVersion -ge '5.0.0')
3131
{

0 commit comments

Comments
 (0)
0