How to Create Library Resources: Command-Line Examples

The examples in this topic show you how to use the Windows PowerShell - Virtual Machine Manager command shell to perform the following tasks:

  • Create a hardware profile. Stores hardware configuration information that you can use repeatedly to create new virtual machines or templates for virtual machines.
  • Create a guest operating system profile. Stores operating system configuration information that you can use repeatedly to create new virtual machines or templates for virtual machines.
  • Create a template. Stores hardware configuration information and guest operating system information that you can use repeatedly to create new virtual machines.
  • Create a virtual hard disk. Stores a virtual machine's guest operating system and data.

The preceding tasks are examples of resources that you can store in the library catalog in the Virtual Machine Manager database.

In addition to the examples provided here, other examples for how to create or add resources to the library are available in the command-line help.

How to Create a Hardware Profile

You can create a new hardware profile for a virtual machine or a template that contains a network adapter, a SCSI adapter, and a DVD drive by typing the following series of commands at the Virtual Machine Manager command prompt:

PS C:\> Get-VMMServer -ComputerName "VMMServer1.Contoso.com"
PS C:\> $JobGroupId = [Guid]::NewGuid().ToString()
PS C:\> New-VirtualNetworkAdapter -JobGroup $JobGroupID -EthernetAddressType Dynamic -VirtualNetwork "Internal Network" 
PS C:\> New-VirtualSCSIAdapter -JobGroup $JobGroupID -AdapterID 6 -Shared $FALSE
PS C:\> New-VirtualDVDDrive -JobGroup $JobGroupID -Bus 1 -Lun 0 
PS C:\> New-HardwareProfile -Name "NewProfile2" -Owner "CONTOSO\Nicholas" -Description "Temporary Hardware Config used to create a VM/Template" -MemoryMB 512 -JobGroup $JobGroupID

Explanation of each command line:

  • The first command connects to VMMServer1 and retrieves the server object. The following commands use this server by default.
  • The second command generates a globally unique identifier (GUID) and stores the GUID in variable $JobGroupID. The job group ID functions as an identifier that groups subsequent commands into a single job group.
  • The third command creates a virtual network adapter but uses the JobGroup parameter to specify that the network adapter is not created until just before the New-HardwareProfile cmdlet (in the sixth command) runs. The third command sets the Ethernet address type to dynamic and specifies that the new virtual network adapter connects to a virtual network called "Internal Network."
  • The fourth command creates a virtual SCSI adapter but uses the JobGroup parameter to specify that the SCSI adapter is not created until just before the New-HardwareProfile cmdlet (in the sixth command) runs. The fourth command sets the adapter ID to 6 (which is the seventh ID because the range is 0–6), and it sets the Shared parameter to $FALSE so that the adapter is not shared (as it would have to be if you wanted to use it in guest clustering).
  • The fifth command creates a virtual DVD drive but uses the JobGroup parameter to specify that the DVD drive is not created until just before the New-HardwareProfile cmdlet (in the sixth command) runs. Specifying Bus 1 and Lun 0 attaches the virtual DVD drive to Secondary Channel (0) on the IDE bus.
  • The sixth command creates a hardware profile named NewProfile2, sets the owner to Nicholas (whose account is in the Contoso.com domain), specifies a description, and specifies that the amount of memory on the host that a virtual machine (created by using this template) uses is 512 MB. Before the New-HardwareProfile cmdlet creates the hardware profile, the JobGroup parameter in this final command runs all of the preceding cmdlets that specify the same JobGroup GUID. When New-VirtualNetworkAdapter, New-VirtualSCSIAdapter, and New-VirtualDVDDrive run, the resulting created objects are automatically associated with the new hardware profile.

For more information about how to use the command shell to create a hardware profile, type Get-Help New-HardwareProfile -detailed at the Virtual Machine Manager command prompt.

How to Create a Guest Operating System Profile

You can create a guest operating system profile for a virtual machine or template by typing the following series of commands at the Virtual Machine Manager command prompt:

PS C:\> Get-VMMServer -ComputerName "VMMServer1.Contoso.com"
PS C:\> $JoinDomainCredential = Get-Credential
PS C:\> New-GuestOSProfile -Name "NewProfile2" -JoinDomain "Contoso.com" -JoinDomainCredential $JoinDomainCredential

