diff --git a/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs b/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs index e894ec0119b..aa6c222fdf6 100644 --- a/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs +++ b/src/Microsoft.PowerShell.Security/security/CatalogCommands.cs @@ -95,20 +95,27 @@ protected override void ProcessRecord() { foreach (PathInfo tempPath in SessionState.Path.GetResolvedPSPathFromPSPath(p)) { - paths.Add(tempPath.ProviderPath); + if (ShouldProcess(tempPath.ProviderPath)) + { + paths.Add(tempPath.ProviderPath); + } } } } - string drive = null; - - // resolve catalog destination Path - if (!SessionState.Path.IsPSAbsolute(catalogFilePath, out drive) && !System.IO.Path.IsPathRooted(catalogFilePath)) + // We add 'paths.Count > 0' to support 'ShouldProcess()' + if (paths.Count > 0 ) { - catalogFilePath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(catalogFilePath); - } + string drive = null; + + // resolve catalog destination Path + if (!SessionState.Path.IsPSAbsolute(catalogFilePath, out drive) && !System.IO.Path.IsPathRooted(catalogFilePath)) + { + catalogFilePath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(catalogFilePath); + } - PerformAction(paths, catalogFilePath); + PerformAction(paths, catalogFilePath); + } } /// diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 index f89d32f8dae..daa91ddb55a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/FileCatalog.Tests.ps1 @@ -42,6 +42,27 @@ Describe "Test suite for NewFileCatalogAndTestFileCatalogCmdlets" -Tags "CI" { Context "NewAndTestCatalogTests PositiveTestCases when validation Succeeds" { + It "NewFileCatalogWithSingleFile with WhatIf" { + + $sourcePath = Join-Path $testDataPath '\CatalogTestFile1.mof' + # use existant Path for the directory when .cat file name is not specified + $catalogPath = $testDataPath + $catalogFile = $catalogPath + "\catalog.cat" + + try + { + $null = New-FileCatalog -Path $sourcePath -CatalogFilePath $catalogPath -WhatIf + $result = Test-Path -Path $catalogFile + } + finally + { + Remove-Item $catalogFile -Force -ErrorAction SilentlyContinue + } + + # Validate result properties + $result | Should Be $false + } + It "NewFileCatalogFolder" { $sourcePath = Join-Path $testDataPath 'UserConfigProv\DSCResources\scriptdsc'