Export portgroup from the host
Add-PSSnapin vmWARE.VimAutomation.Core
$vc="Vcname"
connect-viserver $vc
$date = Get-Date -Format ‘yyyyMMdd‘
$VLANinfo = foreach ($cluster in get-cluster)
{
foreach ($esx in (Get-VMHost -Location $cluster))
{
foreach ($pg in (Get-VirtualPortGroup -VMHost $esx))
{
Select-Object -InputObject $pg -Property @{N="Cluster"; E={$cluster.Name}},
@{N="VMhost"; E={$esx.Name}},
@{N="VirtualSwitchName"; E={$pg.VirtualSwitchName}},
@{N="portgroup"; E={$pg.Name}},
@{N="VLAN"; E={$pg.VlanID}}
}
}
}
$VLANinfo | Export-Csv "C:\PS\report\$($vc)_allPortGroup_$date.csv"
Import portgroup to the host
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
}
}
Copy vSwith portgroup from an existing ESXi host
Script
##http://www.virtu-al.net/2009/06/27/powercli-easy-vswitch-portgroup-setup/
##copying all vSwitches and PortGroups from an existing ESX server over to a new server
Add-PSSnapin vmWARE.VimAutomation.Core
$VISRV = Connect-VIServer (Read-Host "Please enter the name of your VI SERVER")
$BASEHost = Get-VMHost -Name (Read-Host "Please enter the name of your existing server as seen in the VI Client:")
$NEWHost = Get-VMHost -Name (Read-Host "Please enter the name of the server to configure as seen in the VI Client:")
$BASEHost |Get-VirtualSwitch |Foreach {
$vSwitch = $_
If (($NEWHost |Get-VirtualSwitch -Name $_.Name-ErrorAction SilentlyContinue)-eq $null){
Write-Host "Creating Virtual Switch $($_.Name)"
$NewSwitch = $NEWHost |New-VirtualSwitch -Name $_.Name-NumPorts $_.NumPorts-Mtu $_.Mtu
}
$_ |Get-VirtualPortGroup |Foreach {
If (($NEWHost |Get-VirtualPortGroup -Name $_.Name-ErrorAction SilentlyContinue)-eq $null){
Write-Host "Creating Portgroup $($_.Name)"
$NewPortGroup = $NEWHost |Get-VirtualSwitch -Name $vSwitch |New-VirtualPortGroup -Name $_.Name-VLanId $_.VLanID
}
}
}
Remove VirtualPortGroup Specify vSwitch
#Remove all port groups on speific vSwitch#
Add-PSSnapin vmWARE.VimAutomation.Core
Connect-VIServer VCName
$hostserver = "10.194.194.80"
$virtualsw = Get-virtualswitch -vmhost $hostserver -Name vSwitch0
#$vswgoup = Get-VirtualPortGroup -VMHost 10.194.96.1 -VirtualSwitch $virtualsw | where { $_.Name -ne "Management Network" }
$vswgoup = Get-VirtualPortGroup -VirtualSwitch $virtualsw | where { $_.Name -ne "Management Network" }
$vswgoup | Remove-VirtualPortGroup -confirm:$false
Disconnect-VIServer VCName
原文地址:http://blog.51cto.com/549687/2114717
时间: 2024-11-11 13:45:48