Explanation of each command line:

  • The first command connects to VMMServer1 and retrieves the server object. The following commands use this server by default.
  • The second command creates a Windows PowerShell credential object (PSCredential object) by prompting the user for the user name and password required to join a computer to the domain and stores the credentials in variable $JoinDomainCredential.
  • The third command creates a guest operating system profile called NewProfile2. When you use NewProfile2 to create a virtual machine, the profile uses the specified domain account and account password to join the virtual machine to the Contoso.com domain.
    The recommended practice is to use only an account with limited rights for automatically joining a virtual machine to a domain because the domain account name and password appear briefly in plain text on the virtual machine host.

For more information about how to use the command shell to create a guest operating system profile, type Get-Help New-GuestOSProfile -detailed at the Virtual Machine Manager command prompt.

For more information about how Windows PowerShell handles credentials, type Get-Help Get-Credential -detailed at the command prompt.

How to Create a Template from a Virtual Machine

You can create a template, which stores hardware configuration and operating system configuration information, from an existing virtual machine by typing the following series of commands at the Virtual Machine Manager command prompt:

PS C:\> $LibraryServer = Get-LibraryServer -VMMServer "VMMServer1.Contoso.com" | where {$_.Name -eq "FileServer01.Contoso.com"}
PS C:\> $VM = Get-VM -VMMServer "VMMServer1.Contoso.com" -Name "VM01" | where {$_.VMHost.Name -eq "VMHost1.Contoso.com"}
PS C:\> $VHD = Get-VirtualHardDisk -VM $VM | where {$_.Name -eq "Win2003EER2Base"}
PS C:\> New-Template -Name "Template1" -VM $VM -BootVirtualHardDisk $VHD -LibraryServer $LibraryServer -SharePath "\\FileServer01.Contoso.com\SCVMM" -RunAsynchronously

Explanation of each command line:

  • The first command retrieves the object that represents the library server called FileServer01 from the Virtual Machine Manager database on VMMServer1 (both servers are in the Contoso.com domain) and stores the library server object in variable $LibraryServer.
  • The second command retrieves from the Virtual Machine Manager database the object that represents the virtual machine named VM01 (which is currently deployed on VMHost1 in the Contoso.com domain) and stores the virtual machine object in variable $VM. VM01, which is the virtual machine that will be converted to a template, is destroyed during the conversion process. (If you want to keep the virtual machine that you are using to create a template, you can use New-VM to clone the virtual machine before you create the template.)
  • The third command retrieves the object that represents the virtual hard disk named Win2003EER2Base from VM01 and stores the virtual hard disk object in variable $VHD.
  • The fourth command creates a template named Template1 from VM01. It specifies the virtual hard disk on VM01 that contains the boot operating system and specifies the library server and share where you want to store the new template. The -RunAsynchronously parameter returns control to the shell immediately (before the command completes).

For more information about how to use the command shell to create a template, type Get-Help New-Template -detailed at the Virtual Machine Manager command prompt.

Note

This example demonstrates only one method of creating a virtual machine template. You can also create a default template, or you can create a template from a virtual hard disk or from an existing template.

How to Create a Virtual Hard Disk

You can create a virtual hard disk, on which you can install a virtual machine's guest operating system and data, by typing the following series of commands at the Virtual Machine Manager command prompt:

PS C:\> Get-VMMServer -ComputerName "VMMServer1.Contoso.com"
PS C:\> $VM = Get-VM -Name "VM01"
PS C:\> New-VirtualHardDisk -VM $VM -Dynamic -Size 1000 -SCSI -Bus 0 -Lun 2 -Filename "DataDisk1.vhd"

Explanation of each command line:

  • The first command connects to VMMServer1 in the Contoso.com domain and retrieves the server object from the Virtual Machine Manager database. The following commands use this server by default.

  • The second command gets from the Virtual Machine Manager database the object that represents the virtual machine named VM01 and stores the virtual machine object in variable $VM.

  • The third command creates a dynamic virtual hard disk named DataDisk1.vhd, specifies that its maximum size is 1000 MB, and attaches it to the SCSI bus of VM01.

    Note

    This example assumes that a virtual SCSI adapter is already connected to the virtual machine. For more information, type "Get-Help New-VirtualSCSIAdapter" at the command prompt.

For more information about how to use the command shell to create a virtual hard disk, type Get-Help New-VirtualHardDisk -detailed at the Virtual Machine Manager command prompt.

See Also

Concepts

How to Add Hosts and Library Servers: Command-Line Examples
How to Deploy or Store a Virtual Machine and Find a Suitable Host: Command-Line Examples
How to Enable Self-Service Users: Command-Line Examples