diff --git a/src/System.Management.Automation/engine/ShellVariable.cs b/src/System.Management.Automation/engine/ShellVariable.cs
index 424be29534..8440845a32 100644
--- a/src/System.Management.Automation/engine/ShellVariable.cs
+++ b/src/System.Management.Automation/engine/ShellVariable.cs
@@ -244,6 +244,14 @@ namespace System.Management.Automation
}
}
+ ///
+ /// Gets the value without triggering debugger check.
+ ///
+ internal virtual object GetValueRaw()
+ {
+ return _value;
+ }
+
///
/// Gets or sets the value of the variable.
///
@@ -796,6 +804,11 @@ namespace System.Management.Automation
}
}
+ internal override object GetValueRaw()
+ {
+ return _tuple.GetValue(_tupleSlot);
+ }
+
internal override void SetValueRaw(object newValue, bool preserveValueTypeSemantics)
{
if (preserveValueTypeSemantics)
diff --git a/src/System.Management.Automation/engine/runtime/Operations/VariableOps.cs b/src/System.Management.Automation/engine/runtime/Operations/VariableOps.cs
index 6799d1bd98..72126c726c 100644
--- a/src/System.Management.Automation/engine/runtime/Operations/VariableOps.cs
+++ b/src/System.Management.Automation/engine/runtime/Operations/VariableOps.cs
@@ -46,6 +46,13 @@ namespace System.Management.Automation
: GetAttributeCollection(attributeAsts);
var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes);
+ if (attributes.Count > 0)
+ {
+ // When there are any attributes, it's possible the value was converted/transformed.
+ // Use 'GetValueRaw' here so the debugger check won't be triggered.
+ value = var.GetValueRaw();
+ }
+
// Marking untrusted values for assignments in 'ConstrainedLanguage' mode is done in
// SessionStateScope.SetVariable.
sessionState.SetVariable(variablePath, var, false, origin);
@@ -81,7 +88,7 @@ namespace System.Management.Automation
null,
Metadata.InvalidValueFailure,
var.Name,
- ((value != null) ? value.ToString() : "$null"));
+ (value != null) ? value.ToString() : "$null");
throw e;
}
diff --git a/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1 b/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1
index 31bc8bc9fc..1cff529502 100644
--- a/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1
+++ b/test/powershell/Language/Scripting/Scripting.Followup.Tests.ps1
@@ -29,4 +29,10 @@ Describe "Scripting.Followup.Tests" -Tags "CI" {
## $arraylist.Clear() should be executed
$arraylist.Count | Should -Be 0
}
+
+ ## fix https://github.com/PowerShell/PowerShell/issues/17165
+ It "([bool] `$var = 42) should return the varaible value" {
+ ([bool]$var = 42).GetType().FullName | Should -Be "System.Boolean"
+ . { ([bool]$var = 42).GetType().FullName } | Should -Be "System.Boolean"
+ }
}