Loading

×Sorry to interrupt
CSS Error

Refresh

Cookie Preference Center

General Information on cookies

When you visit our website, we use cookies to ensure that we give you the best experience. This information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies by clicking on the different category headings to find out more and change your settings. However, blocking some types of cookies may impact your experience on the site and the services we are able to offer. Further information can be found in our

Cookie Policy.

Strictly Necessary

Strictly Necessary

Always Active

Strictly Necessary

Strictly necessary cookies are always enabled since they are essential for our website to function. They enable core functionality such as security, network management, and website accessibility. You can set your browser to block or alert you about these cookies, but this may affect how the website functions. For more information please visit www.aboutcookies.org or www.allaboutcookies.org.

Cookie Details‎

Performance

Performance

Performance

Performance cookies are used to analyze the user experience to improve our website by collecting and reporting information on how you use it. They allow us to know which pages are the most and least popular, see how visitors move around the site, optimize our website and make it easier to navigate.

Cookie Details‎

Please confirm your settings by refreshing the page.

Functional

Functional

Functional

Functional cookies help us keep track of your past browsing choices so we can improve usability and customize your experience. These cookies enable the website to remember your preferred settings, language preferences, location and other customizable elements such as font or text size. If you do not allow these cookies, then some or all of these services may not function properly.

Cookie Details‎

Advertising

Advertising

Advertising

Advertising cookies are used to send you relevant advertising and promotional information. They may be set through our site by third parties to build a profile of your interests and show you relevant advertisements on other sites. These cookies do not directly store personal information, but their function is based on uniquely identifying your browser and internet device.

Cookie Details‎

Advertising Cookies

Consent Leg.Interest

Select All Vendors

Select All Vendors

Select All Hosts

Select All

Clear Filters

Information storage and access

Apply

Confirm My Choices Reject All Allow All

My Subscriptions

Loading

Knowledge Base

Managing virtual machines using PowerCLI (1028291)

Last Updated: 11/20/2017Categories: TroubleshootingTotal Views: 8736Language:
Chinese (Simplified)English

5 like this article

Subscribe to get an email
newsletter with new updates

5 people found this helpful Please provide article feedback:

Purpose

vSphere PowerCLI can be used to automate the different virtual machine tasks. vSphere PowerCLI provides easy-to-use C# and PowerShell interface to VMware vSphere APIs.

In PowerShell, commands are called cmdlets, a term used in this article.

This article provides an overview of the different cmdlets that are available to:

Collect information about the virtual machine

Configure the virtual machine

Work with virtual machine snapshots

Update VMware Tools

Resolution

Collecting information about the virtual machine

This section discusses the commands that can be used to collect detailed information about the virtual machine. This article provides an overview of the commands they can be customized according to the needs.

Collecting information about virtual machine hardware

To get a list of all the virtual machines in the inventory and their name, power state, number of CPUs, and configured memory, run the cmdlet:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM

Name PowerState Num CPUs Memory (MB)
---- ---------- -------- -----------
NW PoweredOff 1 512
PowerCLI PoweredOn 1 1024

Note: This command provides information about the version, virtual hardware attached (virtual disk, network, CD-ROM), datastore, host, High Availability restart, and isolation properties:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM windows-dc | Format-List *

Collecting information about the virtual machine guest operating system

vSphere PowerCLI provides cmdlets to retrieve the details about the virtual machine guest operating system. These cmdlets are independent of the guest operating system installed in the virtual machine.

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VMGuest -VM windows-dc | Format-List *

OSFullName : Microsoft Windows Server 2003, Enterprise Edition (64-bit)
IPAddress : {IP_Address}
State : Running
HostName : windows-dc.vcd.com
Nics : {}
ScreenDimensions : {Width=1024, Height=768}

Note: When retrieving the details about the guest operating system, you are prompted for the user name and password for the ESX/ESXi host and guest operating system. You can provide the authentication details in the command by using the -HostUser root -HostPassword pass1 -GuestUser administrator -GuestPassword vmware123 parameters.

Using PowerCLI, you can also query, start, or stop a service within the guest operating system. You can use Start-Service, Stop-Service, and Restart-Service cmdlets to modify the status of the service. If no virtual machine is specified in the command the change is be made on all the Windows virtual machines in the inventory.

Note: To use this command ensure that the latest version of VMware Tools are installed on the virtual machine and this cmdlet is applicable only for Windows guest.

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM windows-dc | Invoke-VMScript "Get-Service app*"

Status Name DisplayName
------ --- -----------
Stopped AppMgmt Application Management

vSphere PowerCLI also provides cmdlets to provide the network configuration information from within the guest operating system:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VMGuestNetworkInterface -VM windows-dc -HostUser root -HostPassword vmware123 -GuestUser administrator -GuestPassword vmware123

