VMware PowerCLI script to add storage and VLAN’s

When deploying multiple VMware hosts you can use the Template feature in vSphere. If you’re not licenced to use the template feature in vSphere you can use the VMware PowerCLI PowerShell commands to quickly add the strorage and the networks.

First thing you got to do is connect to the host:

  • Connect-VIServer -Server esx.jvr.local

To add VLAN’s to a vSwitch use the following command:

  • get-vmhost -name esx.jvr.local | Get-VirtualSwitch -name vSwitch0 | new-VirtualPortGroup -name “LAN” -vlanid 0
  • get-vmhost -name esx.jvr.local | Get-VirtualSwitch -name vSwitch0 | new-VirtualPortGroup -name “PVS” -vlanid 2310

To add NFS storage use the following commands:

  • get-vmhost -name esx.jvr.local | New-Datastore -Nfs -Name ESX1 -Path /NAS_ESX1 -NfsHost 10.10.10.50

This can also be done at the cluster level. The following command’s can be used:

  • Get-Cluster “CLUSTER_1” | Get-VMHost | Get-VirtualSwitch -name vSwitch1 | New-VirtualPortGroup -name “PVS” -VLanId 2310
  • Get-Cluster “CLUSTER_1” | Get-VMHost | Get-VirtualSwitch -name vSwitch1 | New-VirtualPortGroup -name “HB” -VLanId 2309