Friday, April 20, 2012

Force 100% CPU with PowerShell

For a course we are working on, we needed an application that would force 100% CPU utilization to do a troubleshooting activity. We had an old executable, that did this, but it didn't meet current compliance standards.

So, here is the PowerShell script (one line) that will max out your CPU:
while ($true) {$n++;$n=0}
I'm not sure that the $n=0 is required, but I'm concerned that it will go past the limit of the integer variable type if it runs for an extended period of time.

Also note that this maxes out only a single CPU core because PowerShell is a single process running on a single core. In our virtual machines for courses, this is fine because each virtual machine has only a single virtual CPU.

If you need to max out multiple CPU cores then you probably need to open multiple instances of PowerShell and run the script in each one.

Thursday, April 19, 2012

Physical to Virtual for Hyper-V

One of the common things that you need to do in a virtualized environment is a physical to virtual (P2V) migration. This is the process of taking an existing physical computer and converting to a virtual machine. You often do this to move an existing server from old hardware to the virtual environment. There are no built-in tools for Hyper-V that do the conversion from a physical computer to a virtual machine.

In the past, I have used System Center Virtual Machine Manager (VMM) to perform P2V migrations. This is a good tool and an evaluation version is available for download from Microsoft ( As part of the migration process the Hyper-V integration tools are automatically installed. Storage drivers and network drivers are automatically updated. The downside to VMM is the cost and relative complexity to implement.

As an alternative to VMM for P2V migration, you can use Disk2VHD ( This free tool does a conversion of a physical machine to virtual but does not do any of the driver substitution. You need to do additional manual configuration such as installing the Hyper-V integration tools.

Saturday, April 14, 2012

Using DayOfWeek in PowerShell

I got a chain letter from my Mom recently that talked about some Feng Shui stuff and the month of July 2012 having 3 Fridays, Saturdays, and Sundays. And that this only happens every 823 years. The email called this something called money bags.



Based on the pattern, I thought it should actually be much more common than that, but wanted an easy way to see it. So, I figured, I'll use PowerShell to identify the day of the week for July 1 in various years to see which ones were Fridays. They would have the same pattern shown above.

Here is my code:

for ($year=2000;$year -le 2020;$year++){
    [datetime]$date="July 1,$year"
    Write-Host "Year: " $year " July 1 day of week: " $date.dayofweek
As suspected, this pattern seems to show up quite often. And in fact, isn't even happening in 2012. It happened in July 2011. The time previous to that was 2005. It will happen again next in 2016.

Do I have a point here? Not really. It was just fun to use PowerShell to debunk an Internet chain letter.

Monday, April 9, 2012

Considerations for Client Access Arrays

I normally prefer to post actual information rather than links. However, here are a couple of good blog postings about CAS arrays and common misconceptions. I think #5 about configuring the RPCClientAccess property of mailbox databases is particularly useful. While the property is easy to change, the client configuration is not.

Sunday, April 8, 2012

Remove Messages from an Exchange Mailbox

Every once in a while, I get a request on how to easily remove messages from Exchange mailboxes. If you have Exchange 2010, you can use the Search-Mailbox cmdlet to remove messages. This cmdlet is most commonly used when searching mailboxes for items to recover, but can also be used to remove content with the -DeleteContent parameter.

Here is an example, that searches for a message with a specific subject and removes it:
Search-Mailbox -Identity "Byron Wright" -SearchQuery 'Subject:"Insider Info"' -DeleteContent

If you want to remove this message from all mailboxes, you would do it like this:
Get-Mailbox | -SearchQuery 'Subject:"Insider Info"' -DeleteContent

The key to using the cmdlet to remove messages is making sure that your query captures only the messages you want. I strongly suggest verifying the query results first not matter how much of a hurry you are in. The easy way is to use the -LogOnly and -LogLevel parameters. You need to define the log level as Full to capture query results.
Get-Mailbox | -SearchQuery 'Subject:"Insider Info"' -LogOnly -LogLevel Full
Note: Using multi-mailbox search requires an Exchange enterprise client access license for each mailbox searched.