Posts

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.

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 ( http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17972 ). 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 ( http://technet.microsoft.com...

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.   July Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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"  ...

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. Demystifying the CAS Array Object (Part 1) Demystifying the CAS Array Object (Part 2)

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 de...

Setting Exchange Calendar Permissions

The default permissions for all users to another user's calendar is AvailabilityOnly . This permission allows you to see that a user is busy, but it does not allow you to see the subject or other information. This permission is just enough that you can use free/busy searching for meeting requests. In some cases, you might want to make the subject available. Specifically, for resource mailboxes, it may be useful. In the default configuration, meetings in the calendar of a resource mailbox contain the name of the person that organized the meeting, but not the original subject of the meeting. By allowing users to see the subject of meetings for a resource mailbox, you are allowing them to see who organized the meeting. The permission to use for showing meeting subjects is LimitedDetails . The command to set this permission on a resource mailbox named Room100 is shown below: Set-MailboxFolderPermission Room100:\Calendar -User Default -AccessRights LimitedDetails

Creating a Library of PowerShell Functions

If you are doing a large amount of PowerShell scripting then it is likely you have created functions that you want to reuse between scripts. You can copy and paste a function from one script to another to reuse it, but this creates maintenance issues. If you modify the function to improve it, then you need to update many scripts. To centralize management of functions, you can create a single PowerShell script that contains all of the functions. Then you load the script containing the functions from within other scripts before you call the functions. To load the functions script, use dot sourcing like this: . .\functions.ps1 The example above loads functions.ps1 from the same directory as the script that you ran. You can also specify a full path such as: . C:\functions.ps1 You can also use a variable as part of the path: . $ScriptPath\functions.ps1 Another alternative is to create multiple PowerShell scripts that you call as needed from within your script. Effectively, you would be trea...