Advertisement
PureGremlin

ESX- Copy VSS different switchname

Dec 22nd, 2021 (edited)
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Copies one host's standard vSwitch to another host
  2. #Author: Jason Coleman (virtuallyjason.blogspot.com)
  3. #Usage: Clone-VSS.ps1 -s [the name of the ESX host to copy the vSwitch from] -v [the name of the vSwitch to copy] -d [the name of the ESX host to copy the vSwitch to]
  4. param
  5. (
  6.    [alias("s")]
  7.    [string]$sourceHostString = $(read-host -Prompt "Enter the source Host"),
  8.    [alias("v")]
  9.    [string]$sourceVSwitchString = $(read-host -Prompt "Enter the Source Standard Virtual Switch"),
  10.    [alias("d")]
  11.    [string]$destinationHostString = $(read-host -Prompt "Enter the Destination Host"),
  12.    [alias("n")]
  13.    [string]$destinationVSwitchString = $(read-host -Prompt "Enter the Destination Standard Virtual Switch")
  14. )
  15.  
  16. #Get the destination host
  17. $thisHost = get-vmhost $destinationHostString
  18.  
  19. #Get the source vSwitch and do error checking
  20. $sVSwitch = get-vmhost $sourceHostString | get-virtualswitch -name $sourceVSwitchString -errorAction silentlycontinue
  21. if (!($sVSwitch))
  22. {
  23.    write-host "$sourceVSwitchString was not found on $sourceHostString" -foreground "red"
  24.    exit 1
  25. }
  26. if ($sVSwitch.count -ne 1)
  27. {
  28.    write-host "'$sourceVswitchString' returned multiple vSwitches; please use a more specific string." -foreground "red"
  29.    $sVSwitch
  30.    exit 4
  31. }
  32. if ($thisHost | get-virtualSwitch -name $destinationVSwitchString -errorAction silentlycontinue)
  33. {  
  34.    if ((($thisHost | get-virtualSwitch -name $destinationVSwitchString).uid) -like "*DistributedSwitch*")
  35.    {
  36.       write-host "$destinationVSwitchString is a Distributed vSwitch, exiting." -foreground "red"
  37.       exit 3
  38.    }
  39.    $continue = read-host "vSwitch $destinationVSwitchString already exists on $destinationHostString; continue? [yes|no]"
  40.    if (!($continue -like "y*"))
  41.    {
  42.       exit 2
  43.    }
  44. }
  45. else
  46. {
  47.    #If the VSS doesn't already exist, create it
  48. ##   $thisHost | new-virtualSwitch -name $sVSwitch.name > $null
  49.    $thisHost | new-virtualSwitch -name $destinationVSwitchString > $null
  50. }
  51.  
  52. #Make new Port Groups on the VSS
  53. $destSwitch = $thisHost | get-virtualSwitch -name $destinationVSwitchString
  54. foreach ($thisPG in ($sVSwitch | get-virtualportgroup))
  55. {
  56.    #Skip this Port Group if it already exists on the destination vSwitch
  57.    if ($destSwitch | get-virtualportgroup -name "$($thisPG.Name)" -errorAction silentlycontinue)
  58.    {
  59.       echo "$($thisPG.Name) already exists, skipping."
  60.    }
  61.    else
  62.    {
  63.       echo "Creating Port Group: $($thisPG.Name)."
  64.       new-virtualportgroup -virtualswitch $destSwitch -name "$($thisPG.Name)" > $null
  65.       #Assign a VLAN tag if there is one on the source Port Group
  66.       if ($thisPG.vlanid -ne 0)
  67.       {
  68.          get-virtualportgroup -virtualswitch $destSwitch -name "$($thisPG.Name)" | Set-VirtualPortGroup -vlanid $thisPG.vlanid > $null
  69.       }      
  70.    }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement