In 5.1, when the key's value is an expression ( maybe only if ), you see red. while kind of breaks with a stream of errors. foreach does work. try {} catch {} does not work. try {} finally {} works.
This is fixed in modern PowerShell ( at least 7.6.1 ).
@{
Value1 = if ($true) {'SomeValue'}
Value2 = 'OtherValue'
}
The above throws the below.
At line:2 char:38
+ Value1 = if ($true) {'SomeValue'}
+ ~
The hash literal was incomplete.
At line:3 char:5
+ Value2 = 'OtherValue'
+ ~~~~~~
Unexpected token 'Value2' in expression or statement.
At line:5 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : IncompleteHashLiteral
The workaround could be adding a semicolon
@{
Value1 = if ($true) {'SomeValue'};
Value2 = 'OtherValue'
}
or wrapping in sub-expression operator
@{
Value1 = $(if ($true) {'SomeValue'})
Value2 = 'OtherValue'
}
or you can add an else. ( NOTE: Removing $null in else {} to keep the same semantics. See @surfingoldelephant's comments below.
@{
Value1 = if ($true) {'SomeValue'} else {}
Value2 = 'OtherValue'
}
Props to @kborowinski on PowerShell Discord for discovering it. Appears the parser gets "stuck" in the "else/elseif" mode.
In 5.1, when the key's value is an expression ( maybe only
if), you see red.whilekind of breaks with a stream of errors.foreachdoes work.try {} catch {}does not work.try {} finally {}works.This is fixed in modern PowerShell ( at least 7.6.1 ).
The above
throws the below.The workaround could be adding a semicolon
or wrapping in sub-expression operator
or you can add an
else. ( NOTE: Removing$nullinelse {}to keep the same semantics. See @surfingoldelephant's comments below.Props to @kborowinski on PowerShell Discord for discovering it. Appears the parser gets "stuck" in the "else/elseif" mode.