Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell join scriptblocks

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
like image 756
McVitas Avatar asked May 30 '26 09:05

McVitas


1 Answers

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
like image 142
PowerShellGuy Avatar answered May 31 '26 22:05

PowerShellGuy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!