Powershell – Measure Historical Command

Have you ever been working in a Powershell console and have a command run longer than expected? Did you wish that you had wrapped that command in a Measure-Command so that you could view execution time? This has happened to me several times, so recently I started working on a Measure-HistoricalCommand function that would use Get-History to create a New-TimeSpan from the StartExecutionTime and EndExecutionTime properties. I would like to say thanks to @mtboren from vnugglets.com who helped add some functionality and speed up this function.

[cc lang=”powershell”]
Function Measure-HistoricalCommand {
param(
## for getting info about the last X commands
[parameter(Mandatory=$false,ParameterSetName=”ByCommandCount”)][alias(“Count”)][Int]$Last=1,
## for getting info about a specific command ID
[parameter(Mandatory=$true,ParameterSetName=”ByCommandId”)][alias(“Id”)][Int]$CommandID
) ## end param

process {
## create the Get-History command expression to invoke; use -Id or -Count based on the params passed to this function
$strGetHistoryExpr = “Get-History ” + $(if ($CommandID) {“-Id $CommandID”} else {“-Count $Last”})
Invoke-Expression $strGetHistoryExpr | %{
$ts = New-TimeSpan -Start $_.StartExecutionTime -End $_.EndExecutionTime
## again, take out the array, just return the object straight away
#$myResults += New-Object PSObject -Property @{
New-Object -TypeName PSObject -Property @{
ID = $_.ID
CommandLine = $_.CommandLIne
EndExectuionTime = $_.EndExecutionTime
StartExecutionTime = $_.StartExecutionTime
TimeSpan = $ts
TotalSec = $ts.TotalSeconds
} ## end new-psobject
} ## end History foreach-object
} ## end process
} ## end fn
[/cc]

Posted in Scripting | Leave a comment

VMware SRM and SSL certificates

I recently ran into some problems with SRM and SSL certificates. My lab for this project has two vcenters — both using CA signed SSL certificates. During the SRM install I used the automatically generated certificates. When the installations were complete, I was unable to pair the sites. The error message I received was SSL related:

The host certificate chain is not complete. reason.msg

I spent a lot of time trying to figure out the ‘trick’ on how to create certificates that SRM would actually use. I finally found a blog post here: http://thephuck.com/virtualization/creating-certificates-for-vmware-srm-or-vcenter-using-openssl-made-easy-with-video/ that pointed me in the right direction. I followed the instructions but SRM still wouldn’t use the certificate.

Looking at the certificate created, I noticed a couple of things were missing. Specifically, the following two settings that actually make the SRM certificate different:

extendedKeyUsage = serverAuth, clientAuth
subjectAltName = DNS: fqdn.of.srm.server

Even though my CSR contained both of these settings, the CA did not include them in the certificate. I decided to make a copy of the ‘Web Server’ certificate template I normally use and add a ‘Client Authentication’ purpose. However, when I got to my CA, I noticed an SCCM Web Server template that already had the options I needed.

I followed the instructions, but made two different changes when submitting the CSR to my CA:

Certificate Template: SCCM Web Server (which has a Server & Client purpose)
Attributes: san:dns=host.name.of.vcenter (which adds the subject alternative names)

Finally, after what felt like weeks of SSL hell, I was able to pair my sites. Many thanks to Luke @ThepHuck for these valuable instructions.

Posted in Virtualization | 6 Comments

SRM with vSphere Replication & the not so detailed exception

I have been working on an SRM lab for the Indianapolis VMUG Demo Day event — which is on Thursday July 26th. If you are going to be in the area you should check out this free event (please register at the following link: http://www.vmug.com/index.php?mo=cm&op=ld&fid=200).

This project has given me a lot of hands on lab time with SRM — specifically using vSphere Replication. The idea behind this lab is the planned migration from LegacyDC to VCENTER. After deploying and configuring the appliances and selecting ‘Configure VRMS Connection’ the following error appeared:

VRM Server generic error. Please check the documentation for any troubleshooting information. The detailed exception is: ‘legacydc’.

For a detailed exception that message lacks a lot of…uhmmm, well, detail. In this specific lab I did not have a DNS server and I figured it was possible that the VRMS appliance wasn’t able to resolve the name ‘legacydc’. I edited the /etc/hosts file on both appliances to include the names of each vCenter (legacydc and vcenter). The hosts file did the trick and I was able to configure VRMS connections.

Posted in Virtualization | Leave a comment

VMware View 5.1 and Windows 7

I have been working on a VMware View lab environment for the Indianapolis VMUG Demo Day event. If you are going to be in the Indianapolis Indiana area on Thursday, July 26th 2012, you should stop by and check out this event. Here is a link to registration and event details: http://www.vmug.com/index.php?mo=cm&op=ld&fid=200.

As part of this VMware View install, I found an interesting KB article worth sharing: Customization of Windows 7 or Vista View virtual machines fail with the error: Customization timed out (http://kb.vmware.com/kb/2020627).

The cause of this problem is:
By default, Windows 7 and Vista virtual machines created in vCenter use the E1000 virtual network adapter, which is not compatible with View. The VMXNET3 virtual network adapter must be used in Windows 7 and Vista virtual machines for View desktops.

I never received the ‘customization timed out’ error message, but the desktop pool did stay at the ‘customizing’ stage for over 45 minutes. The reason I found this error interesting, is that the KB article shows that the E1000 network adapter wasn’t supported in View 4.5, 4.6, 5.0 or 5.1. However, I have a View 5.0 installation with a handful of Windows 7 desktops using the E1000 network adapter. I’m not sure why this works in one environment and not in another, but just remember: use VMXNET3 for your Windows 7 desktops! It will save you hours of ‘customizing’ that never finishes.

Posted in Virtualization | Leave a comment

Improving performance with Get-MailboxFolderStatistics

In a previous post I mentioned some performance improvements I made in an Exchange script that used Get-MailboxFolderStatistics. A portion of this script selected the size (in megabytes) of the top 5 folders in each users mailbox. I made a subtle change to the code that saved about 35 minutes on the execution time of the script.

For purposes of the following examples, I measured the execution time of this block of code using my Exchange mailbox. My mailbox has about 250 folders and is about 1.4GB in size.

Previous version of code takes 0.2329 seconds to complete.
[cc lang=”powershell”]
measure-command {
# Previous
$stats = Get-MailboxFolderStatistics $mbx.identity
$folders = $stats | Select-Object folderpath,@{name=”Size(MB)”;Expression={$_.FolderAndSubfolderSize.toMB()}}
$folders = $folders | Where-Object {$_.”Size(MB)” -ne 0} | Sort-Object -Property “Size(MB)” -Descending | Select-Object -First 5
$folders
}
[/cc]

Updated version of code takes 0.1418 seconds to complete.
[cc lang=”powershell”]
measure-command {
# Updated
$stats = Get-MailboxFolderStatistics $mbx.identity
$folders = $stats | Where-Object {$_.FolderAndSubfolderSize -gt 1048576} | Sort-Object -Property “FolderAndSubfolderSize” -Descending | Select-Object -First 5
$folders = $folders | Select-Object folderpath,@{name=”Size(MB)”;Expression={$_.FolderAndSubfolderSize.toMB()}}
$folders
}
[/cc]

On my account, this is a savings of only 0.0911 seconds. That doesn’t sound like enough savings to implement, does it? Actually, if you take my mailbox example and expand it by 40,000 users it would be a savings of about 1 hour. My mailbox is likely larger than the average user, which explains why the savings is closer to 35 minutes instead of 60.

Can you notice the change in the code? It is subtle…nearly the same code/commands are being used — only the order was changed slightly.

In the previous version of the script, all of the folders were selected into an object and the ToMB method was called on the FolderAndSubfolderSize property. The next line of code selects only the first five non-zero entries.

In the updated version of the code, only the first five non-zero entries were added to the object. The next line of code then called the ToMB method on only the first five folders. In my example, this is a savings of about 245 calls on the ToMB method.

Posted in Messaging, Scripting | Leave a comment