I have a powershell script which converts the String and String[] into a BlockScript. It was working until now but it starts to fail suddenly after some windows upgrade.
function Invoke-ForegroundCommand {
param (
[Parameter(Mandatory = $true)]
[scriptblock]
$Command
)
Invoke-Command -ScriptBlock $Command -Verbose
$exitCode = $LASTEXITCODE
}
$javaOptions = @()
$javaOptions + @(
"-Dfile.encoding=UTF8"
"-XX:+HeapDumpOnOutOfMemoryError"
"-XX:+UseG1GC"
"-Xms512m"
"-Xmx2048m"
"--add-opens=java.base/java.nio=ALL-UNNAMED"
"--add-opens=java.base/java.util=ALL-UNNAMED"
"--add-opens=java.base/java.security=ALL-UNNAMED"
)
Invoke-ForegroundCommand -Command "java" -Arguments $javaOpts
It throws the exception “Cannot process argument transformation on parameter ‘Command’. Cannot convert the “java” value of type
| “System.String” to type “System.Management.Automation.ScriptBlock”.”
I have already tried to wrap “java” and $javaOptions into a command block using {} and also tried to create a script block using
$command = "java $javaOptions"
or $command = "java " + ($javaOptions -join " ")
$ScriptBlock = [System.Management.Automation.ScriptBlock]::Create($command)
I could not get it working. Do you know how to get it working?