Importing DPM Settings

In the last article I showed you the code to export the settings.  In this one I show you the code to import them into the server.  I am assuming that the hostname, login and password are remaining.  You are just moving the servers from one virtual center server to another.

if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{Add-PSsnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue}
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
$sourceVI = Read-Host "Please enter the name of the source Server";
$creds = get-credential
$datacenter = Read-Host "Please give the name of the datacenter you would like to run against"
connect-viserver -server $sourceVI -credential $creds
import-csv "R:\Reports\Datacenter-exports\dpm-settings-$datacenter.csv" | %{
$hostview = get-vmhost $_.Name | % {Get-View $_.Id}
$IpmiInfo = New-Object Vmware.Vim.HostIpmiInfo
$IpmiInfo.BmcIpAddress = $_.BmcIpAddress
$IpmiInfo.BmcMacAddress = $_.BmcMacAddress
$IpmiInfo.Login = $_.Login
$IpmiInfo.Password = $_.password
$hostview.UpdateIpmi($IpmiInfo)}
Disconnect-VIServer -Server $SourceVI -Force -confirm:$false

 

Exporting DPM Settings

I recently was tasked to move several datacenters to balance the overall load on our virtual center servers.  One of the things I had to do was to export the existing host settings for Distributed Power Management (DPM).  I wrote the following script to export the settings to a CSV file.

if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{Add-PSsnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue}
#Change to multi-mode vcenter management
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false
#Get vCenter Server Names
$sourceVI = Read-Host "Please enter the name of the source Server";
$creds = get-credential
$datacenter = Read-Host "Please give the name of the datacenter you would like to run against"
#Connect to Source vCenter
connect-viserver -server $sourceVI -credential $creds
Function Get-IpmiInfo {
Begin {
# This hides the errors which appear if the VM Tools are not running
$ErrorActionPreference = "SilentlyContinue"
$Name = @{
Name    = "Name"
Expr     = { $_.name }
}
$BmcIpAddress = @{
Name    = "BmcIpAddress"
Expr     = { $_.config.ipmi.BmcIpAddress }
}
$BmcMacAddress = @{
Name    = "BmcMacAddress"
Expr     = { $_.config.ipmi.BmcMacAddress }
}
$Login = @{
Name    = "Login"
Expr     = { $_.config.ipmi.Login }
}
$Password = @{
Name    = "Password"
Expr     = { $_.config.ipmi.Password }
}
}
Process {
if ( $_.GetType().Name -ne 'VirtualMachineImpl' ) {
Throw "This script expects a VirtualMachineImpl object as input."
}
$_ | Select-Object $name, $BmcIpAddress, $BmcMacAddress, $Login, $Password
}
}
$report = @()
Get-datacenter $datacenter | get-vmhost | get-view | Get-IpmiInfo | export-csv "c:\temp\dpm-settings-$datacenter.csv" -notypeinformation
Disconnect-VIServer -Server $SourceVI -Force -confirm:$false

 

Basic vmkfstools Syntax

Excerpt taken from the ESX MAN Page.

FILE SYSTEM OPTIONS

The long and short forms of options, shown here listed together, are equivalent.

-C, –createfs vmfs3

-b, –blocksize #[mMkK]

-S, –setfsname fsName

Create a VMFS file system on the specified partition, e.g. vml.<vml_ID>:1. The partition becomes the file system‘s head partition. The file block size can be specified via the `-b` option. The default file block size is 1MB. The file block size must be either 1MB, 2MB, 4MB or 8MB.

The -S option sets the label of the VMFS file system, and can only be used in conjunction with the `-C` option. This label can then be used to specify a VMFS file system in subsequent vmkfstools commands or in a virtual machine configuration file. The label will also appear in a listing produced by `ls -l /vmfs/volumes`as a symbolic link to the VMFS file system. VMFS labels can be up to 128 characters long. They cannot contain leading or trailing spaces. After creating the file system, the label can be changed using the command `ln -sf /vmfs/volumes/<FS UUID> /vmfs/volumes/<New label>`.

-Z, –spanfs span-partition

Extend the VMFS-3 file system with the specified head partition by spanning it across the partition designated by `span-partition`. The operation erases existing data on the spanned partition. A VMFS-3 file system can have at most 32 partitions. This option will not work on VMFS-2 file systems as they are read-only in ESX 3.

-G, –growfs grow-partition

