Thursday, February 24, 2011

Some PowerCLI examples

Note:  All of these examples assume that the script contains the line to
           connect to a esx/esxi host. The line should look like the following:
           Connect-VIServer ip_of_host//hostname -user root -password xxxxx

---

Example # 1:  Power on all of the virtual machines

1. Use notepad to create a script on the windows machine with powercli
    installed. Call the script poweronallvms.ps1

2. Add the following line so that the script contains the following:
    Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"} | Start-Vm

3. Execute the script by typing ./poweronnallvms.ps1 and verify the results
    with the vsphere client or the cli/vcli.

 ---

Example # 2: Create a virtual machine from scratch

1. Create a script called createvm.ps1 with your favorite editor.

2. Add the following lines to the script:
    New-VM -Name Lubuntu -NumCPU 2 -memoryMB 1024 -Datastore \  
    Storage1 DiskStorageFormat Thin -GuestID ubuntuGuest
    New-CDDrive -VM Lubuntu -ISOPath [Storage1]/lubuntu10.10.iso \
    -StartConnected
    Start-VM Lubuntu

3. Execute the script by typing ./createvm.ps1 and verify the results with the
    vsphere client. In this case, the iso image had been copied to the  
    datastore ahead of time.

---

Example # 3: Get generic information about host, vms, storage and network

1. Create a script called getinformation.ps1

2. Add the following lines to the script:
    Get-VMHost
    Get-VM
    Get-Datastore
    Get-DataCenter
    Get-VirtualSwitch
    Get-VirtualPortGroup

3. Execute the script by typing ./getinformation.ps1 and observe the output.

 ---

No comments:

Post a Comment