June 2010

PowerCLI: Reconnect VMhosts after changing vCenter certificates

If you have ever changed the vCenter server certificates, you’ve experienced having all your hosts disconnected from vCenter.  I couldn’t imagine reconnecting them one at a time… You could do this all natively in PowerCLI, but that would require you to fully remove and then add the hosts.  That is very inconvenient, and almost as much trouble as doing it by hand… In this case it is both faster and easier to just use the native vSphere API.

# Get the hostsystem object for every host currently disconnected.
$VMhosts = Get-View -ViewType 'Hostsystem' `
                    -Property 'name' `
                    -Filter @{"Runtime.ConnectionState"="disconnected"}            

Foreach ($VMhost in $VMhosts)
{
    # Create a reconnect spec
    $HostConnectSpec = New-Object VMware.Vim.HostConnectSpec
    $HostConnectSpec.hostName = $VMhost.name
    $HostConnectSpec.userName = 'root'
    $HostConnectSpec.password = 'PassWord'            

    # Reconnect the host
    $taskMoRef = $VMhost.ReconnectHost_Task($HostConnectSpec)            

    # optional, but i like to return a task object, that way I can 
    # easily integrate this into a pipeline later if need be.
    Get-VIObjectByVIView -MORef $taskMoRef
}

~Glenn

HOW TO
PowerCLI
Powershell
Scripting
vCenter
VMware

Comments (2)

Permalink

PowerCLI: Configure iSCSI one-liner

While migrating a small environment to vSphere today I ran into my nemesis Host Profiles again. When are they going to Fix these things? The fact that they are incapable of even rudimentary iSCSI configuration is embarrassing. I’m sure vmware will fix it, but until then I wrote a simple one-liner that will configure iSCSI on a new host.

$VMhost = Get-VMhost 'ESX01'
$ChapUserName = 'vmware'
$ChapPassword = 'password'
$SendTargets = '192.168.1.1'            

# Enable the software ISCSI adapter if not already enabled.
$VMHostStorage = Get-VMHostStorage -VMHost $VMhost | Set-VMHostStorage -SoftwareIScsiEnabled $True            

#sleep while iSCSI starts up
Start-Sleep -Seconds 30            

# By default vSphere will set the Target Node name to iqn.1998-01.com.vmware:<HostName>-<random number> 
# This script will remove everything after the hostname, set Chap auth, and add a send Target.
#
# Example iqn.1998-01.com.vmware:esx01-165435 would become iqn.1998-01.com.vmware:esx01
$VMHostHba = Get-VMHostHba -VMHost $VMHost -Type IScsi |
    Where-object { $_.IScsiName -match "(?<IQN>iqn.1998-01.com.vmware\:[^-]+)"} |
    Set-VMHostHba -IScsiName $Matches.IQN |
    Set-VMHostHba -ChapName $ChapUserName -ChapPassword $ChapPassword -ChapType "Required" |
    New-IScsiHbaTarget -Address $SendTargets -Port "3260"                

#restart the host to make sure everything took
Restart-VMHost -VMHost $VMHost -Confirm:$false | out-null

~Glenn

ESX
ESXi
PowerCLI
VMware

Comments (1)

Permalink

Monitoring for orphaned snapshots left by SMVI

NetApp’s SnapManager for Virtual Infrastructure (SMVI) is a great product, but it’s messy. If it encounters the any error, it seemingly forgets to delete the virtual machine snapshots from the Virtual Infrastructure before dying.

To prevent many orphans (I’ve seen as many as 20 on a single virtual machine) from happening, I created a quick Nagios check that simply alerts when it sees them.

This script is very elementary. It very simply uses a regex to check for any snapshots that match the default SMVI naming convention. For each one it finds, a counter is incremented. If any are found, the script returns an error to Nagios, which causes an alert to be sent.

#!/usr/bin/perl -w
#
# check_vi_smvi_snapshots.pl - written by Andrew Sullivan, 2010-06-16
#
# Please report bugs and request improvements at http://get-admin.com/blog/?p=1059
#
# A simple script to look for snapshots that match the name pattern that smvi uses.
# We are merely pulling a list of all snapshots, searching for the string "smvi" in 
# the name, if it's found, we return a warning condition.  This could lead to a 
# "false" positive if it runs while a snapshot series is still ongoing, but since
# the smvi snaps should be very short lived the condidition will not last unless
# the snap is left.
#
# Example:
#   ./check_vi_smvi_snapshots.pl --server your.esx.host --username you --password secret
#
 
use strict;
use warnings;
 
use FindBin;
use lib "$FindBin::Bin/../";
 
use VMware::VIRuntime;
 
# substitute the location of your nagios perl library
use lib "/usr/lib64/nagios/plugins";
use utils qw(%ERRORS);
 
Opts::parse();
Opts::validate();
 
Util::connect();
 
main();
 
Util::disconnect();
 
