CSV format
------------------------------------------------------------------------------------------
Script
------------------------------------------------------------------------------------------
#[cluster,vSwitch,VLANname,VLANid]
# e.g: cluster name,vSwitch0,VM_network,192.168.0.0,11
Add-PSSnapin vmWARE.VimAutomation.Core
$vc="vcntername"
connect-viserver $vc
# Set the input file
$InputFile = "C:\PS\Import_PortGroup\All_PortGroup_SIT_20161202.csv"
# Read the import file
$MyVLANFile = Import-CSV $InputFile
# Parse the input file and add the virtual port groups accordingly
ForEach ($VLAN in $MyVLANFile) {
$MyCluster = $VLAN.cluster
$MyvSwitch = $VLAN.vSwitch
$MyVLANname = $VLAN.VLANname
$MyVLANid = $VLAN.VLANid
# Query the cluster to retrieve the hosts
$MyVMHosts = Get-Cluster $MyCluster | Get-VMHost | sort Name | % {$_.Name}
# Loop through the hosts and add the virtual port group to our vswitch based on the input
ForEach ($VMHost in $MyVMHosts) {
Get-VirtualSwitch -VMHost $VMHost -Name $MyvSwitch | New-VirtualPortGroup -Name $MyVLANname -VLanId $MyVLANid
}
}
附件:http://down.51cto.com/data/2366941
本文转自学海无涯博客51CTO博客,原文链接http://blog.51cto.com/549687/1958942如需转载请自行联系原作者
520feng2007