Category Archives: PowerShell

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.

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))}

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}

PowerGui 1.5.3 Released

Wahoo, I have been waiting for this one, I have tried a few editors but always go back to PowerGui.

Hopefully this will fix the issue I was having with code over a certain amount of lines and also some sloweness I have been getting expanding variables in debug mode.

A few other little problems too.

If you havent tried it yet, download it for free now….

http://powergui.org/downloads.jspa

Last Call: UK PowerShell Use Group

UK Powershell User Group

Following on from the last great meeting the next UK Powershell User Group has been announced…

The November meeting of the User Group will be on Thursday 20th November

Time
6.30 – 9.30

Location
Memphis Room
Building 3
Microsoft campus
Thames Valley Park
Reading

Agenda

  • Powershell Introduction: Objects and get-member
  • PowerGUI SQL Server Reporting Services powerpack
  • PowerShell in SQL Server 2008
  • The usual refreshments break will occur

The PowerShell Introduction is a new feature for meetings where they will explain a particular feature of PowerShell that has caused someone a problem or maybe a slightly more difficult concept.

Please visit the following site to confirm your attendance, please do not just turn up as ou will need to be regiestered….

http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1872.entry