Monday, July 30, 2012

Launching Visual Studio solutions made easy with PowerShell

In my previous post on “Launching Apps with PowerShell Functions”, I showed you how to launch a visual studio solution.  That was very easy I guess because we used the start command and provided full file path with a .sln extension. It started visual studio to open this file.

I am going to extend this solution to solve one of my pain points with Visual Studio Start Page.  On VS start page there is a section for “Recent Projects” which lists recently opened visual studio solutions.  In that section you can pin a solution which you like VS2010 to remember.  It is a great feature to open your most used visual studio solutions.  However, when you get into creating a lot of visual studio projects for development, testing some random code and production.  Over a time this list gets unmanageable and it won’t remember too many projects.  You don’t want to pin all these test projects and bad projects you have created because they will conflict with your frequently pinned production solutions.  Those test solutions might be good for future testing purposes.  Again you don’t want them to go away either.  So I wanted to list all the solutions that I have created in my two or three directories over time and use start command to open it.  There could be more solutions elsewhere but I am not interested in those.  So here is my solution:

I have create a script which will search two or three interested directories like C:\BadSourceCode, C:\ProductionSourceCode, C:\Testing etc for .sln file and list them.  Then I will provide a number to launch a particular visual studio solution I am interested in.  This way I don’t have to wait in the morning to wake up my visual studio from sleep and search and find the solution I want to open.  Ouch!! That is too much of work for me when I am half asleep.  Instead I just want to fire up PowerShell and supply visual studio solution number that I want to open and bang it just works while I sit back and relax.

So here is the script.  You can put the entire script in your profile or store it in a location and create powershell function to run that script.

