Skip to content

Commit db1b309

Browse files
vexx32daxian-dbw
authored andcommitted
Correctly Report impact level when SupportsShouldProcess is not set to 'true' (PowerShell#8209)
Major changes are: - Make all commands return 'ConfirmImpact.None' if `SupportsShouldProcess` is not set to `true`. - Update some cmdlets to explicitly use `ConfirmImpact.Low`. - Update `DefaultCommands.Tests.ps1` to test for 'ConfirmImpact' level.
1 parent ff83206 commit db1b309

10 files changed

Lines changed: 220 additions & 203 deletions

File tree

src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,8 @@ protected override void ProcessRecord()
10761076
/// <summary>
10771077
/// Mounts a drive in the Monad namespace.
10781078
/// </summary>
1079-
[Cmdlet(VerbsCommon.New, "PSDrive", SupportsShouldProcess = true, SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113357")]
1079+
[Cmdlet(VerbsCommon.New, "PSDrive", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
1080+
SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113357")]
10801081
public class NewPSDriveCommand : CoreCommandWithCredentialsBase
10811082
{
10821083
#region Command parameters

src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewAliasCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace Microsoft.PowerShell.Commands
1010
/// <summary>
1111
/// The implementation of the "new-alias" cmdlet.
1212
/// </summary>
13-
[Cmdlet(VerbsCommon.New, "Alias", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113352")]
13+
[Cmdlet(VerbsCommon.New, "Alias", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
14+
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113352")]
1415
[OutputType(typeof(AliasInfo))]
1516
public class NewAliasCommand : WriteAliasCommandBase
1617
{

src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace Microsoft.PowerShell.Commands
1010
/// <summary>
1111
/// The implementation of the "New-TemporaryFile" cmdlet.
1212
/// </summary>
13-
[Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=821836")]
13+
[Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
14+
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=821836")]
1415
[OutputType(typeof(System.IO.FileInfo))]
1516
public class NewTemporaryFileCommand : Cmdlet
1617
{

src/Microsoft.PowerShell.Commands.Utility/commands/utility/Update-TypeData.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace Microsoft.PowerShell.Commands
1717
/// <summary>
1818
/// This class implements update-typeData command.
1919
/// </summary>
20-
[Cmdlet(VerbsData.Update, "TypeData", SupportsShouldProcess = true, DefaultParameterSetName = FileParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113421")]
20+
[Cmdlet(VerbsData.Update, "TypeData", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
21+
DefaultParameterSetName = FileParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113421")]
2122
public class UpdateTypeDataCommand : UpdateData
2223
{
2324
#region dynamic type set
@@ -883,7 +884,8 @@ private void ProcessTypeFiles()
883884
/// <summary>
884885
/// This class implements update-typeData command.
885886
/// </summary>
886-
[Cmdlet(VerbsData.Update, "FormatData", SupportsShouldProcess = true, DefaultParameterSetName = FileParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113420")]
887+
[Cmdlet(VerbsData.Update, "FormatData", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
888+
DefaultParameterSetName = FileParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113420")]
887889
public class UpdateFormatDataCommand : UpdateData
888890
{
889891
/// <summary>

src/Microsoft.PowerShell.Commands.Utility/commands/utility/Var.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ protected override void ProcessRecord()
377377
/// <summary>
378378
/// Class implementing new-variable command.
379379
/// </summary>
380-
[Cmdlet(VerbsCommon.New, "Variable", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113361")]
380+
[Cmdlet(VerbsCommon.New, "Variable", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
381+
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113361")]
381382
public sealed class NewVariableCommand : VariableCommandBase
382383
{
383384
#region parameters

src/System.Management.Automation/engine/Attributes.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,19 @@ public bool SupportsTransactions
381381

382382
private bool _supportsTransactions = false;
383383

384+
private ConfirmImpact _confirmImpact = ConfirmImpact.Medium;
385+
384386
/// <summary>
385387
/// Gets and sets a ConfirmImpact value that indicates
386388
/// the "destructiveness" of the operation and when it
387389
/// should be confirmed. This should only be used when
388390
/// SupportsShouldProcess is specified.
389391
/// </summary>
390-
public ConfirmImpact ConfirmImpact { get; set; } = ConfirmImpact.Medium;
392+
public ConfirmImpact ConfirmImpact
393+
{
394+
get => SupportsShouldProcess ? _confirmImpact : ConfirmImpact.None;
395+
set => _confirmImpact = value;
396+
}
391397

392398
/// <summary>
393399
/// Gets and sets a HelpUri value that indicates
@@ -1134,12 +1140,12 @@ public ValidateRangeAttribute(ValidateRangeKind kind) : base()
11341140

11351141
private void ValidateRange(object element, ValidateRangeKind rangeKind)
11361142
{
1137-
Type commonType = GetCommonType(typeof(int),element.GetType());
1143+
Type commonType = GetCommonType(typeof(int), element.GetType());
11381144
if (commonType == null)
11391145
{
1140-
throw new ValidationMetadataException(
1146+
throw new ValidationMetadataException(
11411147
"ValidationRangeElementType",
1142-
null,
1148+
innerException: null,
11431149
Metadata.ValidateRangeElementType,
11441150
element.GetType().Name,
11451151
typeof(int).Name);
@@ -1213,7 +1219,7 @@ private void ValidateRange(object element, ValidateRangeKind rangeKind)
12131219
}
12141220

12151221
break;
1216-
}
1222+
}
12171223
}
12181224

12191225
private void ValidateRange(object element)
@@ -2100,7 +2106,8 @@ protected override void Validate(object arguments, EngineIntrinsics engineIntrin
21002106
// because a value-type value cannot be null.
21012107
if (!isEmpty && !isElementValueType)
21022108
{
2103-
do {
2109+
do
2110+
{
21042111
object element = ienum.Current;
21052112
if (element == null || element == AutomationNull.Value)
21062113
{

src/System.Management.Automation/engine/InternalCommands.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ public object GetValue(PSObject inputObject, string propertyName)
5757
/// to each element of the pipeline.
5858
/// </summary>
5959
[SuppressMessage("Microsoft.PowerShell", "PS1012:CallShouldProcessOnlyIfDeclaringSupport")]
60-
[Cmdlet("ForEach", "Object", SupportsShouldProcess = true, DefaultParameterSetName = "ScriptBlockSet",
61-
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113300", RemotingCapability = RemotingCapability.None)]
60+
[Cmdlet("ForEach", "Object", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
61+
DefaultParameterSetName = "ScriptBlockSet", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113300",
62+
RemotingCapability = RemotingCapability.None)]
6263
public sealed class ForEachObjectCommand : PSCmdlet
6364
{
6465
/// <summary>

src/System.Management.Automation/engine/Modules/NewModuleManifestCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ namespace Microsoft.PowerShell.Commands
2424
/// <summary>
2525
/// Cmdlet to create a new module manifest file.
2626
/// </summary>
27-
[Cmdlet(VerbsCommon.New, "ModuleManifest", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=141555")]
27+
[Cmdlet(VerbsCommon.New, "ModuleManifest", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
28+
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=141555")]
2829
[OutputType(typeof(string))]
2930
public sealed class NewModuleManifestCommand : PSCmdlet
3031
{

src/System.Management.Automation/engine/remoting/commands/ReceivePSSession.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ namespace Microsoft.PowerShell.Commands
6262
/// > $job = Receive-PSSession -ComputerName ServerOne -Name SessionName -OutTarget Job.
6363
/// </summary>
6464
[SuppressMessage("Microsoft.PowerShell", "PS1012:CallShouldProcessOnlyIfDeclaringSupport")]
65-
[Cmdlet(VerbsCommunications.Receive, "PSSession", SupportsShouldProcess = true, DefaultParameterSetName = ReceivePSSessionCommand.SessionParameterSet,
66-
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217037", RemotingCapability = RemotingCapability.OwnedByCommand)]
65+
[Cmdlet(VerbsCommunications.Receive, "PSSession", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Low,
66+
DefaultParameterSetName = ReceivePSSessionCommand.SessionParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=217037",
67+
RemotingCapability = RemotingCapability.OwnedByCommand)]
6768
public class ReceivePSSessionCommand : PSRemotingCmdlet
6869
{
6970
#region Parameters

0 commit comments

Comments
 (0)