Something to add here...
looking at the value for $dvsw and $esx after the script fails shows them empty.
Also, a get-dvswitch -dcname DC -dvswname dvsw1, returns nothing... while a get-virtualswitch -distributed does.
This is a Cisco Nexus 1000v if that matters.