Advertisement
pedronrivera

Port Group Script

May 13th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Read MyVLANs.csv for input and create virtual machine portgroups # Input file has a header row which contains cluster,vSwitch,VLANname,VLANid
  2. # The virtual machine port groups will be created on all hosts in the cluster
  3. # If the virtual port group exists it will be skipped but will write errors to the console
  4. # Set the input file
  5. $InputFile = "C:\Users\ewright\Documents\SCRIPTS-TEMP\MyVLANs.csv"
  6.  
  7. # Read the import file
  8. $MyVLANFile = Import-CSV $InputFile
  9.  
  10. # Parse the input file and add the virtual port groups accordingly
  11. ForEach ($VLAN in $MyVLANFile) { $MyCluster = $VLAN.cluster $MyvSwitch = $VLAN.vSwitch $MyVLANname = $VLAN.VLANname $MyVLANid = $VLAN.VLANid
  12.  
  13. # Query the cluster to retrieve the hosts
  14. $MyVMHosts = Get-Cluster $MyCluster | Get-VMHost | sort Name | % {$_.Name}
  15.  
  16. # Loop through the hosts and add the virtual port group to our vswitch based on the input
  17. ForEach ($VMHost in $MyVMHosts) { Get-VirtualSwitch -VMHost $VMHost -Name $MyvSwitch | New-VirtualPortGroup -Name $MyVLANname -VLanId $MyVLANid } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement