diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index fef419a4f..0616cf040 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -342,7 +342,7 @@ private List ProcessRepositories( allPkgsInstalled.AddRange(installedPkgs); } - if (!_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf") && _pkgNamesToInstall.Count > 0) + if ((!_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf") || (SwitchParameter)_cmdletPassedIn.MyInvocation.BoundParameters["WhatIf"] == false) && _pkgNamesToInstall.Count > 0) { string repositoryWording = repositoryNamesToSearch.Count > 1 ? "registered repositories" : "repository"; _cmdletPassedIn.WriteError(new ErrorRecord( @@ -624,7 +624,7 @@ private List InstallPackages( } // If -WhatIf is passed in, early out. - if (_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf")) + if (_cmdletPassedIn.MyInvocation.BoundParameters.ContainsKey("WhatIf") && (SwitchParameter)_cmdletPassedIn.MyInvocation.BoundParameters["WhatIf"] == true) { return pkgsSuccessfullyInstalled; } diff --git a/src/code/RegisterPSResourceRepository.cs b/src/code/RegisterPSResourceRepository.cs index ddf5cad24..a4994df8a 100644 --- a/src/code/RegisterPSResourceRepository.cs +++ b/src/code/RegisterPSResourceRepository.cs @@ -186,17 +186,20 @@ protected override void ProcessRecord() break; case PSGalleryParameterSet: - try - { - items.Add(PSGalleryParameterSetHelper(Priority, Trusted)); - } - catch (Exception e) + if (PSGallery) { - ThrowTerminatingError(new ErrorRecord( - new PSInvalidOperationException(e.Message), - "ErrorInPSGalleryParameterSet", - ErrorCategory.InvalidArgument, - this)); + try + { + items.Add(PSGalleryParameterSetHelper(Priority, Trusted)); + } + catch (Exception e) + { + ThrowTerminatingError(new ErrorRecord( + new PSInvalidOperationException(e.Message), + "ErrorInPSGalleryParameterSet", + ErrorCategory.InvalidArgument, + this)); + } } break; diff --git a/test/InstallPSResourceTests/InstallPSResourceRepositorySearching.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceRepositorySearching.Tests.ps1 index 46d4c54a6..eea4dae94 100644 --- a/test/InstallPSResourceTests/InstallPSResourceRepositorySearching.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceRepositorySearching.Tests.ps1 @@ -137,4 +137,12 @@ Describe 'Test Install-PSResource for searching and looping through repositories $err | Should -HaveCount 0 $warningVar | Should -Not -BeNullOrEmpty } + + It "install resources from repository should respect WhatIf value of false" { + # Package "test_module" exists in the following repositories (in this order): localRepo, PSGallery, NuGetGallery + $res = Install-PSResource -Name $testModuleName -Repository $PSGalleryName -TrustRepository -SkipDependencyCheck -PassThru -WhatIf:$false + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $testModuleName + $res.Repository | Should -Be $PSGalleryName + } } diff --git a/test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1 b/test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1 index 783457d55..f9c350356 100644 --- a/test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1 +++ b/test/ResourceRepositoryTests/RegisterPSResourceRepository.Tests.ps1 @@ -85,6 +85,12 @@ Describe "Test Register-PSResourceRepository" -tags 'CI' { $res.Priority | Should -Be 50 } + It "register repository with PSGallery switch parameter value of false (PSGalleryParameterSet)" { + Unregister-PSResourceRepository -Name $PSGalleryName + $res = Register-PSResourceRepository -PSGallery:$false -PassThru + $res | Should -BeNullOrEmpty + } + It "register repository with PSGallery, Trusted parameters (PSGalleryParameterSet)" { Unregister-PSResourceRepository -Name $PSGalleryName $res = Register-PSResourceRepository -PSGallery -Trusted -PassThru