sub main {
 
	# the number of smvi snapshots
	my $smviSnaps = 0;
 
	# for setting the type of exit we want
	my $exitCondition = "";
 
	# we need MORs for each of the VMs on the host
	my $VMs = Vim::find_entity_views( view_type => 'VirtualMachine' );
 
	foreach my $vm (@$VMs) {
		if ($vm->snapshot) {			
			foreach my $childSnapshot (@{$vm->snapshot->snapshotInfo->rootSnapshotList}) {
				$smviSnaps += getSnaps($childSnapshot);
			}
 
		} else {
			#print $vm->name . " has no snapshots\n";
		}
	}
 
	if ($smviSnaps > 0) {
		print "WARNING - " . $smviSnaps . " SMVI snapshots exist.\n";
		$exitCondition = "WARNING";
 
	} else {
		print "OK - No SMVI snapshots exist.\n";
		$exitCondition = "OK";
 
	}
 
	Util::disconnect();
	exit $ERRORS{ $exitCondition };
}
 
sub getSnaps {
	my ($snapshotTree) = @_;
	my $snapcount = 0;
 
	# uncomment for debugging
	#print "Found snap: " . $snapshotTree->{name} . "\n";
 
	if ( $snapshotTree->{name} =~ /smvi/ ) {
		$snapcount++;
	}
 
	if ($snapshotTree->childSnapshotList) {
		foreach my $childSnapshot (@{$snapshotTree->childSnapshotList}) {
			$snapcount += getSnaps($childSnapshot);
		}
	}
 
	return $snapcount;
}

I’ve set the check to execute once an hour in my environment, as I don’t feel that granularity finer than that is needed…an hour’s worth of change is ok for an SMVI snapshot for me.

Nagios
NetApp
Perl
Scripting
Virtulization

Comments (5)

Permalink

Color me astonished!!

I’ve been out of touch for most of this week, having only been able to be connected for an extended period of time today, and from somewhere out in left field I received an extremely surprising email from Mr. John Troyer…

I have been named a 2010 vExpert! Words can not describe how honored I am to receive this designation, I feel truly humbled by the others that have received the award and I can only hope that when I grow up I can be like them.

Thank you again to John Troyer and his team for this privilege!

Andrew

Virtulization

Comments (0)

Permalink

Winner, winner, chicken dinner

I WON!  If you haven’t heard, I won the big prize for the Scripting Games this year.  First place landed me a ticket to TechEd North America! I had a hell of a time getting the time off work, and getting my travel authorized, but it all worked out… Below is my agenda for the conference, feel free to contact me via twitter… with the short lead time I have Nothing scheduled (Feels kind of weird to just be going to a show…)

I should be near anything that says PowerShell in between sessions.

Monday, June 7

9:00 AM – 10:30 AM
KEY01 Tech·Ed North America Keynote Presentation

1:00 PM – 2:15 PM
WSV07-INT New Remote Management Technologies in Windows Server 2008 R2

2:45 PM – 4:00 PM
WSV334 Windows Server 2008 R2: Tips on Automating and Managing the Breadth of Your IT Environment

4:30 PM- 5:45 PM
MGT308 Microsoft System Center Configuration Manager v.Next: Overview

Tuesday, June 8

8:00 AM – 9:15 AM
WCL304 Best Practices Guide to Managing Applications

9:45 AM – 11:15 AM
KEY02 Business Intelligence Conference Keynote Presentation

1:30 PM – 2:45 PM
WCL313 Paradigm Shift: Microsoft Visual Basic Scripting Edition to Windows PowerShell

3:15 PM- 4:30 PM
MGT309 Microsoft System Center Configuration Manager v.Next: Software Distribution

5:00 PM – 6:15 PM
DAT203 Managing Microsoft SQL Server: For the "Reluctant" DBA

6:15 PM
WCL318 Using Windows Preinstallation Environment (PE) 3.0 to Troubleshoot and Fix Problems, and to Capture and Deploy WIM Images

Wednesday, June 9

8:00 AM – 9:15 AM
SIA333-R Useful Hacker Techniques: Which Part of Hackers’ Knowledge Will Help You in Efficient IT Administration? (repeated from 6/8 at 3:15pm)

9:45 AM – 11:00 AM
WSV319 Manage Your Enterprise from a Single Seat: Windows PowerShell Remoting

11:45 AM – 1:00 PM
WSV301 Administrators’ Idol: Windows and Active Directory Best Practices

1:30 PM – 2:45 PM
WSV313 Failover Clustering Deployment Success

3:15 PM – 4:30 PM
DAT407 Windows Server 2008 R2 and Microsoft SQL Server 2008: Failover Clustering Implementations

5:00 PM – 6:15 PM
MGT306 Microsoft System Center Configuration Manager v.Next: Hierarchy Design

Thursday, June 10

8:00 AM – 9:15 AM
SIA334 The Secrets of Effective Technical Talks: How to Explain Tech without Tucking Them In!

9:45 AM -11:00 AM
WCL06-INT Using Windows PowerShell for Enterprise Desktop Automation

1:30 PM – 2:45 PM
WCL314 Windows Sysinternals Primer: Process Explorer, Process Monitor, and More

3:15 PM – 4:30 PM
WSV401 Advanced Automation Using Windows PowerShell 2.0

5:00 PM – 6:15 PM
WCL315 The Case of the Unexplained, 2010: Troubleshooting with Mark Russinovich

6:30 PM – 11:00 PM
TechEd Party

See you all there! I’m the chubby white guy with a beard.

~Glenn

TechEd

Comments (2)

Permalink