powershell join scriptblocks

1.5k Views Asked by At

so I a have this huge set of scripts made of scriptblocks in files. The problem is that some functionality is repeated in different scriptblocks, so I am thinking that it would be more elegant if I could split the scriptblocks to even smaller parts and join them as necessary. When I tried to join them just with + operator it does not work though. Any ideas if that's possible?

$sb1={some commands}
$sb2={some more commands}
$sb3=$sb1+$sb2
invoke-command $sb3
1

There are 1 best solutions below

3
On

Hm, this ain't exactly pretty, but as a quick adhoc, instead of combining the actual scriptblock objects, you can load the scriptblocks as strings, concatenate them with a newline, and create a scriptblock out of that new string.

$sb1="gps"
$sb2="gsv"
$sb3= [scriptblock]::Create($sb1 + "`n" + $sb2)
invoke-command $sb3