It is hard to figure out what's wrong. You may not want to hear this, but I do know a that running a script with elevated TI privileges is a good way to test your host operating system's stability. If it fails then you've maybe disabled too much or your OS is broken. Figuring out what needs to be turned back on or updated is truly challenging. Sometimes it's easier to test the script in a fresh virtual machine.