I am hoping someone else has already developed a workaround for this particular issue. This is the last hurdle in an otherwise useful script to assist in our CoB (or DR) plan. Replicated VMs are spun up at the DR site in a specific folder in vCenter and once online I'd intended to use this script to modify NIC settings and reconnect them to the VM Network, leaving staff available to handle the SQL box that everyone depends on and other manual tasks. The same goes for failback, and of course proper error handling is a must as well as checks to ensure that VMs native to the DR site do not have their settings modified. Several days ago I'd found a script online that tackled a portion of what I intended and as such it was a great starting point. Now I have a script that meets all of my needs, with exception of replacement of the default gateway. Actually without this piece, the rest is basically a waste of time
I've tried both of these methods and while I can utilize remove-ipaddress to remove only the current IP address and use set-netipaddress to add a new IP and gateway, I still end up with the orphan gateway. viewing the output of the remove-netipaddress and remove-netroute and you see why they fail: Powershell is in non-interactive mode and as such prompting is not supported, nor apparently can you override prompts.
$scripttmp = 'remove-netipaddress -InterfaceAlias "' + $intAlias + '" -IPAddress ' + $currentIP + ' -PrefixLength 24 -DefaultGateway ' + $curGateway + ' -Confirm:$false -ErrorAction SilentlyContinue'
$remnetout = invoke-vmscript -ScriptText $scripttmp -ScriptType PowerShell -VM $vm -GuestUser $GuestUserName -GuestPassword $GuestPassword
OR
$scripttmp='remove-netroute -interfaceindex ' + $intIndex + ' -NextHop ' + $curGateway + ' -DestinationPrefix ' + $DestPrefix'
$rnetout=invoke=vmscript -ScriptText $scripttmp -ScriptType Powershell -VM $vm -GuestUser $GuestUserName -GuestPassword $GuestPassword
Any assistance is of course much appreciated.