Reader small image

You're reading from  Mastering PowerCLI

Product typeBook
Published inOct 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781785286858
Edition1st Edition
Languages
Right arrow
Author (1)
Sajal Debnath
Sajal Debnath
author image
Sajal Debnath

Sajal Debnath is a highly certified Cloud computing technocrat with more than 12 years of experience in virtualized data center design, Cloud computing, and BC/DR solutions. He is an EMCISA, VCAP-DCD/DCA, VCAP-CID/CIA, RHCE 4/5/6, RHCVA, Openstack, and ITIL certified person. He is presently associated with VMware Software India Pvt. Ltd. as a senior system engineer. Previously, he worked with France Telecom, Hewlett Packard, and many more in multiple roles. He is involved in prestigious Indian government projects, such as National Cloud, Digital Locker, and so on.
Read more about Sajal Debnath

Right arrow

Chapter 7. Managing Virtual Machines

In the previous chapters, we discussed the different aspects of building and automating a vSphere environment using PowerCLI. In this chapter, we will discuss how to manage virtual machines. In general, we will discuss the following topics:

  • Creating and managing virtual machines

  • Modifying virtual machines

  • Managing templates

  • Managing OS customization specifications

  • Managing the guest OS

  • Managing vApps

We will start our discussion with the creation of VMs and other hardware-related aspects of a VM.

Creating virtual machines


We will use the following cmdlets to manage the different aspects of managing a virtual machine in a vSphere environment using PowerCLI cmdlets:

  • Get-VM

  • Move-VM

  • New-VM

  • Remove-VM

  • Set-VM

We will start by creating a simple virtual machine. To do this, we will use the New-VM cmdlet:

PS C:\> New-VM -Name Test3 -ResourcePool Mgmt -Datastore iSCSI-4 ` -NumCPU 1 -MemoryGB 1 -DiskGB 10 -NetworkName "VM Network" `
–Floppy -CD -DiskStorageFormat Thin -GuestID winNetDatacenterGuest

Name                 PowerState Num CPUs MemoryGB       
----                 ---------- -------- --------       
Test3                PoweredOff 1        1.000   

The output will be as follows:

Note that the ResourcePool parameter accepts ResourcePool, Cluster, VApp, and standalone VMHost as input options. For example, in the following screenshot, I am creating a VM directly under the cluster, Lab Cluster:

We can use the Get-VM cmdlet to check the list of available VMs or a particular VM:

PS...

Modifying virtual machines


Modifying a virtual machine can be divided into many categories. We have divided it as follows:

  • CD drives and floppy drives

  • Hard disks

  • Network adapters and USB devices

  • Passthrough devices

  • Snapshots

  • Resource configuration and policies

  • Managing VMware tools

As given in the preceding list, we will start with managing CD drives and floppy drives:

We will use the following cmdlets to manage the CD drives and floppy drives in a VM:

  • Get-CDDrive / Get-FloppyDrive

  • New-CDDrive / New-FloppyDrive

  • Remove-CDDrive / Remove-FloppyDrive

  • Set-CDDrive / Set-FloppyDrive

To get a list of connected CD or floppy drives, we can use the Get-CDDrive or Get-FloppyDrive cmdlet. For example, by running this cmdlet, we can find out that the Test-1 VM is currently not connected to any CD drive:

PS C:\> Get-CDDrive -VM Test-1

PS C:\>

So, let's first add a CD drive to the VM using the New-CDDRive cmdlet:

PS C:\> New-CDDrive -VM Test-1
WARNING: Parameter 'VM' is obsolete. Passing multiple values...

Managing templates


One of the major aspects of managing a vSphere environment is managing templates. In any virtualized data center, invariably, there will be many templates that are maintained in it. In this section, we will check the PowerCLI cmdlets related to template management. Here is a list of the available cmdlets used for managing templates:

  • Get-Template

  • Move-Template

  • New-Template

  • Remove-Template

  • Set-Template

Like all the other topics, in this topic too, we will start with getting a list of the available templates. We can do this using the Get-Template cmdlet:

PS C:\> Get-Template

Name                                              
----                                              
Template1                                    

PS C:\> Get-Template -Location 'Lab Folder'

Name                                              
----                                              
Template1                                    

PS C:\> Get-Template -Datastore NFS_SHARE

Name...

Managing OS customization specifications


