mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
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
This commit is contained in:
@@ -1658,7 +1658,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// This class implements the Start-process command
|
||||
/// </summary>
|
||||
[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);
|
||||
|
||||
|
||||
@@ -132,6 +132,9 @@
|
||||
<data name="ProcessNameForConfirmation" xml:space="preserve">
|
||||
<value>{0} ({1})</value>
|
||||
</data>
|
||||
<data name="StartProcessTarget" xml:space="preserve">
|
||||
<value>{0} {1}</value>
|
||||
</data>
|
||||
<data name="CouldNotEnumerateModules" xml:space="preserve">
|
||||
<value>Cannot enumerate the modules of the "{0}" process.</value>
|
||||
</data>
|
||||
|
||||
@@ -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" {
|
||||
|
||||
Reference in New Issue
Block a user