Write-Host " " 
Write-Host "Listing all visual studio solutions" -ForegroundColor yellow
Write-Host " " 
Add-Type @'
public class solution
public int id;
public string name;
public string fullname;
$tempSolutions = Get-ChildItem C:\Testing,C:\Production,C:\BadSourceCodes -Recurse -Include *.sln | select Name, FullName 
$si = New-Object solution
$st = [Type] $si.GetType()
$base = [System.Collections.Generic.List``1]
$qt = $base.MakeGenericType(@($st))
$so = [Activator]::CreateInstance($qt)
for($i = 0; $i -lt $tempSolutions.Count; $i++)
$obj = New-Object solution
$ = $i
$ = $tempSolutions[$i].Name
$obj.fullname = $tempSolutions[$i].FullName
$so | Format-Table -AutoSize
$choice = Read-Host "Enter a number to open a vs solution or letter x to exit: " 
if($choice -eq "x"){
Write-Host -ForegroundColor red "exited selection" -BackgroundColor black
Write-Host -ForegroundColor Yellow "Opening VS solution:" $so[$choice].name
Start-Process $so[$choice].fullname

The above script will recursively list all .sln files and then create a custom object for each file and then add it to a list object.  To launch a particular solution, we provide a number to get that object from a list and use the fullname property of the object to the start command to open that solution inside visual studio 2010.

I have created this little function and inside this function I provide the path to the script file for launching solutions.

function get-vssolutions {

PS C:\> get-vssolutions 

Enter a number to open a vs solution or type letter x to exit: 

Friday, July 20, 2012

Launching Apps with PowerShell Functions

Launching of apps is much easier with windows 7 taskbar.  The "pin to taskbar" option when you right click any open software will pin the item to the taskbar.  I have lots of remote desktop sessions pinned on remote desktop connection. However, there are some situations where launching some softwares it is quicker through your keyboard than through the mouse.  AutoHotkey is very popular when you want to assign shortcut keys for launching apps and trigger some custom actions.  However, I have fallen in love with the PowerShell way of launching some of my favorite and most used apps on windows.   I have found it very useful and so I would like to share them with you.

Launch Remote Desktop Connections with PowerShell 
Put this PS function in your PS profile and replace server10 with your server name in the following command and see remote desktop connection open up on its own.  For the first time, you will have to enter the password.  I love this very much.  

function rmd {
& "C:\windows\system32\mstsc.exe" /v:$computername /fullscreen

PS> C:\>rmd server10

Search about a PowerShell error on Google from PowerShell
Suppose you had an error when you were working inside powershell and you had no idea what that error was and you thought that a Google search might result an answer.  At this point you don’t want to copy the error object or text and then open or switch to Google and type.  This function takes care of searching on Google for you.  It will search for the last error [$error[0]] encountered .  

Check this function out and if you like it then put it in your powershell profile.  Replace the chrome.exe path with your path or it might be just replacing Goofy with your name.

function Get-ErrorInfo

& "C:\Users\Goofy\AppData\Local\Google\Chrome\Application\chrome.exe"$error[0].Exception

Let’s do some dumb mistake and try to search it on Google.

PS C:\> get-comand19

The term 'get-comand19' is not recognized as the name of a cmdlet, function, script file, or operable program. Check th
e spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:13
+ get-comand19 <<<<
    + CategoryInfo          : ObjectNotFound: (get-comand19:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\> get-errorinfo

Just search on Google with PowerShell
If you observed the command carefully then you know how to search on Google through powershell.  Here is my simple function to search on Google.

function g { 
& "C:\Users\Goofy\AppData\Local\Google\Chrome\Application\chrome.exe"$searchstring

Launch volume control pop-up with PowerShell
I have a bad habit of experimenting something in powershell (more on that later).  However, I found this link on about open the volume control with a command.  Try this command “sndvol.exe –f” in run command window (open it by typing win+r) and you can see volume control but again when working in PowerShell I want to do it from PowerShell. Sweet little no brainer PS function. 

function vol { & "C:\windows\system32\sndvol.exe" -f}

Hit vol and see volume control pop-up.  Use up and down arrow key to control vol (obviously).
Launch a Visual Studio solution with PowerShell
Start a visual studio solution from PowerShell.  Yes, I know that you can pin your most frequented items to task bar but I wanted to do it in powershell. I have created a small PowerShell function to start my daily project that I am working on. I don’t if people would find it useful or not but I do find it useful in my work life.  

function vs {
start "C:\sourcecode\dailyprojectIworkOn.sln"

Launch a word document with PowerShell
Ah! So easy once you know how the start cmd works right. Just put it in front of any document and it will open in relevant software for you. One of the things I have opened while working on a project is having a word document opened up.  It is like my live journal of that project.  Over a time my word document get very large and I have to do two things: 1) Open word doc 2) Scroll to the last page 3) Save it before exiting.  For two and and three I have created a word macro which will scroll the doc to the last page and save it for me before I exit.  Finally for launching that word document I do this.

function show{
"pm" {
start "C:\project\projdoc.docm"
So you see I don't panic if the start menu is gone from windows 8.  I only have to remember the PowerShell way of launching apps.  If you have some other PowerShell functions that you put in your PowerShell profile then share with me in the comments sections.

Sunday, July 8, 2012

Using Powershell Function to Quickly Test Regular Expressions

I have found the easiest way to learn and experiment with regular expressions using Powershell.  In this function below, I have converted .Net code to test regular expressions into PowerShell Function test-regex.  It accepts two parameters $inputString and $regex.  In $regex you provide the regular expression you want to test.  Put this little snippet into your PowerShell profile so that you don’t have to run this function everytime.  PowerShell Profile will run this function everytime you open a new PowerShell session.  This MSDN article explains on how to put functions into a PowerShell profile.

function test-regex {
$match = [System.Text.RegularExpressions.Regex]::Match($inputString,$regex)
Write-Host $match.Value -foreground "Yellow" 

This allows me to quickly test regular-expressions in powershell without much hassle. And if you want to find help about regular expressions inside powershell then you can type following command and get all help related to regular expressions.

>PS help about_regular*

I refer “Regular Expression Language Reference” MSDN article to understand more about regular expressions.  

In the below example, I wanted to extract Distinguished Name (DN) from a list of thousands of users from a text file.  Each line in that text file had a Unique DN.  So I resorted to Regular Expression to match DN in each line.   Check out the sample text which has DN in bolded text.

Some Random Text in which our DN is located so here is our Distinguished Name: CN=jsssmith2,OU=allUsers,OU=Payroll Users,DC=Com2,DC=myCompany,DC=com.  Everyline has this DN and we want to extract DN from every line.

I want to show how we can easily test this using powershell function we created. Copy the above text and paste in powershell using just right click.  Check out the syntax to test the function.

Our input string and regex are in double quotes above.  You can see the matching text in Yellow color.  The nice thing about this is if you get it wrong then iterating is easy because you just have to hit the up arrow on your keyboard and just change the regex part and it will give you another match.  

The matching regular expression was “CN.*DC=com”.  The neat thing in this approach I like is not remembering any software to open to test regexes.  Since I have PowerShell as my first item on my task bar I hit Win+1 and start testing.  I hope you like this approach.  If there is any easier approach or quicker way to test regular expressions then please let me know about it in the comments sections.