Looks like you’re deleting a CNAME so you may need to specify the record type. Looking back at scripts I had written like this I was dealing with A records so I don’t know for sure but that’s what appears to be wrong.
use DNSCMD /? to check the syntax for deleting a CNAME (alias) record.
The record type is specified in the command, i am deleting an A record and the creating it with a different IP.
From the command above, deleting the A record works fine but it is the creation that fails. If i replace the variable $switch_ip by the IP it works fine:
Example
Invoke-Command -Computername nlamsdc03 -ScriptBlock {cmd.exe /c dnscmd nlamsdc03 /RecordAdd acme.com webmail-test 60 A 10.1.1.101}
The problem here is that the $switch_ip variable doesn’t exist in the scope of the script block that you’re executing on the remote machine. Try passing it as an argument, like this: