"The $credentialIId variable above has a value of an array of numbers"
That would be a problem. There should be just one number. Try printing it out from the script and see what it looks like. If there's more than one credential with the same name, you would need to add "TOP 1" to the "SELECT ID FROM Orion.Credential ..." query or delete the redundant creds.