Extend the VMFS-3 file system with the specified `grow-partition`. Prior to growing the file system, users must use a tool such as `fdisk` or `parted` to create the partition first.  Once the partition size `grow-partition` is available, file system can be grown by designating the `grow-partition` using the option `-G`.  Existing data on the grow partition is preserved.

-P, –queryfs

-h, –human-readable

List the attributes of a VMFS file system when used on any file or directory of a VMFS file system. It lists the VMFS version number, the number of partitions constituting the specified VMFS file system, the file system label (if any), file system UUID, available space, and a listing of the device names of all the partitions constituting the file system. If partitions backing VMFS file system go offline then the number of partitions and available space reported change accordingly. The `h` option causes sizes to be printed in human-readable format (such as 5k, 12.1M, or 2.1G).

VIRTUAL DISK OPTIONS

-c, –createvirtualdisk #[gGmMkK]

-a, –adaptertype [buslogic|lsilogic|ide] srcFile

-d, –diskformat [thin|zeroedthick|eagerzeroedthick]

Create a virtual disk with the specified size on the VMFS file system. The size is specified in bytes by default, but can be specified in kilobytes, megabytes or gigabytes by adding a suffix of `k`, `m`, or `g` respectively. The `adaptertype` option allows users to indicate which device driver should be used to communicate with the virtual disk. The default disk format is `zeroedthick`.

-U, –deletevirtualdisk

Delete files associated with the specified virtual disk.

-E, –renamevirtualdisk srcDisk

Rename files associated with a specified virtual disk to the specified name.

-i, –clonevirtualdisk srcDisk

-d, –diskformat [rdm:<device>|rdmp:<device>|zeroedthick|thin|eagerzeroedthick|2gbsparse]

Create a copy of a virtual disk or raw disk.  The copy will be in the specified disk format.  The default disk format is pre-allocated.

-e, –exportvirtualdisk dstDisk

This operation is deprecated. Use `-i srcDisk -d 2gbsparse` to achieve what it used to.

-X, –extendvirtualdisk #[gGmMkK]

Extend the specified VMFS virtual disk to the specified length.  You can extend the virtual disk to a `eagerzeroedthick` format, if specified with the `-d eagerzeroedthick` option.  Extending a virtual disk will break any currently existing snapshots. This command is useful for extending the size of a virtual disk allocated to a virtual machine after the virtual machine has been created.  However, this command requires that the guest operating system has some capability for recognizing the new size of the virtual disk and taking advantage of this new size.

-M, –migratevirtualdisk

Migrate an ESX 2 virtual disk to an ESX 3 virtual disk.

-r, –createrdm /vmfs/devices/disks/…

Map a raw disk to a file on a VMFS file system.  Once the mapping is established, it can be used to access the raw disk like a normal VMFS virtual disk.  The `file length` of the mapping is the same as the size of the raw disk that it points to.

-q, –queryrdm

List the attributes of a raw disk mapping.  When used with a `rdm:<device>` specification, it prints out the vml of the raw disk corresponding to the mapping referenced by the <device>.  It also prints out identification information for the raw disk (if any).

-z, –createrdmpassthru /vmfs/devices/disks/…

Map a passthrough raw disk to a file on a VMFS file system.  This allows a virtual machine to bypass the VMKernel SCSI command filtering layer done for VMFS virtual disks.  Once the mapping is established, it can be used to access the passthrough raw disk like a normal VMFS virtual disk.

-v, –verbose #

This option is ignored for the queryrdm option.  Setting the verbosity level will list additional information for the virtual disk configuration.

-g, –geometry

Get the geometry information (cylinders, heads, sectors) of a virtual disk.

-w, –writezeros

Initialize the virtual disk with zeros. Any existing data on virtual disk is lost.

-j, –inflatedisk

Convert a thin virtual disk to preallocated with the additional guarantee that any data on thin disk is preserved and any blocks that were not allocated get allocated and zeroed out.

-k, –eagerzero

Convert a preallocated virtual disk to eagerzeroedthick and maintains any existing data.

SUPPORTED DISK FORMATS

`zeroedthick`

Space required for the virtual disk is allocated at creation time

`eagerzeroedthick`

Space required for the virtual disk is allocated at creation time.  In contrast to zeroedthick format, the data remaining on the physical device is zeroed out during creation.

`thin`

Thin-provisioned virtual disk.

`rdm`

Virtual compatibility mode raw disk mapping.

`rdmp`

Physical compatibility mode (pass-through) raw disk mapping.

`2gbsparse`

A sparse disk with 2GB maximum extent size.

