Beruflich Dokumente
Kultur Dokumente
1 –
Automating Your vSphere Environment
This document contains frequently asked questions about managing VMware vSphere with
PowerCLI. This document is a wiki page, so if you have contributions or corrections, feel free
to make them by editing the document directly.
Note: If you are using Internet Explorer, the code samples below do not render properly.
To deal with this problem you can click "Edit Document" on the left hand side of the page,
switch to plain text view, and copy text out of the document.
A: vSphere PowerCLI is built on the vSphere API. As a general rule you can do most
things with PowerCLI than you can with the vSphere Client. This means that you can script
common tasks to help you save time.
A: vSphere PowerCLI has a centralized website in our communities where users can get the
latest downloads, documentation, ask questions in our forums.
A: Windows 7, Windows 2008 Server, Windows Vista, Windows XP, Windows Server 2003
Q. I am new to PowerCLI what videos do you recommend we view to learn the basics ?
A. Yavor Boychev from PowerCLI engineering put together some very helpful videos.
A: Yes.
A: VMware offers a two day training course for PowerCLI. Customers can use
their PSO credits and apply it to course. Course is available worldwide please
consult with your local VMware and Partner Training Center – http://vmware.com/go/
vsphereautomation
A: Use the Connect-VIServer cmdlet and provide a value to the Server parameter.
Connect-VIServer -Server
A: Use the cmdlet Get-PowerCLIVersion which prints information about the product version:
Get-PowerCLIVersion
PowerCLI Version
----------------
VMware vSphere PowerCLI 4.1 build 264274
Q: How do I stop PowerShell from prompting me for confirmation before taking action?
Use
-Confirm:$false
Q: Can I manage more than one vCenter Server at a time? Can I connect to both ESX and
vCenter Server at the same time.
A: Yes, each PowerCLI commandlet has help. The help is available directly in PowerCLI
console. There is also online version of the help.
VM Lifecycle:
A: You can create a new virtual machine using the New-VM cmdlet. Before running this
cmdlet, you need to connect to a server. The following examples shows different ways to
create new virtual machine:
# creating virtual machine with default values for GuestID, Disk size, Memory size, etc.
New-VM -Name <target vm name> -Host <host-name>
# filtering machines by name and start those that are not started
Get-VM <name-filter> | where { $_.PowerState –eq "PoweredOff" } | Start-VM
A: Yes, if the virtual machine has installed VMware Tools in it, it is possible to invoke
operations like stop, restart and suspend within the guest OS. The commandlets are Stop-
VMGuest, Restart-VMGuest, Shutdown-VMGuest
# Gracefully shut downing all machines which name starts with WinXP string.
Get-VM WinXP* | Shutdown-VMGuest –confirm:$false
Q: I need to upgrade memory of all virtual machines under given folder/resource pool, how
can I do it?
A: There is PowerCLI commandlet named Set-VM which allows to change a lot of virtual
machines characteristics like name, description, memory size, number of CPU, etc. The
following example shows how to filter virtual machines by resource pool and to update their
memory to 2 GB.
# Getting all virtual machines under given resource pool and upgrade their memory to 2048 MB
Get-ResourcePool <resource pool name> | Get-VM | Set-VM –MemoryMB 2048 –confirm:$false
New-VM –Name <Target-VM-Name> –VMFilePath "\[<Datastore Name>\] <Path To VMX>.vmx" –VMHost <host-name
Q: How can I set VM power-on options, such as configuring tools to automatically upgrade?
Q: How can I determine the version of VMware Tools my VMs are using?
A: This information is contained in the Toolversion property under the config property of the
VirtualMachine object. To get this information, use the following script.
Get-VM VMName | Get-VMGuestNetworkInterface -GuestUser Administrator -GuestPassword <password> -HostUser root -HostPas
unregister virtual machine from the inventory. The following examples show both
actions:
# Delete virtual machine from inventory and its contents from datastore
Get-VM <vm name> | Remove-VM –DeleteFromDisk –confirm:$false
Q: How can I determine the connection state of CD ROM drives and floppy drives?
Q: I have set up different virtual machine templates, how can I deploy virtual machines from
them?
A: The commandlet New-VM allows deploying virtual machine from existing template.
Get-Template WindowsXPTemplate | New-VM –Name <target-vm-name> -VMHost <name of vmhost> -Datastore <na
A: The commandlet New-VM allows to clone virtual machine from other existing virtual
machine.
New-VM –VM <name of existing VM> -Name <target-vm-name> -VMHost <name of vmhost> -Datastore <name of d
A: Yes, PowerCLI has set of commandlets to obtain, change and create customization
specifications. PowerCLI also allows applying customization specification along with the
deployment by using the New-VM commandlet.
Snapshots:
To do snapshot of a particular machine use the –VM parameter and specify the
virtual machine name.
A: A virtual machine can have multiple snapshots and you can revert the VM to any of them
at a time. The following script reverts a virtual machine to a specified snapshot.
A: The following script lists snapshots that are older than 30 days.
Datastores
A: Yes, PowerCLI gives this ability based on the Powershell provider concept. This mean
that VMware datastores can be browsed with the native Powershell commandlets like Set-
Location, Get-Item, Get-ChildItem and their aliases like cd, ls, dir. PowerCLI datastore
provider also allows to manipulate datastore contents with native Powershell commandlets
like Copy-Item, Move-Item, Remove-Item, etc.
After PowerCLI starts there is registered drive vmstores: which contains a sub folder
for each currently connected server. See the following example that shows some basic
operations with vmstores drive:
# this example will list all files and subfolders that starts with WinXP
# inside the datastore with name Storage1.
ls vmstores:\myServer@443\Datacenter\Storage1\WinXP*
# this example shows how to delete all log files inside sub-folder Machine1
rm vmstores:\myServer@443\Datacenter\Storage1\Machine1\*.log
A: Yes it is possible to copy files from/to datastore using the commandlet Copy-
DatastoreItem.
Get-VMHost YOURHOST | New-VirtualSwitch -name "My New Switch" -nic YOURNIC (e.g. vmnic1)
Get-VMHost YOURHOST | Get-VirtualSwitch -name "My New Switch" | New-VirtualPortGroup -vlanid 100
Q: How can I display and configure the physical adapters of my ESX host?
A: The following script displays a list of the physical adapters of an ESX host.
Reporting
Q: How can I create a tabular report of all my VMs, including their configurations and
number of virtual disks that they have?
A: The following script creates a tabular report of all VMs with their configurations.
# This output can be redirected to Export-Csv which will produce a CSV file
Get-VM | Select-Object Name, MemoryMB , @{ Name="MemReservation"; Expression={$_.ExtensionData.Summary.Config.memor
Q: I want report that shows the capacity and free space of all available datastores, is it
possible?
Q: I want report for provisioned and used space by virtual machines, is it possible?
Q: I want report about all VM Hosts, their parent cluster, CPU count, and available memory,
is it possible?
Guest OS interaction
A: Yes, if the virtual machine has installed VMware Tools it is possible to invoke batch file
or powershell script on Windows guest OS and bash scripts on Linux guest OS. This is
possible with Invoke-VMScript commandlet.
A: Yes, if the virtual machine has installed VMware Tools it is possible to copy from and to
guest OS files using the Copy-VMGuestFile commandlet.
Q: I have a lot of virtual machines that need VMware tools upgrade. Is it possible to do that
with PowerCLI?
A: This task can be completed in two ways. It should be used Add-VMHost if the VM Host is
not included in inventory and Move-VMHost when the VM host already is connected to the
vSphere Virtual Center.
Q: I want to perform some maintenance operations on one of my hosts, what should I do?
A: First the host should be put in maintenance mode. If the host is inside DRS cluster all
powered on virtual machines will be migrated to another to another host.
Q: I want to create resource pool with specific memory and CPU reservations and limits,
how can I do that?
New-ResourcePool -Name <resource pool name> -Location <Parent Cluster name or VMHost name or Resource pool> -CpuReserv
Other:
Q: How can I create folders that appear under "Virtual Machines and Templates" in VC (a
so-called "Blue Folder")?
Q: I'm concerned about the security implications of Invoke-VMScript, how can I disable it?
A: Invoke-VMscript can be disabled on a VM-by-VM basis by adding this line to the VM's
VMX file: monitor_control.restrict_backdoor = "true" . You can add this line by hand or you
can add it with the following script:
Please help us help our community - Add the questions you often get asked by your peers
and tired of having to answer it many times.