Author Archives: Alan

About Alan

Alan Renouf has a role of Automation Frameworks Product Manager at VMware responsible for providing the architects and operators of the cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/vipowershell and has a personal blog at https://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

Who deleted my VM ?

Today I had a colleague come to me and say someone had deleted his VM, he didn’t know when and thinks it may have been a couple of months ago, he didn’t know which host or which datastore it was in. could I tell him when and who. Hmmmm, time to start trawling through the logs I thought.

A quick Twit from @stahler told me that this would be in the Virtual Center Database so, here is a quick powershell which I used to find the culprit…

# Fill in the following information:
$SqlServer = "MYDBSERVER";
$SqlDB = "VMwareDataBase";
$MYVM = "TESTSERVER1"
$TypeofEvent = "vim.event.VmRemovedEvent"
# The vim.event.VmRemovedEvent is a Removed action from VC you can also use :
# vim.event.VmGuestShutdownEvent
# vim.event.VmPoweredOffEvent
# vim.event.VmConnectedEvent

Function Read-VIDB ($SqlQuery)
{
# Setup SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SqlServer; Database = $SqlDB; Integrated Security = True"

# Setup SQL Command
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

# Setup .NET SQLAdapter to execute and fill .NET Dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet

#Execute and Get Row Count
$nRecs = $SqlAdapter.Fill($DataSet)

if ($nRecs -gt 0)
{
# Do Stuff
$dataSet.Tables | Select-Object -Expand Rows
}
}

$SqlQuery = "SELECT CREATE_TIME, USERNAME, VM_NAME, HOST_NAME, EVENT_TYPE FROM VMWareDS.VPX_EVENT WHERE (VM_NAME = N'$MYVM') AND (EVENT_TYPE = '$TypeofEvent')"
$MyResults = Read-VIDB $SqlQuery
$MyResults

Getting Windows SharePoint Services and Microsoft Search Server 2008 Express to index PowerShell script (.PS1) files

A guest blog apperance by Nigel Boulton

As a scripter of more years than I care to think about, I’ve lost count of the number of times that either somebody has asked me how to do something specific in a particular scripting language, or I need to remember for a new script that I’m writing.

In most cases, I know I’ve done it before, but I never seem to be able to remember in which script I did it! For this reason, I thought it would be useful to build a fully searchable script repository that I could access from wherever I happened to be at the time.

I did this using Windows SharePoint Services 3.0 and Microsoft Search Server 2008 Express. I’m not going to go into details here of how to do this, as it’s fairly straightforward stuff – basically a standard install of these products, with separate document libraries to hold the scripts and programs I’ve written in the past in various languages.

The document libraries are held in a SharePoint site and indexed by Search Server. Instead, this post is about the part that I struggled with – and didn’t manage to find a working solution for online – namely how to get Search Server to correctly index PowerShell script files.

My crawl logs were reporting the following warning for every .ps1 file I’d uploaded to the document library: ‘Crawled (The filtering process could not process this item. This might be because you do not have the latest file filter for this type of item. Install the corresponding filter and retry your crawl.)’, and naturally, no search results were being returned for text in .ps1 files.

I struggled to find a way round this for a while, until Alan and Hal kindly put my dilemma out to the community. Oisin experimented and determined how to do it on his MOSS 2007 system, and although his settings didn’t work directly on my system (most likely because of the differences between the products), they set me on the right track to working out some that did (thanks again Oisin). So this blog post is in the hope that someone else in the community might benefit from this experience. Here are the necessary steps:

  1. In Search Server Admin, go to File types – New File Type and add “ps1” (no leading dot)
  2. In Regedit, create a new key called “.ps1” under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office Server\12.0\Search\Setup\ContentIndexCommon\Filters\Extension and set it’s (Default) value to {4A3DD7AB-0A6B-43B0-8A90-0D8B0CC36AAB} (the GUID is the same as the one for the .txt extension in same parent key)
  3. Run net stop osearch
  4. Run net start osearch
  5. Start a full crawl on the Content Source
  6. Check the Crawl log

The above procedure can be applied for other file types that Search Server doesn’t index by default (and are essentially plain text files that the text filter can understand), although bear in mind that certain file types are blocked by default and it will be necessary to go into Central Admin – Operations (under Security Configuration) – Blocked file types, and then remove the desired file type from the list.

Once this was working, I thought it would be nice to add the finishing touch by giving my PowerShell scripts the proper icon within SharePoint, instead of the boring default blank white document. A bit of online research turned up a procedure for adding indexing of .pdf files, which I adapted to provide an icon for .ps1 files (this is based on http://support.microsoft.com/kb/837849). To do this:

  1. Download and save a 17×17 PowerShell script icon gif file (I didn’t have one to hand but Google Image Search did the trick nicely), and save it as:
    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\Images\icps1.gif
  2. Edit the “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\Xml\DocIcon.xml” file and insert the following line in the <ByExtension> section, in the appropriate place alphabetically for PS1.

    <Mapping Key=”ps1″ Value=”icps1.gif” OpenControl=””/>

  3. Run iisreset /noforce

One final tip – the versioning feature of SharePoint is very useful for maintaining a history of changes made to scripts, but only the current version (and it’s version notes) are indexed, so if I find myself needing to remove any code that I would still like to be able to search for in future, I tend to save the existing script out, then upload it with a different (but descriptive) file name, before replacing the version stored in SharePoint with the modified script.

Extracting Cisco Discovery Protocol Info

Inspired by Gabe over at GabesVirtual World (Check out his site he has some great posts) I have added the Cisco Discovery Protocol info, its not the nicest looking output but the info is there !

Extracts all info for each ESX host into a text file, don’t forget to change the VI server you are connecting to and the path on lines 1 and 2 of the following script:

Connect-VIServer MYVISERVER
$filename = "c:\VirtualNetworkConfig.txt"
$vmhosts = Get-VMHost | Where-Object {$_.State -eq "Connected"} | Get-View
Write-output (Get-Date) | out-file -encoding ASCII -filepath $Filename
foreach ($vmhost in $vmhosts)
{
$networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
Write-output $vmhost.Name | out-file -Append -encoding ASCII -filepath $Filename
foreach($netSys in $networkSystem)
{
foreach($pnic in $netSys.NetworkConfig.Pnic)
{
$subnets = $netSys.QueryNetworkHint($pnic.Device)
foreach($pnicHint in $subnets)
{
Write-Output $pnicHint.Device | out-file -Append -encoding ASCII -filepath $Filename
foreach($pnicIpHint in $pnicHint.Subnet)
{
$info = $hint.connectedSwitchPort | Select *
$moreinfo = $hint.connectedSwitchPort.deviceCapability
Write-Output $info | Select * | out-file -Append -encoding ASCII -filepath $Filename
Write-Output $moreinfo | Select * | out-file -Append -encoding ASCII -filepath $Filename
Write-Output "Observed IPRanges: " $pnicIpHint.IpSubnet | out-file -Append -encoding ASCII -filepath $Filename
}
}
}
}
}

Managing VMWare Infrastructure with PowerShell

So you have used VMWare Virtual Infrastructure 3, you know how powerful it is and how great the features are within the VI client but what happens when you want to get more details or you want to automate some features… You wait for the next version and hope they have included them in there – WRONG !

You wait till early next year and you buy “Managing VMWare Infrastructure with PowerShell“ written by Hal Rottenberg and published by SAPIEN Press.

I have just finished technically reviewing this great book, if you haven’t bothered learning PowerShell yet (why not) and you haven’t looked at the VI Toolkit then please do yourself a favour and buy this book when it is released.

Not only does it give you an amazing insight into the way things work below the GUI it also teaches you the basics of PowerShell and how to use this technology to manage the VMware feature set we have come to know and love.

Hal’s unique writing style will take you through learning how to perform many extensive tasks which would normally take hours, sometimes days through the client and show you how to perform these actions within minutes or sometimes seconds. He even adds a little of his own sense of humour to lighten some of the more mundane tasks.

Learn how to deploy multiple virtual machines in minutes, report on the extensive areas of virtual infrastructure and much much more.

Believe me, this is one book I would recommend for any VI Admin to read and store net to your management workstation.

If you are a VI admin reading this then stop doing things the long way and get a bit of Posh in your life.

Table of Contents
Introduction – Why You Need This Book
  1. Windows PowerShell Crash Course
  2. Getting Started
  3. Scripting with Virtual Infrastructure
  4. Understanding Your Verbs and Nouns
  5. Basic Cmdlets
  6. Deploying Your Virtual Infrastructure
  7. Configuring Your Environment
  8. Reporting and Discovery
  9. Keeping Things Running (Maintenance)
  10. When Things Break (Troubleshooting)
  11. Management and Automation Case Studies
Appendices
Other Resources

Observed IP Ranges

Recently I needed to check a whole bunch of servers to see if we had a particular VLAN tagged on any of the ESX Hosts, after a quick post on the VI Toolkit forum and a little messing, the following script tells me quickly which observed IP ranges are on each of the nics in each of the hosts…

Connect-VIServer MYVISERVER
$vmhosts = Get-VMHost | Get-View
foreach ($vmhost in $vmhosts)
{
$networkSystem = Get-view $vmhost.ConfigManager.NetworkSystem
Write-Host $vmhost.Name -foregroundcolor yellow
foreach($netSys in $networkSystem)
{
foreach($pnic in $netSys.NetworkConfig.Pnic)
{
$subnets = $netSys.QueryNetworkHint($pnic.Device)
foreach($pnicHint in $subnets)
{
Write-Host $pnicHint.Device
foreach($pnicIpHint in $pnicHint.Subnet)
{
Write-Host "`t" $pnicIpHint.IpSubnet
}
}
}
}
}

Get-Scripting now !

If you still want to learn powershell but haven’t got around to buying a book yet, why not win one….

In Episode 5 of the Get-Scripting Podcast we announced details of a competition to fill in some of the speech bubbles above the heads of community members from the Teched Powershell Panel Discussion photo.

Those nice people at Quest have given us a print copy of Jeffrey Hicks’ recent book ‘Managing Active Directory With Windows Powershell’ to give away as a prize, this is a great book for learning how to utilise Powershell with AD and also a great way to get started in Powershell – they are also going to get Jeffrey to sign it too.

If you’d like to be in with a chance of winning simply send us some suggestions for what some of the panel members below might be thinking and the one we think is the best will get the book. We’ve got a few other freebies left over from Teched in the Get-Scripting prize cupboard so there should be a runners up prize too.

Send your entries to:

get [dash] scripting [at] hotmail [dot] co [dot] uk

We’ll announce the winner in Episode 6 of the podcast which should be recorded mid-December.

Citrix on VMware, 1vCPU or 2vCPU ?

Following my previous Citrix testing on VMware I thought it best to try a dual vCPU VM to see if I can get double the users of a single vCPU, sounds logically sensible to me !

So just a reminder that a single vCPU with 25 users ran at around 73% average, a nice figure to run at giving plenty of CPU for peaks and troughs.

This test of a dual vCPU was undertaken following the sucesfull test of 25 users on one VM with 1 x vCPU this was run as a baseline to see how it relates to the single vCPU VM.

Virtual Machine Specification

2 x vCPU (Using VMware process affinity to tie the VM to a specific CPU core)
3072 GB RAM (reduced from 4GB as memory was not being used)

1 x 20GB Hard Disk Stored on SAN

1 x vNic (1GB)

Users Logged on: 50

  • Logged 10 users on and working over 5 minutes
  • Logged another 10 users on and working over 5 minutes
  • Logged another 5 users on and working over 3 minutes
  • Kept 25 users logged on and working for 15 minutes
  • Logged all 25 users off over a 5 minute period

clip_image002

As you can see from the above, a 3000Mhz server runs at around 60% which is not a good start bearing in mind the single vCPU VM.

Next Test 50 Users and Dual vCPU

This test was undertaken following the sucesfull test of 25 users on one server with 1xVCPU allowing for a reasonoble amount of capacity for application usage.

Virtual Machine Specification

2 x vCPU (Using VMware process affinity to tie the VM to a specific CPU core)
3072 GB RAM

1 x 20GB Hard Disk Stored on SAN

1 x vNic (1GB)

Users Logged on: 50

  • Logged 20 users on and working over 5 minutes
  • Logged another 20 users on and working over 5 minutes
  • Logged another 10 users on and working over 3 minutes
  • Kept 50 users logged on and working for 15 minutes
  • Logged all 50 users off over a 5 minute period

clip_image002[7]

The peak CPU usage of this graph was reached before 50 users were connected and therefore proves that the best method to get more users per physical server is to scale out rather than increase CPU resources.

This will obviously make sense in some cases where the cost of Operating System, Application software and licenses and management costs may make sense to build a 2 x vCPU and get slightly more users.

 

Getting VMware Host Physical Nic Speeds

I had an issue with one of my hosts this week who’s nic had auto-negotiated to the wrong speed, following this I wrote a quick script to check the rest of my hosts.

Good job as there were a fair few nics running at 100MB rather than 1GB causing a few issues.

The following script will list each host and there nics and speeds….

Connect-VIServer MYVISERVER

$VMhosts = Get-VMHost | Get-View
Foreach ($vmhost in $vmhosts){
	Write-Output $vmhost.Name
	$pnic = 0
	Do {    
		$Speed = $VMhost.Config.Network.Pnic[$pnic].LinkSpeed.SpeedMb    
		Write "Pnic$pnic $Speed"    
		$pnic ++
	} Until ($pnic -eq ($VMhost.Config.Network.Pnic.Length))}

Unable to format a LUN

The SAN team issued a new LUN to my hosts and I was unable to format it as I recieved the following error: “Unable to read partition information” on LUN 180 on vmhba1.

Running fdisk from the service console cleared this up, heres my output :

[root@myhost]# fdisk /vmfs/devices/disks/vmhba1\:0\:180\:0

Command (m for help): p

Disk /vmfs/devices/disks/vmhba1:0:180:0 (Sun disk label): 15 heads, 64 sectors, 24686 cylinders
Units = cylinders of 960 * 512 bytes

Device Flag Start End Blocks Id System
/vmfs/devices/disks/vmhba1:0:180:0p3 u 0 24686 11849280 5 Whole disk
/vmfs/devices/disks/vmhba1:0:180:0p4 u 0 2 960 f Unknown
/vmfs/devices/disks/vmhba1:0:180:0p5 u 2 24686 11848320 e Unknown

Command (m for help): o
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.

The number of cylinders for this disk is set to 24686.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

After this I was able to use the LUN as normal, really put here for my reference in the future 🙂

Checking CPU Affinity

I needed to double check the CPU Affinity settings I had set on some VM’s which I wanted tied to the cores in my host, just to double check I had these correct the following one liner works a treat….

Get-Cluster “My Cluster” | Get-VM | Get-View | foreach {Write-output $_.Name $_.Config.CpuAffinity.AffinitySet}