PowerCLI: Get Snapshot Information

Get Snapshot Information KB ID 0001829

Problem

The question was asked on Experts Exchange today.

Are there any scripts or reports that would give me information on VMware VM’s with snapshots?

was pretty sure this was a straight forward one, so I jumped on the test network.

Solution: Get Snapshot Information

Connect to your vCenter and use the following commands.

[box]

Connect-viserver vCenter-Name 
THEN AUTHENTICATE
Get-VM | Get-Snapshot | Select-Object VM, Name, SizeGB, Created

[/box]

That was easy!

Get Snapshot Information : With RV Tools

You can also get the same information from RVTools, which if you don’t already use, do so!

Solution: PowerShell Delete Snapshots

You can delete all snapshots by simply piping the command above to Remove-Snapshot, But you will porbably want to do that on a VM by VM basis. Use the cfollowing command.

[box]

 Get-VM VM-Name| Get-Snapshot | Remove-Snapshot

[/box]

Related Articles, References, Credits, or External Links

NA

VMware: PowerCLI Errors

PowerCLI Errors KB ID 0001830

Problem

I was doing some work recently and tried to connect to my vCenter server and was greeted with this.

PS C:\Users\administrator.PNL> connect-viserver vc-70.pnl.com
connect-viserver : Object reference not set to an instance of an object.
At line:1 char:1
+ connect-viserver vc-70.pnl.com
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Connect-VIServer], NullReferenceException
+ FullyQualifiedErrorId : System.NullReferenceException,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

Some internet searching led me to find out I simply needed to update PowerCLI  – but when I did that this happened.

PS C:\Users\administrator.PNL> Update-Module VMware.PowerCLI
PackageManagement\Install-Package : Authenticode issuer ‘CN=DigiCert Trusted Root G4, OU=www.digicert.com, O=DigiCert
Inc, C=US’ of the new module ‘VMware.VimAutomation.Sdk’ with version ‘13.1.0.21605170’ is not matching with the authenticode issuer ‘CN=VeriSign Class 3 Public Primary Certification Authority – G5, OU=”(c) 2006 VeriSign, Inc. – For authorized use only”, OU=VeriSign Trust Network, O=”VeriSign, Inc.”, C=US’ of the previously-installed module ‘VMware.VimAutomation.Sdk’ with version ‘12.0.0.15939651’. If you still want to install or update, use -SkipPublisherCheck parameter. At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:2089 char:20 + … $sid = PackageManagement\Install-Package @PSBoundParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Microsoft.Power….InstallPackage:InstallPackage) [Install-Package],
Exception
+ FullyQualifiedErrorId : AuthenticodeIssuerMismatch,Validate-ModuleAuthenticodeSignature,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

Errm OK?

Solution: PowerCLI Errors

Seems like the fix for both errors is to simply update PowerCLI, updating actually does not work (as you can see). So, you need to FORCE an clean install of PowerCLI and also ignore the certificate error (above) with a SkipPublisherCheck switch.

[box]

Install-Module VMware.PowerCLI -Force -SkipPublisherCheck

[/box]

Then all was well with the world!

Related Articles, References, Credits, or External Links

NA

VMware: Find Connected ISO’s

KB ID 0001708

Problem

If you want to search your VMware estate to find VMs that have connected CD/DVD ISO files, then here are your best two options;

Option 1: Use PowerCLI

Whilst connected to your virtual infrastructure (Connect-VIServer) issue the following command;

[box]

Get-VM | FT Name, @{Label="ISO file"; Expression = { ($_ | Get-CDDrive).ISOPath }}

[/box]

Option 2: Use PowerCLI

If you don’t already have RVTools then get it downloaded! One of the many cool things it does, is show ISO data. (On the vCD tab).

Ejecting All Connected VMware ISO files.

If you want to eject all those ISO files you can use the following PowerCLI syntax;

[box]

Get-VM | Get-CDDrive | where {$_.IsoPath -ne $null} | Set-CDDrive -NoMedia -Confirm:$False

[/box]

Note This will not work if a VM is powered off, and you will see a “The operation is not allowed in the current state.” error.

Related Articles, References, Credits, or External Links

NA

PowerCLI: Get All Snapshot Information

KB ID 0001690

Problem

This was asked on EE today, and it was an interesting one so I wrote it up. How to locate all the Snapshots in your VMware virtual infrastructure, and see how much space they are taking.

Solution

Use the following PowerCLI;

[box]

Get-Snapshot * | Select-Object -Property VM, Name, SizeGB, Children | Sort-Object -Property sizeGB -Descending | ft -AutoSize

[/box]

Related Articles, References, Credits, or External Links

NA

vSphere: Get ESX Server Serial Numbers

KB ID 0001670

Problem

A few weeks ago I needed to sort out some extended warranty for a customers servers. To do that  I needed the serial numbers of those servers, (a mixture of IBM/Lenovo and Dell Servers).

As I didn’t fancy a drive to two different datacenters, I wanted to try and get them programatically.

Solution

After some searching I came across a post by one of my old EE buddies LucD with exactly what I needed. I’m assuming you have PowerCLI setup before beginning.

Connect to your Virtual infrastructure;

[box]

Connect-VIServer {vCenter-server-FQDN}

[/box]

Then, (assuming you have a folder called C:\Temp that you can write to).

[box]

Get-VMHost | Select Name, @{N='Serial';E={(Get-EsxCli -VMHost $_).hardware.platform.get().SerialNumber}} | Export-Csv c:\temp\serial.csv -NoTypeInformation -UseCulture

[/box]

Then  open your C:\Temp\SerialNumber.csv file, and there’s your serial numbers.

 

Related Articles, References, Credits, or External Links

NA

VMware: List/Audit VMware Tools Versions

KB ID 0001618

Problem

If you want to either audit, or simply get a quick list of which of your VMs are running which versions of VMware Tools, here are a few options.

Show VMware Tools Versions With PowerCLI

Use the following Syntax

[box]

Get-VM | Select-Object -Property Name,@{Name='ToolsVersion';Expression={$_.Guest.ToolsVersion}}

[/box]

Like so;

Show VMware Tools Versions With VI Client

Using HTML Client To Show VMware Tools Information

Select either a host or the virtual center > VMs > More Info arrow > Show/Hide Columns > VMware Tools Options.

Using FLEX Client To Show VMware Tools Information

Select either a host or the virtual center > VMs > Columns Icon > VMware Tools Options.

Use RVTools To Audi VMware Tools Versions

I use RVTools a lot, especially for auditing new clients infrastructures, it has a section dedicated to VMware Tools.

Related Articles, References, Credits, or External Links

NA

PowerCLI: Connect-VIServer Certificate Errors

KB ID 0001603

Problem

When attempting to connect to a vCenter or ESXi host, you see the following error;

 

[box]

Connect-VIServer : {Date} {Time}  Connect-VIServer Error: Invalid server certificate. Use Set-PowerCLIConfiguration 
to set the value for the InvalidCertificateAction option to Prompt if you'd like to connect once or to add a 
permanent exception for this server.
Additional Information: Could not establish trust relationship for the SSL/TLS secure channel with authority
'{Server-Name}'.
At line:1 char:1
+ Connect-VIServer
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [Connect-VIServer], ViSecurityNegotiationException
    + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_CertificateError,VMware.VimAutomation.ViCore.
   Cmdlets.Commands.ConnectVIServer

[/box]

Solution

Well you can either ‘Sort out your certificates properly, or ‘Drag down the self signed certificate and install it‘, or simply execute the following command;

[box]

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

[/box]

Try again.

Related Articles, References, Credits, or External Links

NA

PowerCLI: vMotion Multiple VMs

KB ID 0001585

Problem

vMotioning one VM is pretty simple to do;

[box]

Get-VM VM1 | Move-VM -Destination (Get-VMHost ESX-02.pnl.com)

[/box]