VMId : VirtualMachine-vm-33
VM : windows-dc
NetworkAdapter : Network adapter 1
SubnetMask : 255.255.255.0
NicId : VirtualMachine-vm-33/4000
Name : Local Area Connection
IPPolicy : Static
Ip : IP_Address
Dns : {IP_Address}
DefaultGateway : 192.168.0.2
Description : Intel(R) PRO/1000 MT Network Connection
Mac : MAC_Address
RouteInterfaceId : 0x10003
Uid : /VIServer=@xx.xx.xx.xx:443/VMGuestNetworkInterface=00-11-22
-aa-bb-cc/
DnsPolicy : Static
WinsPolicy : Static
Wins :

To retrieve the routing configuration of the specified virtual machine, run the cmdlet:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VMGuestroute -VM windows-dc -HostUser root -HostPassword vmware123 -GuestUser administrator -GuestPassword vmware123

Destination : 0.0.0.0
Gateway : 192.168.0.2
Interface : IP_Address
Netmask : 255.255.255.0
VMId : VirtualMachine-vm-33
VM : windows-dc

Configuring a virtual machine using PowerCLI

This section discusses the cmdlets that you can use to configure/reconfigure the virtual machines.

To create new virtual machines, run the cmdlet:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> New-VM -VMHost xx.xx.xx.xx -Name TestCli -MemoryMB 1024 -DiskMB 8024

Name PowerState Num CPUs Memory (MB)
---- ---------- -------- - ----------
TestCli PoweredOff 1 1024

To migrate a virtual machine using vMotion, run the cmdlet:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VMHost xx.xx.xx.xx | Get-VM "windows-cli" | Move-VM -Destination xx.xx.xx.xx

Name PowerState Num CPUs Memory (MB)
---- ---------- -------- -----------
windows-cli PoweredOn 1 1024

To migrate a virtual machine using Storage vMotion, run the cmdlet:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM windows-rhel5 | Move-VM -Datastore vCloud-1

Name PowerState Num CPUs Memory (MB)
---- ---------- -------- -----------
windows-rhel5 PoweredOn 1 3072

All of the virtual machines in the in the inventory can be configured with or without the CD-ROM drive using this command:

Note: To disable CD-ROM, use the $false option. To enable CD-ROM, use the $true option.

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM | Get-CDDrive | Set-CDDrive -Connected:$false

Confirm
Are you sure you want to perform this action?
Performing operation "Setting Connected: False, NoMedia: False." on Target "CD/DVD Drive 1".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):Y

If you would like to remove confirmation option, then add this at the end of commandlet "-Confirm:$false", this is an example:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM | Get-CDDrive | Set-CDDrive -Connected:$false -confirm:$false

Working with virtual machine snapshots

vSphere PowerCLI provides cmdlets to create/remove snapshots for all of the virtual machines in the inventory. However, you may further customize the command to specify virtual machines from a specific ESX/ESXi host, cluster, or datacenter.

To take a new snapshot for all virtual machines in a cluster, run the cmdlet:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-Cluster "vCloud" | Get-VM | New-Snapshot -Name Automate

Name Description PowerState
---- ----------- ----------
Automate PoweredOff
Automate PoweredOff

To remove a snapshot from all of the virtual machine in the inventory, run the cmdlet:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM | Get-Snapshot | Remove-Snapshot

Confirm
Are you sure you want to perform this action?
Performing operation "Removing snapshot." on Target
"VirtualMachineSnapshot-snapshot-174".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(default is "Y"):A

To remove a confirmation option, add this at the end of commandlet:

-Confirm:$false

For example:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-VM | Get-Snapshot | Remove-Snapshot -Confirm:$false

Note: Use these commands with caution as the changes made are applicable for all virtual machines.

Updating VMware Tools

Update-Tools cmdlets can be used to update the VMware Tools on a single or multiple virtual machines. This command reboots the virtual machine after updating the VMware Tools. You can use the -NoReboot option to update the VMware Tools without rebooting the virtual machine.

In this example, the VMware Tools for all of the virtual machines in Resource Pool vCloud are updated without rebooting the virtual machine:

[vSphere PowerCLI] C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Get-ResourcePool vcloud | Get-VM | Update-Tools -NoReboot
WARNING: Automatic update of VMware tools is not fully supported for
non-Windows OSs. Manual intervention might be required.

Note: Before running the cmdlet, ensure that the VMware Tools service is running. This command is used only to upgrade VMware Tools. To do an unattended install, use msiexec.exe. For more information, see the Microsoft article Command - Line Options.

Related Information

使用 PowerCLI 管理虚拟机

Detectable by VMware SkylineTM

Actions

Copy

link to clipboard
copied!

Print

Language:
Chinese (Simplified)English

Attachments

Additional Resources

KB
•
End Of General Availability of the Free vSphere Hypervisor (ESXi 7.x and 8.x) (2107518)

KB
•
Download and Install VMware Fusion (2014097)

Results 1-2 of 2

Ask The Community

Get answers quickly from VMware experts in the community

Post Subject

CONTINUE IN COMMUNITIES

Related Products:

VMware vSphere PowerCLI

VMware vSphere

Related Versions:

VMware vSphere PowerCLI 4.0

Take Our Survey

Support Assistant