Sample Scripts for Managing vSphere with VMware PowerCLI
To help you get started with VMware PowerCLI , this documentation provides a set of sample scripts that illustrate basic and advanced tasks in vSphere administration.
Connect to a vCenter ServerSystem To run PowerCLI cmdlets on vSphere and perform administration or monitoring tasks, you must establish a connection to an ESXi host or a vCenter Server system. Manage Virtual Machines on vSphere With PowerCLI , you can automate various administration tasks on virtual machines, for example retrieving information, shutting down and powering off virtual machines. Add a Standalone Host to a vCenter Server System You can add standalone hosts to a vCenter Server system by using the Add-VMHost cmdlet. After adding the hosts, you will be able to manage them through the vCenter Server system.Set the License Key for a Host on vCenter Server You can set the license key for a host on a vCenter Server system by using the LicenseKey parameter of the Set-VMHost cmdlet. Activate Maintenance Mode for a Host on vCenter Server To complete some specific administration tasks, you might need to activate maintenance mode for a host. On vCenter Server, you can activate maintenance mode by using the Set-VMHost cmdlet. Create vSphere Inventory Objects By using PowerCLI cmdlets, you can automate creating different inventory objects on vSphere. Create Virtual Machines on vCenter Server Using an XML Specification File You can use a specification provided in an XML file to automate the creation of virtual machines on vCenter Server.Manage Virtual Machine Templates on vCenter Server You can use PowerCLI to create virtual machines templates and convert them to virtual machines on vCenter Server.Create and Use Snapshots on vCenter Server You can use the Snapshot parameter of Get-VM to take a snapshot of virtual machines and then revert the states of the virtual machines back to the snapshot. Update the Resource Configuration Settings of a Virtual Machine on vCenter Server You can use the Set-VMResourceConfiguration cmdlet to modify the resource configuration properties of a virtual machine, including memory, CPU shares, and other settings.Get a List of Hosts on a vCenter Server System and View Their Properties With PowerCLI , you can get information about all available hosts in a data center and view their properties. Change the Host Advanced Configuration Settings on vCenter Server You can modify host configuration, including advanced settings related to virtual machine migration, and apply them to another host.Move a Virtual Machine to a Different Host Using VMware vSphere vMotion You can migrate a virtual machine between vCenter Server hosts by using vSphere vMotion.Move a Virtual Machine to a Different Datastore Using VMware vSphere Storage vMotion You can migrate a virtual machine between datastores using the VMware Storage vMotion feature of vCenter Server.Move a Virtual Machine to a Different vCenter Server System You can migrate a virtual machine from one vCenter Server system to another by using Cross vCenter Server vMotion. Create a Host Profile on a vCenter Server System The VMware Host Profiles feature enables you to create standard configurations for ESXi hosts. With PowerCLI , you can automate creation and modifying of host profiles. Apply a Host Profile to a Host on vCenter Server To simplify operational management of large-scale environments, you can apply standard configurations called host profiles to hosts on vCenter Server. If you want to set up a host to use the same host profile as a reference host, you can attach the host to a profile. Manage Statistics and Statistics Intervals on vCenter Server You can use the PowerCLI cmdlets to automate tasks for viewing and managing statistics for vCenter Server inventory objects.Modify the Settings of the NIC Teaming Policy for a Virtual Switch You can set the NIC teaming policy on a vSwitch. The NIC teaming policy determines the load balancing and failover settings of a virtual switch and lets you mark NICs as unused.Create a vApp on vCenter Server With PowerCLI , you can create and manage vApps.Modify the Properties of a vApp With PowerCLI , you can start and stop vApps, and modify their properties.Export or Import vApps You can import and export vApps to OVA and OVF files.Create an iSCSI Host Storage For a host, you can enable iSCSI, add iSCSI targets, and create new host storages.Add Passthrough Devices to a Host and Virtual Machine You can get information about existing passthrough devices and add new SCSI and PCI devices to virtual machines and hosts.Create a Custom Property Based on an Extension Data Property You can create custom properties to add more information to vSphere objects. Custom properties based on extension data properties correspond directly to the property of the corresponding .NET view object.Create a Script-Based Custom Property for a vSphere Object You can create a custom property by writing a script and providing a name for the property. The script evaluates when the custom property is called for the first time.Apply a Customization Object to a Cloned Virtual Machine You can apply a custom configuration to a cloned virtual machine by using a customization object.Modify the Default NIC Mapping Object of a Customization Specification You can modify the default NIC mapping object of a customization specification and apply the specification on a newly created virtual machine.Modify Multiple NIC Mapping Objects of a Customization Specification You can modify multiple NIC mapping objects of a customization specification and apply the specification to an existing virtual machine.Create Multiple Virtual Machines that Use Static IP Addresses You can deploy multiple virtual machines with a single network adapter and configure the deployed virtual machines to use static IP addresses by applying a customization specification. Create Multiple Virtual Machines with Two Network Adapters You can deploy multiple virtual machines with two network adapters each and configure each adapter to use specific network settings by applying a customization specification. Create a vSphere Role and Assign Permissions to a User With PowerCLI , you can automate management of vSphere permissions, roles, and privileges. View the Action Triggers for an Alarm on vCenter Server You can see which action triggers are configured for an alarm.Create and Modify Alarm Actions and Alarm Triggers on vCenter Server With PowerCLI , you can create and modify vCenter Server alarm actions and alarm triggers.Remove Alarm Actions and Triggers In some cases, you might want to remove obsolete alarm actions and triggers.Create and Modify Advanced Settings for a Cluster You can customize the behavior of a cluster on a vCenter Server system by creating and modifying custom advanced settings for it.Modify the vCenter Server Email Configuration You can modify the email configuration settings of a vCenter Server.Modify the vCenter Server SNMP Configuration To use SNMP, you must first configure the SNMP settings of the vCenter Server.Use Esxtop to Get Information on the Virtual CPUs of a Virtual Machine You can use the Get-EsxTop cmdlet to retrieve real-time data for troubleshooting performance problems. Filter vSphere Objects with Get-View You can use the Get-View cmdlet to filter vSphere objects before performing various actions on them.Populate a View Object with Get-View To save time and efforts, you can use Get-View to retrieve PowerCLI views from previously retrieved view objects.Update the State of a Server-Side Object You can use the Get-View cmdlet to update server-side objects.Reboot a Host with Get-View You can reboot a host by using its corresponding view object.Modify the CPU Levels of a Virtual Machine with Get–View and Get–VIObjectByVIView You can modify the CPU levels of a virtual machine using a combination of the Get-View and Get-VIObjectByVIView cmdlets.Browse the Default Inventory Drive You can browse the default inventory drive and view its contents.Create a New Custom Inventory Drive In addition to the default drive, you can create new custom inventory drives by using the New-PSDrive cmdlet.Manage Inventory Objects Through Inventory Drives You can use the PowerCLI Inventory Provider to browse, modify, and remove inventory objects from inventory drives.Browse the Default Datastore Drives You can use the PowerCLI Datastore Provider to browse the default datastore drives: vmstore and vmstores .Create a New Custom Datastore Drive You can use the PowerCLI Datastore Provider to create custom datastore drives.Manage Datastores Through Datastore Drives You can use the PowerCLI Datastore Provider to browse datastores from datastore drives.Modify the Timeout Setting for Web Tasks To avoid unexpected timeouts, you can run Set-PowerCLIConfiguration to modify the PowerCLI settings for long-running Web tasks. Using Tags You can assign tags to different types of objects, such as virtual machines, resource pools, datastores, and vSphere distributed switches. You can use tags to retrieve a specific group of objects. Network Management with vSphere Distributed Switches The cmdlets provided in the VMware.VimAutomation.VDS module let you manage networking with vSphere distributed switches and port groups. Create a Virtual Machine from a Content Library Item You can deploy a virtual machine from a content library template. Create a vApp from a Content Library Item You can deploy a vApp from a content library template. Create a New VM-VM DRS Rule You can create a VM-VM DRS affinity rule within a cluster. Create a New VM-VMHost DRS Rule You can create a VM-VMHost DRS rule within a cluster after creating a VM DRS cluster group and a VMHost DRS cluster group.