ExecutionContext.InvokeCommand.ExpandString throws exception in PowerShell 4.0

Question:

I have a script as below

[string]$newValue = Get-ConfigurationByType $setting.Value

After this line, the value of $newValue is

In the loop, I call ExpandString

foreach ($partner in $partners)
{
$partnerSpecificValue =$ExecutionContext.InvokeCommand.ExpandString($newValue)
}

It throws exception

But when I try to input hard-code string, it returns expected result without exception

The value of $partnerSpecificValue is

Does anyone know a workaround to resolve this bug? Thank you very much. I am running PowerShell v4.0 on Windows Server 2012 R2.

Answer:

I have a workaround for this bug.

https://social.technet.microsoft.com/Forums/en-US/5efc52d2-619c-4063-9a47-bc646c45e882/executioncontextinvokecommandexpandstring-throws-exception-in-powershell-40?forum=winserverpowershell

Source:

ExecutionContext.InvokeCommand.ExpandString throws exception in PowerShell 4.0 by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply