Pages

Thursday 15 August 2013

PowerCLI Copy-VMGuestFile Cmd-let


Hi all,
 
This is one of my most used, and favourite cmdlets in PowerCLI.  It allows you to copy a file to a guest VM from where ever you’re running your PowerCLI session and vice-versa. 
 
What’s the benefit of that I hear you ask?

What if you have a template that you haven’t assigned an IP address to, and that you don’t want to assign an IP to due to stringent policies in place by your company, but you still really need to get that file up to the VM?

Basically any restriction on network access and this command can help you out.

It uses TCP port 902 to transfer the file, so you must have that port open for this to work!  You can test this just by trying to open a console session to the VM in vCenter.

So to utilise this cmdlet:

Copy-VMGuestFile -Source *Source path from your local machine*  -Destination *Path to required destination in the VM*  –VM *vmname*  -HostUser *Admin account for the ESX\ESXi host the VM is resident on* -HostPassword **** -GuestUser “Domain or Computername\Username for the VM OS”  -GuestPassword ****** -LocalToGuest (or -GuestToLocal) **Defines the direction of the file copy**
 

You can also use credential store xml files to authenticate using the –guestcredentials and –hostcredentials parameters.

There are some pre-requisites to be met before you use this cmdlet however:

·         You must have VMTools installed in the guest
·         You must run this cmdlet from the 32-bit version of PowerCLI
·         It only runs on the following OSs:

XP 32-bit SP3

2003 32-bit SP2

2003 64-bit SP2

Windows 7 64-Bit

Windows Server 2008 R2 64-Bit

RHEL 5
 
·         For vCenter Server/ESX/ESXi versions earlier than 4.1, you need the VirtualMachine.Interact.ConsoleInteract privilege. For vCenter Server/ESX/ESXi 4.1 and later, you need the VirtualMachine.Interact.GuestControl privilege.
To run this cmdlet against vCenter Server/ESXi 5.0 and later, you need VirtualMachine.GuestOperations.Modify privilege.



Hopefully you’ll get as much use out of this as I do!

 

Chris.

Wednesday 7 August 2013

VMDK Location Check Script

Hi everyone,
 
As a part of the clean-up of my first post, there was a requirement to move the VMDKs from the 3rd Parties datastores, to our own datastore cluster.
Because of wanting to stagger the VMDK migrations I decided to write a separate script rather than tacking it on this on to the end of the previous script.
 
The first portion is producing some nice reporting.  I used the Convertto-html cmdlet for this and works quite well I think!
After the report is produced the decision is there to be made by the user as to whether there is the space to migrate the requesteded amount of VMDKs.
 
The script is designed to be run peace-meal as personally I didn’t like the idea of terabytes of data flooding the storage network!
 
Here it is:
 
#######################################################################
# Script Name: VMDK_Location_Check.ps1
# Purpose: To migrate VMDKs from non-compliant datastores
#
#
# Created by: Chris Lawrence http://hoipoiloi.blogspot.com
# Date: 01/08/2013
#######################################################################
 
#Variables.  Please amend as required:
#How many VMDKs you want to move at once
$X = 5
#Datastore you want to move the VMDKs to
$Datastore = Get-datastore -Name "Your Datastore"
 
#------------
#Sets some manadatory variable
$Administrator = [Environment]::UserName
$Path = "C:\Users\$Administrator\Documents"
$VMDKLocation = Get-vm | Get-HardDisk | Where {$_.filename -notmatch "DATASTORE"}
$TotalGB = $VMDKLocation | Measure-Object CapacityGB -Sum
$GBsInX = $VMDKLocation |Select -First "$X" | Measure-Object CapacityGB -Sum
$FreeSpace = $Datastore.freespaceGB - $gbsinX.sum
$FreeSpace = "{0:N0}" -f $Freespace
 
#------------
#Report creation
#Pre-define some table and header styles using HTML
$Header = @"
<style>TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #A74AC7;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"@
 
#Create some HTML code using the Convertto-html cmdlet.
$Frag1 =  $totalgb | ConvertTo-HTML -Property Count, Sum -As Table -Body "<H2>Total VMs and Total GBs non-compliant</H2>"
$Frag2 =  $GBSinX | ConvertTo-HTML -Property Count, Sum -As Table -Body "<H2>Total specified VMs and GBs to be moved</H2>"
$Frag3 =  ConvertTo-HTML -Body "<H2>Free space is $Freespace(GBs) in $datastore once $X machines have been made compliant</H2>"
$Frag4 =  $VMDKLocation | ConvertTo-HTML -Body "<H2>Complete list of the non-compliant VMs</H2>" -Property Parent, CapacityGB, FileName -As Table -Head $Header
#Combine these html fragments to one report:
ConvertTo-Html -Body "$Frag1 $Frag2 $Frag3 $Frag4" -Title "VMDK Location Report" | Out-File $path\Report.html
Invoke-Expression -Command  $path\Report.html
 
      Write-host -ForegroundColor Green "
      If the amount of free space is acceptable, hit ENTER to
      run the SvMotion portion of the script
      If not press Control-C to quit the script"
Read-Host
 
$Datastores = $vmdklocation | Select -First "$X"
      Foreach ($VM in $Datastores)
{
Move-VM -VM $VM.parent -Datastore "Datastore" -RunAsync
}
 
You can put as many of the  Where {$_.filename -notmatch "DATASTORE"} lines in as you want, depending on how many datastores you have that are within your “compliant” list.  Just repeat the comment separated by a | .  So:
Where {$_.filename -notmatch "DATASTORE"} | Where {$_.filename -notmatch "DATASTORE"}
 
Other than that, happy scripting!
 
Chris.