When production and disaster recovery vCenters contain VMs with the same names, how do you make sure a teardown script only touches the intended environment?
How do you keep PowerCLI from targeting the wrong vCenter?
Answers
Check the default server mode and the current connections before doing anything destructive. In multiple default server mode, a Get-VM call without -Server can query all connected default vCenters. You can inspect $global:DefaultVIServers to see what is in that set.
Connect to the intended vCenter, keep the returned connection object, and pass it explicitly with -Server when retrieving VMs. Don't rely on the VM name alone to distinguish production from a restored copy.
Reply to this answer
Before stopping or deleting anything, print the selected VMs and their associated vCenter, then check the count against the restore list. A matching count alone isn't enough if the wrong VMs were selected.
For permanent deletion, have someone review the target list and the scope before the script proceeds. Suppressing confirmation prompts makes that review even more useful.
_y84!torquelo.5:
That makes sense. The preview needs to show the environment as well as the names.
Reply to this answer
Separate the permissions too. A teardown account that can delete DR copies but cannot delete production VMs gives you a control outside the script. Check that its permissions are scoped to the actual DR resources, not just that it connects to the DR vCenter.
Reply to this answer
Don't let the teardown procedure be the only safeguard. Make sure production backups can be restored and that the recovery process is tested. If something does go wrong, report it quickly so recovery can start rather than waiting to sort out blame.
Reply to this answer
Add your answer
Answers are reviewed before publication.
8e~cobblepouch_416:
A separate PowerShell session for the teardown also helps keep unrelated connections out of the way. Explicit targeting is still worth doing.