VM Compare AD Group Script

If you apply GPO’s to servers for ease of administration and security, you may want an easy way if all your servers are in the appropriate group(in this case “Virtual Servers”) and compare this list to a list of all running Windows machines.  The following script will do this:

########################################################################
# Title:	VM Groupmembership Script
# Filename:	vmgroup.ps1
# Created by:	Douglas Smith
# Date:		7/14/2010
# Version:	0.1
# Description:		*This script requires the Quest AD cmdlets be installed.*
########################################################################
#### Pass through paramaeters ###################################################
param (
[string]$vcserver,
)
#### Variables ##############################################################
$Date = Get-Date
#### Functions ##############################################################
#### Begin Script ############################################################
#### Validate Input Variables ####################################################
If ($vcserver -eq "") {
$wscript = new-object -comobject wscript.shell
$msgbox = $wscript.popup("You MUST enter the name or IP of your vSphere server",0,"Access Denied",1)
exit
}
#### Check if Snapin loaded, if not load #############################################
if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{Add-PSsnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue}
if ((Get-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) -eq $null )
{Add-PSsnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue}
#### Connect to vSphere
$VIServer = connect-VIServer $vcserver
$adlist = get-qadgroupmember "Virtual Servers" -type computer | Select-Object name |
Sort-Object name | ForEach-Object{$_.name.tolower()}
$vms = Get-Vm | Where-Object {$_.powerstate -eq "PoweredOn"} |
Where-Object {$_.name.length -gt 10} |
Where-Object {$_.guest.osfullname -like quot;*indows*"} | Select-Object name |
Sort-Object name | ForEach-Object{$_.Name.ToLower()}
$vmcount = $vms.count
$AddMachine = Compare-Object $vms $adlist -syncwindow $vmcount |
Where-Object {$_.sideindicator -eq "< ="} | Sort-Object InputObject
$RemoveMachine = Compare-Object $vms $adlist -syncwindow $vmcount |
Where-Object {$_.sideindicator -eq "=>"} | Sort-Object InputObject
#### Create Spreadsheet
$excel = New-Object -comobject Excel.Application
#$excel.visible = $True #### Use for troubleshooting purposes
$excel.DisplayAlerts = $False
$excelfile = $excel.Workbooks.Add()
$WorkSheet = $excelfile.worksheets.item(1)
$Worksheet.Name = "Add to Group"
$Worksheet.Cells.Item(1, 1) = "Machines that need to be added to the Group"
$WorkBook = $Worksheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
$Row = 2
$Addmachine | ForEach-Object {
$Worksheet.Cells.Item($Row, 1) = $_.InputObject
$Row ++
}
[void]$WorkBook.EntireColumn.AutoFit()
$WorkSheet = $excelfile.worksheets.item(2)
$Worksheet.Name = "Remove from Group"
$Worksheet.Cells.Item(1, 1) = "Machines that need to be removed from the Group"
$WorkBook = $Worksheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
$Row = 2
$RemoveMachine | ForEach-Object {
$Worksheet.Cells.Item($Row, 1) = $_.InputObject
$Row ++
}
#### Adjust Columns
[void]$WorkBook.EntireColumn.AutoFit()
#### Save File
$excelfile.SaveAs("c:\temp\VirtualServerGroup-" + $date.month + "-" + $date.day + "-" + $date.year + ".xls", 1)
#### Quit Excel
$excel.Quit()
#Disconnect from vCenter Server
$VIServer | Disconnect-VIServer -Confirm:$false

Understand and apply VMFS resignaturing

Use datastore resignaturing if you want to retain the data stored on the VMFS datastore copy.
To resignature a mounted datastore copy, first unmount it. Before you resignature a VMFS datastore, perform a storage rescan on your host so that the host updates its view of LUNs presented to it and discovers any LUN copies.

Procedure

  1. Log in to the vSphere Client and select the server from the inventory panel.
  2. Click the Configuration tab and click Storage in the Hardware panel.
  3. Click Add Storage.
  4. Select the Disk/LUN storage type and click Next.
  5. From the list of LUNs, select the LUN that has a datastore name displayed in the VMFS Label column and click Next.  The name present in the VMFS Label column indicates that the LUN is a copy that contains a copy of an existing VMFS datastore.
  6. Under Mount Options, select Assign a New Signature and click Next.
  7. In the Ready to Complete page, review the datastore configuration information and click Finish.

The information for this article was gathered from the ESX Configuration Guide.