Year: 2011

10 Days of Microsoft SQL Server, Silverlight and SharePoint Books – PacktPub

Packt Publishers are celebrating the publication of new Microsoft books and are offering an exclusive discount for all Microsoft (SQL Server, SharePoint, Silverlight) books during September 2011. There are several books of interest and I am sure you will find a huge set of helpful ones in that list. Go and grab the discount! Here is the link which explains it in detail: 10 Days of Microsoft SQL Server, Silverlight and SharePoint Books. The campaign for the SQL Server books will be for the first 10 days of the month.

Monitoring Volume Change Events in PowerShell using WMI

While I was preparing a few demo scripts for a Bangalore IT Pro UG meet session, I tumbled upon on WMI event class Win32_VolumeChangeEvent. This one is interesting. It is derived from Win32_DeviceChangeEvent class and gives us the ability to monitor local drive events directly.For example, you can get a notification when a local drive or mount point gets removed or added. The following table shows a list of event types we can monitor. Note This class may not be there on Windows XP. I have not verified this fact. Value Meaning 1 Configuration Changed 2 Device Arrival 3 Device Removal 4 Docking Let us see a few examples: Adding a new local drive We can monitor a local drive addition using the following query:

Using this, you can monitor removable drives such as external hard drives and flash drives.  Removal of a local drive To monitor the removal of local drive events, we can use the following query:

Registering for the above events

Once we have these event registrations done, we …

PoshUtils: Get-DBSize – Get SQL Database space usage

Here is an improved version of the script I am using to get the database space usage details. This is one of the utility scripts I have been using and will be publishing soon as PoshUtils on Codeplex. For this script to work, you need the SQL Management Object (SMO) assemblies. These assemblies are available on systems with SQL server installed or where SQL management tools are installed.

Here are some examples:

This gets the database space usage details for all databases on a SQL server named Server01.

The above example gets the space usage details for TestDB1 and TestDB2 on  a SQL Server named Server02.

This last example get the space usage details for TestDB1 and TestDB2 on the local SQL server. And, finally, you can avoid all parameters to get space usage for all databases on the local SQL Server. Here is how the output looks:

A simple PowerShell script explorer addon using ShowUI

Update: The select-ViaUI function is no more in ShowUI module starting version 1.2. So, if you plan to run this, you need to go back to ShowUI 1.1. Okay, there is nothing really fancy here. This is all done in just ten lines of PowerShell code. In fact, only 3 lines for the real logic. I am not even creating my own UI to do this. This is all done using the Select-ViaUI cmdlet in ShowUI module. Here is how it looks: So, within this addon, I list all ps1, psm1, and xml files at a pre-defined location. At the bottom, you see a search box. If you find it difficult to scroll to the item you want, you can just type the name ( or partial name) and filter the list. Once you select the item, just click OK to open that file(s) in ISE. Simple! Here is the code:

Just execute the above code or add the above code to your ISE profile and restart ISE. Now, when you press Ctrl+Alt+E, this …

PowerShell ISE addon menu generator – The ShowUI way

Update: @Jaykul was kind enough to review this code and post an updated (and better) version of this addon with guidelines & best practices. Don’t forget to read the first few lines of text on that page to learn the best practices when scripting WPF UI. A while ago, I wrote about a PowerShell ISE addon that can be used to add items to ISE and load them dynamically by adding the same to ISE profile. This used WinForms to generate the GUI required for the addon functionality. As you may know already, ShowUI 1.1 is available for creating WPF UI in PowerShell. So, I decided to re-write this addon to use ShowUI. Here is the new code:

The WinForms implementation of this addon had 235 lines of PowerShell code. Where as, ShowUI version has only 48 lines! This shows that the value per line of PowerShell code is amazing when using ShowUI. ShowUI really makes it easy. Thanks to James Brundage and Joel Bennett. Now, to use this addon: Copy the contents of above script …