Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/System.Management.Automation/engine/lang/parserutils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ internal static object ReplaceOperator(ExecutionContext context, IScriptExtent e
pattern = rList[0];
if (rList.Count > 1)
{
substitute = rList[1];
substitute = PSObject.Base(rList[1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment why it is needed? what we break without this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, This is actually a pretty fundamental operation in PowerShell. Anywhere you receive something of type object, you have to deal with the case where it might be wrapped in a PSObject. So before you can use it as string or ScriptBlock, you have to get the base object. The PSObject.Base() method encapsulates this operation. If it's passed a PSObject, it returns the core object otherwise it just returns the object.

}
}
}
Expand Down Expand Up @@ -972,6 +972,9 @@ private static object ReplaceOperatorImpl(ExecutionContext context, string input
{
switch (substitute)
{
case string replacementString:
return regex.Replace(input, replacementString);

case ScriptBlock sb:
MatchEvaluator me = match => {
var result = sb.DoInvokeReturnAsIs(
Expand Down