From f4b075c856e1e25fd08478d39f38117ffc67b689 Mon Sep 17 00:00:00 2001 From: Sarith Sutha Date: Fri, 8 Sep 2017 01:18:13 -0400 Subject: [PATCH] Add -WhatIf switch to Start-Process cmdlet (#4735) * Add -WhatIf switch to Start-Process cmdlet * Add test for the -WhatIf switch * Added a test to ensure that using a whatif switch prevents the action from being performed. * Incorporated code review comments * merged two tests into one as suggested * Included the error action at the end of the cmdlet * Remove extra space before `StringUtil` and new line * Renamed the resource string id ProcessStartInfo to StartProcessTarget --- .../commands/management/Process.cs | 5 ++++- .../resources/ProcessResources.resx | 3 +++ .../Start-Process.Tests.ps1 | 10 ++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 6023199fcc..f6d7b8c235 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -1658,7 +1658,7 @@ namespace Microsoft.PowerShell.Commands /// /// This class implements the Start-process command /// - [Cmdlet(VerbsLifecycle.Start, "Process", DefaultParameterSetName = "Default", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135261")] + [Cmdlet(VerbsLifecycle.Start, "Process", DefaultParameterSetName = "Default", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135261")] [OutputType(typeof(Process))] public sealed class StartProcessCommand : PSCmdlet, IDisposable { @@ -2051,6 +2051,9 @@ namespace Microsoft.PowerShell.Commands startInfo.WindowStyle = _windowstyle; } + string targetMessage = StringUtil.Format(ProcessResources.StartProcessTarget, startInfo.FileName, startInfo.Arguments.Trim()); + if (!ShouldProcess(targetMessage)) { return; } + //Starts the Process Process process = Start(startInfo); diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx index 558f22cafa..a95f7d3720 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/ProcessResources.resx @@ -132,6 +132,9 @@ {0} ({1}) + + {0} {1} + Cannot enumerate the modules of the "{0}" process. diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 index 6a52123ec0..09ad4e1f4f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Start-Process.Tests.ps1 @@ -104,6 +104,16 @@ Describe "Start-Process" -Tags @("Feature") { $process.Name | Should Be "notepad" $process | Stop-Process } + + It "Should be able to use the -WhatIf switch without performing the actual action" { + $pingOutput = Join-Path $TestDrive "pingOutput.txt" + { Start-Process -Wait $pingCommand -ArgumentList $pingParam -RedirectStandardOutput $pingOutput -WhatIf -ErrorAction Stop } | Should Not Throw + $pingOutput | Should Not Exist + } + + It "Should return null when using -WhatIf switch with -PassThru" { + Start-Process $pingCommand -ArgumentList $pingParam -PassThru -WhatIf | Should Be $null + } } Describe "Start-Process tests requiring admin" -Tags "Feature","RequireAdminOnWindows" {