In this topic, we will discuss how to manage OS customization specifications in a vSphere environment. We can use the following cmdlets to manage customization specifications:

  • Get-OSCustomizationSpec

  • New-OSCustomizationSpec

  • Remove-OSCustomizationSpec

  • Set-OSCustomizationSpec

To create a new guest OS customization specification, we can use the New-OSCustomizationSpec cmdlet. Using this cmdlet, we can specify the specification for both Windows and Linux VMs. To use it with Windows, there are certain mandatory parameters, which we need to specify. These are as follows:

  • A name

  • A domain or workgroup

  • A FullName and OrgName

In the case of a Linux VM, only a name and domain name need to be specified.

We can use the –OSType parameter to mention whether the VM is a Windows or Linux type. This parameter accepts only Windows and Linux as valid values. The cmdlet and the output is shown in the following example:

PS C:\> New-OSCustomizationSpec –Name 'TestSpec1...

Managing the guest OS


In this category, we will discuss how to manage the guest OS of a VM using PowerCLI. To manage the different aspects of the guest OS, we can use the following cmdlets:

  • Get-VMGuest

  • Restart-VMGuest

  • Stop-VMGuest

  • Suspend-VMGuest

  • Copy-VMGuestFile

  • Get-VMGuestNetworkInterface

  • Set-VMGuestNetworkInterface

  • Get-VMGuestRoute

  • New-VMGuestRoute

  • Remove-VMGuestRoute

  • Invoke-VMScript

We can use the Get-VMGuest cmdlet to get the guest operating system of the VM. Using the next three cmdlets, that is, Restart-VMGuest, Stop-VMGuest, and Suspend-VMGuest, we can control the power state of a VM guest operating system. The Get-VMGuest cmdlet gives the current status of a VM guest. Similarly, we can use the restart, stop, and suspend cmdlets to restart, stop, and suspend the state of a virtual machine:

PS C:\> Get-VMGuest "LAB AD"

State          IPAddress            OSFullName             
-----          ---------            ----------             
Running        {fe80::acd6:aa2f...

Managing vApps


So far, we have worked with creating and managing the different aspects of a virtual machine. In this section, I am going to discuss how to manage vApps in a vSphere environment and also import or export OVF or OVA VMs.

We can use the Get-OvfConfiguration cmdlet to get and control the required parameters of an OVF or OVA template.

First, let's check the Get-OvfConfiguration cmdlet to get the configuration of an OVF template:

In Chapter 9, Managing the vSphere API, we will discuss how to modify these parameters in order to supply the values from the command line.

We can use the Import-VApp cmdlet to import an existing vApp:

In the preceding example, we imported the existing OVA template into an ESXi host, ESXi1.lab.com.

Next, we will discuss how to manage vApps in a vSphere environment. We can use the following cmdlets to manage vApps:

  • Export-VApp

  • Get-VApp

  • Import-VApp

  • Move-VApp

  • New-VApp

  • Remove-VApp

  • Set-VApp

  • Start-VApp

  • Stop-VApp

First, let's get a list of all the existing...

Summary


In this chapter, we discussed how to manage the different aspects of a virtual machine in a vSphere environment using PowerCLI. We started this chapter with a discussion on how to create and manage virtual machines. We then discussed how to modify a virtual machine using PowerCLI followed by a discussion on how to manage templates in a vSphere environment. Next we covered how to manage OS customization specifications and guest OS management using PowerCLI. Finally, we covered how to manage vApps in a vSphere environment.

In the next chapter, we will discuss how to implement security best practices using PowerCLI and Update Manager. Then, we will cover how to automate DR using PowerCLI and SRM. Finally, we will discuss how to manage vCloud Air workloads followed by a discussion on managing vRealize Operations Manager using PowerCLI.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering PowerCLI
Published in: Oct 2015Publisher: PacktISBN-13: 9781785286858
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Sajal Debnath

Sajal Debnath is a highly certified Cloud computing technocrat with more than 12 years of experience in virtualized data center design, Cloud computing, and BC/DR solutions. He is an EMCISA, VCAP-DCD/DCA, VCAP-CID/CIA, RHCE 4/5/6, RHCVA, Openstack, and ITIL certified person. He is presently associated with VMware Software India Pvt. Ltd. as a senior system engineer. Previously, he worked with France Telecom, Hewlett Packard, and many more in multiple roles. He is involved in prestigious Indian government projects, such as National Cloud, Digital Locker, and so on.
Read more about Sajal Debnath