VMware ESX Post installation configuration via PowerCLI


    I made a simple example script using the VMware PowerCLI that can be used to do the post installation configuration of a VMware ESX 4 server. This script does the following things:

    • – Connect to VMware ESX server

    • – Set the SC memory to 800MB

    • – Creates vSwitch2 and add vmnic2 and vmnic3

    • – Add several PortGroups and VLANs to vSwitch2

    • – Remove default PortGroup VM Network

    • – Creates a VMkernel VMotion port with IP address, subnetmask and VLAN

    • – Add vmnic0 and vmnic1 to vSwitch0, set for the VMotion port vmnic1 active and vmnic0 standby and for the Service Console port vmnic0 active and vmnic1 standby

    • – Configure NTP servers and open the firewall

    • – Sets advanced settings Disk.UseDeviceReset to 0 and Disk.UseLUNReset  to 1

    • – Sets he Qlogic HBA queue depth and the Disk.SchedNumReqOutstandig to 64

    • – Enable VMhost Startup and stop

    Here’s the listing of the script:

    # Title: VMware ESX4 Post config #
    Filename:    esx4postconfig.sp1 # 
    Created by: Ivo Beerens #                                                      
    Date: February 2010 #                                                            
    Version: 1.0 #
    Website: www.ivobeerens.nl #                                                        
    E-mail: ivo[AT]ivobeerens.nl #                                                        

    # Variables #
    vCenter name and port #
    $vcserver = "vc01"
    $portvc = "443"
    #Service Console memory #
    $SCMemory = 800
    # default PortGroup #
    $DefaultPG = Get-VirtualPortgroup -Name VM Network
    # VMotion IP, subnetmask and VLAN
    $VMotionIP =“
    $VMotionSubnet = ““
    $VMotionVLan =
    # HBA Queue depth #
    $HBAqueudepth = 64
    # vSwitch names #
    $Switch1 =
    $Switch2 =

    Connect-VIServer $vcserver -User root -Password idontknow -port $portvc

    # Set SC memory to 800MB #
    Get-VMHost | Get-View | %{(Get-View -Id $_.ConfigManager.MemoryManager).ReconfigureServiceConsoleReservation($SCMemory*1mb)}

    # Create vSwitch1 and add vmnic2 and vmnic3 #
    New-VirtualSwitch -Name $Switch2 -Nic vmnic2,vmnic3

    # Add PortGroups and VLANs  to the vSwitch2 #
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name"VLAN999 Management" -VLANID 999
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN100 Servers" -VLANID 100
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN99 Clients Ser-D" -VLANID 99
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN98 Clients Ser-B" -VLANID 98
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN33 Secure LAN" -VLANID 33
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN32 DMZ3" -VLANID 32
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN31 DMZ1" -VLANID 31
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN30 DMZ0" -VLANID 30
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN7 VOiP" -VLANID 7
    get-vmhost | Get-VirtualSwitch -Name $Switch2 | New-VirtualPortGroup -Name
    "VLAN1 Default"

    # Remove default Port Group after default installation #
    Remove-VirtualPortGroup -VirtualPortGroup $DefaultPG -Confirm:$false

    # Creates a VMkernel port VMotion on vSwitch0 #
    New-VMHostNetworkAdapter -PortGroup “VMotion“ -VirtualSwitch $Switch1 -IP $VMotionIP -SubnetMask $VMotionSubnet -VMotionEnabled:$true

    # Set VLAN of the VMotion VMkernel #
    $VMotionPG = Get-VirtualPortgroup -Name ‘VMotion’
    Set-VirtualPortGroup -VirtualPortGroup $VMotionPG -VlanId $VMotionVLan

    # Add vmnic0 and vmnic1 to vSwitch0 #
    $vs = get-virtualswitch -name $Switch0
    Set-VirtualSwitch -VirtualSwitch $vs -Nic vmnic0,vmnic1

    # Set VMotion vmnic1 active vmnic0 standby #
    get-virtualportgroup -name VMotion | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic1
    get-virtualportgroup -name VMotion | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicStandby vmnic0

    # Set Service Console vmnic0 active, vmnic1 standby #
    get-virtualportgroup -name ‘Service Console’ | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic0
    get-virtualportgroup -name ‘Service Console’ | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicStandby vmnic1

    # Configures NTP, add NTP servers, starts NTP and open the firewall port #
    Add-VmHostNtpServer -NtpServer "0.vmware.pool.ntp.org"
    Add-VmHostNtpServer -NtpServer
    Add-VmHostNtpServer -NtpServer
    "NTP Client" | Set-VMHostFirewallException -enabled:$true

    # Advanced settings instellen #
    Set-VMHostAdvancedConfiguration -Name"Disk.UseDeviceReset" -Value 0
    Set-VMHostAdvancedConfiguration -Name
    "Disk.UseLunReset" -Value 1

    # Set the queuedepth for the Qlogic HBA and SchedNumReqOutstanding setting #
    Get-VMhostModule "qla2xxx" | Set-VMHostModule -Options"ql2xmaxqdepth=$HBAqueudepth"
    Set-VMHostAdvancedConfiguration -Name
    "Disk.SchedNumReqOutstanding" -Value $HBAqueudepth

    # Enable VMHostStartup #
    $VMstart = Get-VMHostStartPolicy
    Set-VMHostStartPolicy -VMHostStartPolicy $VMstart -Enabled:$true -StartDelay 60 -StopDelay 60 -StopAction GuestShutDown

    # Disconnect #
    disconnect-viserver -confirm:$false

    It’s easy to adjust the settings for your need. This shows the strength to use the PowerCLI for doing automation in VMware. If you have suggestions please let me know.


    8 thoughts on “VMware ESX Post installation configuration via PowerCLI”

    1. This is good stuff. Thank you – it’s helping me a lot to migrate my build scripts from SSH/Shell to PowerCli.

      How do you set the DNS servers through PowerCLI? In the past I have been doing this through a shell build script:

      # Overwrite the resolv.conf file to create primary and secondary DNS entries
      cat > /etc/resolv.conf << DNS

    2. @Chris

      Here is an example howto set using PowerCLI

      $vmHostNetworkInfo = Get-VmHostNetwork
      Set-VmHostNetwork -Network $vmHostNetworkInfo -DnsAddress,
      Set-VmHostNetwork -Network $vmHostNetworkInfo -DomainName ivobeerens.local -SearchDomain ivobeerens.local

    3. Chris,

      nice script. can you confirm this also works with ESXi5 ? have you tested it before ?

      thanks a lot.

    4. is it possible to set the Load balancing and Network failover detection for a particluar port group ?

    5. I have a Windows workstation with vSphere client on it. I have a ps1 script (on my local Win box) to set some networking paramaters on an ESX server. I have 3 versions of the script, and 3 ESX servers. How do I run script 1 on ESX server 1, script 2 on ESX server 2 and script 3 on ESX server 3?

      Thanks much,

    6. @MJT use the Connect-VIServer $vcserver -User root -Password idontknow -port $portvc and variables

    7. how to run a shell script from post cli after installing the linux vm in ESXi5.


    Leave a Comment