You should value check $errReturnCode right after SMSInstallCheck see what value that gives you. It might get wrong from there.

Also do some @error checks in the script see where you get a value higher then 0. That way you can pinpoint where it goes wrong.