I am not sure if it is okay to share the corrected code here? It is Microsoft licensed code.
But here are the errors I found:
- The variable $credentials is called $credential incorrectly in a few places
- The RemoveConnection function is never called by the Import-WACConnection because when they are trying to call that function they are using Remove-Connection. Just need to remove the hyphen here.
- When they call the RemoveConnection function they are also using a variable that has never had its value set: $GatewayEndpoint (as well as $credential mentioned earlier). This should be $Endpoint. They also haven't named the parameters on this line and the variables are in the wrong order. Easiest to name the variables e.g. -Endpoint -ConnectionId -Credential.
- Line 46 is where they use a code block to check for the $accessToken. Use this format for the RemoveConnection and ApplyConnection functions as well. Remove line 120.
- You will also need to return the response from the RemoveConnection and ApplyConnection functions with "return $response" at the end of these functions
- Set the $AccessKey parameter Mandatory = $false if you use Kerberos in the Import-WACConnection function
I think that covers most of it.