And moving ALL the VMs on one host, to another is pretty straight forward also;

[box]

Get-VMHost ESX-01.pnl.com | Get-VM | Move-VM -Destination (Get-VMHost ESX-02.pnl.com)

[/box]

But what if you have a list of VMs you want to move, and you are too lazy, (sorry too skilled), do do them manually or one at a time?

Solution

Firstly get your VMs into a CSV file, in this example I’m only going to use two, drop their names into a column headed ‘Name‘.

Then use the following PowerCLI;

[box]

Import-Csv C:\Temp\VMs.csv | Foreach {Get-VM $_.Name | Move-VM -Destination (Get-VMHost ESX-02.pnl.com)}

[/box]

Then have a coffee, or stare out of the window while your work is done for you 🙂

 

Related Articles, References, Credits, or External Links

NA

vSphere: Migrating vSwitch PortGroups and VLANS

KB ID 0001578

Problem

Over the past few weeks I’ve been doing some work for a client that involves me adding some new ESX hosts. These will be setup the same as their existing ESX hosts. That’s fine, but they use standard vSwitches and have A LOT of port groups and VLANs.

I could sit and create them all manually, and tag them onto the the correct VLANs, but something that’s boring and repetitive is better solved with a bit of scripting!

Pre Requisites: You will need VMware PowerCLI installing (Install-Module -Name VMware.PowerCLI)

Solution

Connect to your vCenter

[box]

Connect-VIServer server-name.domain-name.com

[/box]

Note: If it complains about certificates (see below) issue this command ‘Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false‘ then try to connect again

Connect-VIServer Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Prompt if you’d like to connect once or to add a permanent exception for this server.

Note2: If migrating between hosts on different vCenters, then Connect-VIServer to BOTH vCenters before proceeding.

Once connected use the following two commands to specify your existing SOURCE ESX server name, and your new TARGET ESX server names, change yours accordingly.

[box]

$sourcehost = get-vmhost -name existing-esx.domain-name.com
$targethost = get-vmhost -name new-esx.domain-name.com

[/box]

Now, to copy and create all the port groups and VLANs use the following command. Note: Change the vSwitch numbers accordingly to match the source and destination vSwitch you are copying from and to.

[box]

$sourcehost | get-virtualswitch -name vSwitch0 | get-virtualportgroup | foreach { $newportgroup = $targethost | get-virtualswitch -name vSwitch0 | new-virtualportgroup -name $_.Name -VLanID $_.VLanID }

[/box]

You can observe them getting created in the vSphere web client like so;

Note: If you are migrating ‘vanilla’ Port Groups, you may see a couple of errors fly past if it tries to create ‘Management Network‘ and ‘VM Network‘ as these exist by default, so it can’t recreate them.

Related Articles, References, Credits, or External Links

NA

VMware: Export a VM to OVA With PowerCLI

KB ID 0001507

Problem

It’s pretty easy to create an OVA/OVF from the vCenter Web console, but what about from Powershell / PowerCLI? Below I run though converting a 2008 x32 Windows server to OVA.

Solution

I’ll leave the web console in the background so you can see whats happening. From PowerCLI the first task is to connect to the vCenter.

[box]Connect-VIServer {vCenter-FQDN}[/box]

Supply a username and password.

Now remove any snapshots from the VM;

[box]Get-Snapshot Test-VM | Remove-Snapshot -confirm:$false[/box]

The VM needs to be off before we can export it, the following command will shut it down gracefully;

[box]Get-VM -Name Test-VM | Shutdown-VMGuest -confirm:$false[/box]

If your VM has an ISO connected to it, it can have an annoying habit of adding that to the OVA file! So remove any presented .iso files with the following command;

[box]Get-VM -Name Test-VM | Get-CDDrive | Set-CDDrive -NoMedia -confirm:$false[/box]

Finally we export our VM;

[box]Get-VM -Name Test-VM | Export-VApp -Destination ‘E:\Exported‘ -Format OVA[/box]

Related Articles, References, Credits, or External Links

NA