Tag Archives: PowerShell

Examine VMware CPU Ready Times with Powershell

When your (VMware) consolidation ratios are becoming high, it might be smart to keep an eye on your vm’s CPU Ready Times. Unfortunately, by default, the VI Client will only show realtime ready time statistics. Plus you’d have to look at each vm individually. Thank God VMware for the PowerCLI! Read this document for more information on how to interpret the results.

Continue reading

»crosslinked«

Managing Scheduled Tasks Remotely Using Powershell

The following Powershell functions allow you to manage querying, creating and removing scheduled tasks on one or more computers remotely.
The functions use schtasks.exe, which is included in Windows. Unlike the Win32_ScheduledJob WMI class, the schtasks.exe commandline tool will show manually created tasks, as well as script-created ones. The examples show some, but not all parameters in action. I think the parameter names are descriptive enough to figure it out, really. If not, take a look at schtasks.exe /?. One tip: try piping a list of computer names to foreach-object and into this function.

Continue reading

Increment PowerGUI Xml Version with Powershell

If you are using PowerGUI (which you should) and some of your collaegues do too, you might want to use a central configuration. Whenever you want to update the central configuration xml file, you need to increment the version number in order to push this change out to your collaegues. The following function increments the version number for you and even allows you to store the new file to the central location at the same time. All without having to edit the complex xml file manually. It even saves a backup copy of your central config file in case you mess up ;) It assumes you use a simple x.y version number, so please start out with 2.0 when setting up your config.
To update your central config: just make the modifications within PowerGUI and then run this function.

Continue reading

WSUS Cleanup with Powershell

If you manage a Windows Server Update Services (WSUS) server, you probably run the Server Cleanup Wizard every once and a while. It removes old and superseded updates and computers that haven’t reported their status for more than 30 days. Wouldn’t it be nice to schedule such a cleanup to run every month? Too bad there’s no command line tool I know of that can help you out with this. Powershell to the rescue!
Powershell can not only run the built-in commandlets or even those added by snapins. It can leverage the full power of the .NET Framework. Browse the MSDN Library if you want to find more cool things you can do with it. Here’s a script that uses this information to run the cleanup wizard:

Continue reading

Translate Vml to LUN Path with Powershell

While checking the vmkernel logs on our VMware ESX Servers today, I ran into some errors referencing luns using a vml string. It looks something like this: vml.827149017315617. I would like to know what lun this error is referencing, but I prefer the LUN Path notation, e.g.: vmhba1:2:137. So I wrote this Powershell VI Toolkit function that can translate the vml into the lun path:

# Function: Translate a VML (e.g.: vml.9364839746917650) to a Lun Path (e.g.: vmhba1:2:137)
 
function Translate-VmlToLunPath
	{
	param(
	[string]$VMHostName,
	[string]$Vml
	)
	Return (Get-VMHost $VMhostName | Get-ScsiLun | Where {$_.ConsoleDeviceName -match $Vml}).CanonicalName
	}

Feed it a host name and a vml string and it will return the lun path. Here’s an example script that uses this function when looking for LUNs with SCSI Reservation Errors:

# Example use in a script: Get LUNs with SCSI Reservation Conflicts
 
$VIServerName = "myVIServer"
$NumLines = 1000
 
$VC = Connect-VIServer $VIServerName
 
ForEach ($VMHost in Get-VMHost)
	{
	ForEach ($Log in ($VMHost | Get-Log -Key vmkernel -NumLines $NumLines))
		{
		$MatchedEntries = $Log.Entries | Where {$_ -match "reservation" -and $_ -match "vml.\d*"}
		ForEach ($VmlId in $matches.values)
			{
			$myObj = "" | Select VMHost, ErrorLun
			$myObj.VMHost = $VMHost.Name
			$myObj.ErrorLun = Translate-VmlToLunPath -VMHostName $VMHost.Name -Vml $VmlId
			Return $myObj
			}
		}
	}
 
Disconnect-VIServer -Confirm:$False

Enjoy!
Hugo

Regular Expression Magic in Powershell

I have been looking at regular expressions in Windows Powershell recently. Although it seems very complex, the power of regular expressions (regex) is worth the effort! Think about server naming conventions for example. It’s easy for a human to recognise a server name that begins with a location code and contains a status (Development, Test, Acceptance or Production) amongst other things. RegEx allows you to learn Powershell to recognise the same things.

The attached script contains a function that finds matches for a regular expresion and converts the named matches to object properties. Sounds complex? Take a look at this example for server names:

RegEx

If you can create the regular expression, this function does the matching and objectizing. It requires no modification at all for working with regular expressions that match other kinds of things, because you name the matching groups inside the regex definition. Cool stuff!

Obj-RegEx (Rename to .ps1)

Enjoy!

Hugo