Posts Tagged ‘Powershell’

postheadericon 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 = 172.16.1.50“
    $VMotionSubnet = “255.255.0.0“
    $VMotionVLan =
    "3"
    # HBA Queue depth #
    $HBAqueudepth = 64
    # vSwitch names #
    $Switch1 =
    "vSwitch0"
    $Switch2 =
    "vSwitch2"

    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
    "1.vmware.pool.ntp.org"
    Add-VmHostNtpServer -NtpServer
    "2.vmware.pool.ntp.org"
    Get-vmhostfirewallexception
    "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.

    postheadericon Powershell Windows Eventlog script

    This week i found the report-events Powershell script (see figure 1). The script is made by Jeffrey Hicks. It generates a nice HTML file with errors and warnings from the Windows eventlog.

    image

    figure 1. Output reports-event script

    You can specify multiple servers in a text file, select how many hours from the current time to look in the eventlog and e-mail the HTML file after generation. 

    The following syntax can be used:

    get-content c:\script\servers.txt | c:\scripts\report-event.ps1 – report c:\script\eventlog.html –hours 48 –smtp mail.beerenss.nl –sendto eventlogs@beerens.nl –from eventlog@beerens.nl

    Explanation:

    get-content c:\script\servers.txt, text file containing the server names

    – report c:\script\eventlog.html, name of the HTML file

    | c:\scripts\report-event.ps1, call the actually script

    –hours 48, hours to report from the curren time

    –smtp mail.beerens.nl. SMTP server to sent the HTML rapport 

    –sendto ivo@beerens.nl, To address

    –from eventlog@beerens.nl . from address

     

    Schedule this script to run frequently to watch your eventlogs!

     

    Download the script here.

     

    postheadericon Winners VI toolkit scripting contest

    The winners of the VI toolkit scripting contest are announced:

    - The first prize goes to LucD for his Guest Provisioning System script.

    - The Second prize goes to tzamora for his VMware Infrastructure Power Documenter script.

    - The Third prize goes to Dan Baskette with his PowerVDI script.

    The Healthcheck script that i created has not won :-(

    You can read the rest @ the VMware Powershell Blog

    For all VI toolkit contributions check this link

    postheadericon VI Toolkit Update 1 released

     

    VMware has released VI Toolkit Update 1. There where performance problems with the get-VM command.

    Some users had reported slowness when using the Get-VM cmdlet. The reason turned out to be an odd quirk in querying some types of network adapters, so many people weren’t affected at all. On the other hand, if you were affected, it was pretty annoying. To address this we’ve released Update 1 of our toolkit. If you’ve had trouble with Get-VM, download it things will go a lot smoother. One user reported that his queries went from more than 5 minutes to just over 10 seconds — a world of difference!

    This is the only change we made to the toolkit, so don’t expect lots of new functionality when you download, you’ll have to wait a bit longer for that. If you haven’t had any trouble with 1.0 it’s perfectly fine to keep using it until the next release.

    Also, many of you are probably eagerly anticipating the results of our scripting contest. I want to thank everyone for participating and point out that the entries are now open to the public. Check it out, there’s a lot of really great stuff in there.

    postheadericon VMware Powershell Healthcheck script

    Healthcheck is a Powershell script that reports information like snapshots, VMware tools version, datastore space, CDROM and/or floppy drives connected etc. to HTML and e-mail the output to a person or distribution list.

    Reason for creating this script:

    As VMware Consultant I see a lot of common problems in VMware environments like:

    - Snapshots are enabled and forgot the commit to the VM.

    - Datastores are almost full (for example if snapshots are enabled)

    - VMware tools versions are different

    - CDROM and floppy drives are still mounted to the VM

    - Virtual Machines have CPU and Memory limits or reservations (VMs are swapping)

    - In the VM, the VMware Tools timesync option is not enabled

    In the Virtual Infrastructure Client (VIC) it is difficult to see this sort of information. By creating a Powershell script,  I can do a quick inventory.  In a lot of VMware environments I created a scheduled tasks, so the script runs once a week and sent to HTML rapport to the administrator.

    What does the script:

    I wrote a Powershell script with HELP from the VMTN community that makes a HTML file and sent the output by e-mail to a person or distribution list.  The Healthcheck script does the following checks:

    - VMware ESX hardware

    - VMware ESX versions

    - VMware VirtualCenter versions

    - Active snapshots

    - CDROM and Floppy drive(s) mounted to the VM

    - Datastore information like capacity, free space and the percentage free space

    - VirtualMachine (VM) information like VMware tools version, CPU, Memory reservations and limits etc.

    - On what VMs VMware Tools timesync is not enabled

    Requirements:

    The following software must be installed:

    Microsoft Powershell 1.0 (http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx)

    VMware Infrastucture (VI) toolkit for Windows 1.0

    http://www.vmware.com/download/sdk/

    Set the ExecutionPolicy in Windows Powershell to RemoteSigned by using the following command:

    set-ExecutionPolicy RemoteSigned

    Installation:

    - Unzip the Healthcheck.zip script to a directory on the VC server for example.

    - When the ZIP if unpacked there are two files:

    - Healthcheck.ps1, this is the Powershell script

    - Style.CSS, controls the HTML layout

    Configuration:

    - Edit the Powershell.ps1 file

    edit the following variables:

    $vcserver=”localhost”

    Enter the VC server, if you execute the script on the VC server you can use the localhost name

    $filelocation=”D:\temp\Healthcheck.htm”
    Specify the location where to store the HTML output

    $enablemail=”yes”
    Enable (yes) or disable (no) to sent the script by e-mail

    $smtpServer = “mail.ivobeerens.nl”
    Specify the SMTP server in your network

    $mailfrom = “VMware Healtcheck <powershell@ivobeerens.nl>”
    Specify the from field

    $mailto = ivo@ivobeerens.nl

    Specify the address where the e-mail to sent  to

    Usage:

    Manually run the Healthcheck.ps1 script”:

    1. Open Powershell

    2. Browse to the directory where the Healthcheck.ps1 script resides

    3. enter the command:

    ./Healthcheck.ps1

    To create a schedule task in for example Windows 2003 use the following syntax in the run property:
    Powershell -command “& ‘path\Healthcheck.ps1′
    edit the path

    Powershell -command “& ‘path\Healthcheck.ps1′

    edit the path .

    Future:

    - List Orphaned VMDK’s

    - Add performance information like VM usage

    - Check timesync on the VMware hosts

    Happy testing :-)

    Download Link: Healthcheck script

    The script is posted on the VMware Powershell contest forum, link

    Some screenshots of the HTML output:

     

    1JPG

    2

    3