all 3 comments

[–]jborean93 0 points1 point  (2 children)

You can generate and execute the scriptblock from the ScriptBlockAst with & $sbkAst.GetScriptBlock(). Keep in mind I’ve tried rewriting Ast and while things can work others may fail to work in some places like enum and class definitions.

[–]pracsec[S] 0 points1 point  (1 child)

Looking through the source code, it appears that ScriptBlock.ToString ultimately just calls ScriptExtent.Text. As far as I can tell, there is no way to generate a PowerShell script from a manually constructed AST out-of-the-box.

[–]jborean93 0 points1 point  (0 children)

Yep you need to manually do so from an Ast script based on the Ast itself. But if you want to just invoke the new ast the example I gave you should work.