This script is useful when you are introducing a a new host to your environment and need to create multiple VLANs on this host.
Both of these script would do the same thing but the only difference is, to make use of first script you first need to create a csv file having VirtualSwitchName, VLANname, VLANid details on the other hand second script would take one host of your choice as reference and would create the same VLANs on target host.
Script 1: -
Script 2 :-
Hope these scripts would be useful.
That's it... :)
Both of these script would do the same thing but the only difference is, to make use of first script you first need to create a csv file having VirtualSwitchName, VLANname, VLANid details on the other hand second script would take one host of your choice as reference and would create the same VLANs on target host.
Script 1: -
<# ==================================================================
Title: create_multiple_vlan-2.ps1
Description:this script can be used to create VLANs on a newly added VMhost, it will read VLAN-detail.csv and create VM port groups
Requirements: Windows Powershell with PowerCLI installed
Pre-req: You need to create a vlan-detail.csv with required VirtualSwitchName, VLANname, VLANid, and the the vSwitch should be present on target host
Usage: .\create_multiple_vlan.ps1
==================================================================== #>
Add-PSSnapin VMware.VimAutomation.Core
$myvc = read-host -Prompt "Enter your vCenter server Name or IP name"
Connect-VIServer $myvc
$targetVMhost = read-host -Prompt "Enter your target host name"
$InputFile = “c:\vlan-detail.csv”
$MyVLANFile = Import-CSV $InputFile
ForEach ($VLAN in $MyVLANFile) {
$MyvSwitch = $VLAN.VirtualSwitchName
$MyVLANname = $VLAN.VLANname
$MyVLANid = $VLAN.VLANid
get-vmhost $targetVMhost | Get-VirtualSwitch -Name $MyvSwitch | New-VirtualPortGroup -Name $MyVLANname -VLanId $MyVLANid
}
disconnect-VIServer $myvc -Confirm:$false
Script 2 :-
<# ==================================================================
Title: create_multiple_vlan.ps1
Description:this script can be used to create VLANs on a newly added VMhost by taking referance of any existing host
Requirements: Windows Powershell with PowerCLI installed
pre-req: vSwitch should be present on target gost
Usage: .\create_multiple_vlan.ps1
==================================================================== #>
Add-PSSnapin VMware.VimAutomation.Core
$myvc = read-host -Prompt "Enter your vCenter server Name or IP name"
Connect-VIServer $myvc
$sourceVMhost = read-host -Prompt "Enter your source host name"
$targetVMhost = read-host -Prompt "Enter your target host name"
get-vmhost $sourceVMhost | Get-VirtualSwitch | Get-VirtualPortGroup | select VirtualSwitchName, Name, vlanID | export-csv "c:\vlan-detail.csv" -NoTypeInformation
$InputFile = “c:\vlan-detail.csv”
$MyVLANFile = Import-CSV $InputFile
ForEach ($VLAN in $MyVLANFile) {
$MyvSwitch = $VLAN.VirtualSwitchName
$MyVLANname = $VLAN.name
$MyVLANid = $VLAN.VLANid
get-vmhost $targetVMhost | Get-VirtualSwitch -Name $MyvSwitch | New-VirtualPortGroup -Name $MyVLANname -VLanId $MyVLANid
}
Remove-Item $InputFile
disconnect-VIServer $myvc -Confirm:$false
Hope these scripts would be useful.
That's it... :)