I have this script file contains:
function run_command
{
param
(
[string] $scriptblock
)
$sb = [Scriptblock]::Create($scriptblock)
Start-Job -ScriptBlock $sb | Wait-Job | Receive-Job
}
$a = "aaa"
$scriptblock = @"
if($a){echo $a}
else {echo nene}
"@
run_command -scriptblock $scriptblock
I have two problem: 1. If the scriptblock contains IF statement, the execution failed with error like "The term 'someofArguments' is not recognized as the name of a cmdlet...".
But without IF statement everything is working fine. So how to send that scriptblock with IF statement?
- How to avoid using backtick in $command with variables $true, $false and mainly $null? Because without backtick they are translated to true,false and nothing.
Thank you.
PS: I found this topics PowerShell function with parameterized script block and similar posts, that are saying to send scriptblock as string and in function itself convert it to type scriptblock.
EDIT: I figure out this solution, but it is not very nice. Do someone know easier solution than put backtick like this?
$scriptblock = @"
`$a=`"$a`"
if(`$a){echo `$a}
else {echo nene}
"@