I have tested all these solutions and they work. I can now apply the script (now significantly improved) from within sam and also from the command line with ssam. I will keep these posts as examples. The only question that remains is how to add comments to this script. If I use # it is ignored.