A better Start-Job cmdlet

With Powershell 2, the Start-Job command was added, which allows statements to be executed in the background. There are a few oddities regarding script blocks that need to be understood first though. Powershell doesn’t support closures The current working directory isn’t preserved I confronted these issues first hand today while working with a Rails app. I wanted to execute ruby script\server in a job, but it wasn’t working. PS C:\Users\Brian\workspace\myapp> start-job { ruby script\server } Id Name State HasMoreData Location Command -- ---- ----- ----------- -------- ------- 3 Job3 Running True localhost ruby script\server PS C:\Users\Brian\workspace\myapp> receive-job 3 C:\Ruby19\bin\ruby.exe: No such file or directory -- script/server (LoadError) + CategoryInfo : NotSpecified: (C:\Ruby19\bin\r...ver (LoadError):String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError For some reason, when executing the job, it is executing in a different working directory. »

Get file extension information with Powershell

I wanted to get a list of different file extensions in a project I was looking at so I knew the makeup of the code and technology. Pretty cool little command. gci -recurse | select -expand extension | group | sort count -Descending Which returned the following. Count Name Group ----- ---- ----- 667 .asp {.asp, .asp, .asp, .asp...} 530 .gif {.gif, .gif, .gif, .gif...} 345 {, , , ...} »

Analyzing Visual Studio Build Performance

Every now and again, your build time gets so long you have to investigate what the cause is. Since Visual Studio uses MSBuild, it is possible to get very detailed build information. The first step is getting into diagnostic mode. This will print out way too much information, but it is the only mode that prints out execution summaries for each project. To get into diagnostics mode, simply go to Tools -> Options -> Projects and Solutions -> Build and Run. »

Find Orphaned Solution Files

Each day, I am becoming more and more OCD. Tonight I wrote a script that will analyze a Visual Studio csproj file and compare it to the files in the directory. The main goal being an easy way to find orphaned files that might still be in version control. It probably only works on very vanilla projects, but I figured I would share anyways. # find-orphanedFiles.ps1 # # Compare files »

Restoring Transaction Logs in Powershell

Using the SQL Server UI to restore a bunch of transaction logs is the most painful thing I have every done. I created this script, and then tested restoring around 10,000 transaction logs and burnt through them pretty fast. Create the script restore-transactionLog.ps1 with the following code. (I am using integrated auth and running the script on localhost. It wouldn’t be too hard to use different connection strings in the code if you wanted) param ( [string] $dbName ) process { if(test-path $_){ $sql = "RESTORE LOG [$dbName] FROM DISK = N'$($_.fullName)' WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10" write-verbose $sql sqlcmd -Q "$sql" -E }else{ write-error "Can't find file $_" } } Then you can use it pretty easily in the following scenario. »

Using Powershell scripts from MSBuild, Scheduled Tasks, etc.

There are a few different ways to use Powershell from the legacy cmd shell. The most common way is to call it like the following. > powershell write-host "hello world" As you can see, the powershell.exe is called with Powershell commands as the parameters. I started noticing some odd behavior though. I have the following script, TestScript.ps1. It has code as follows: param ( $str ) write-host $str Very simple right. »

Thoughts on #RichCC

A couple weekends ago, I attended Richmond Code Camp and had a great time. This was different than the last code camp I went to, in the fact that I spent most of the day presenting, not watching others. I have coded in front of co-workers before. I have given presentations to strangers before. But I had never coded in front of strangers before. There were a lot of things I learned. »

Find All References to a DLL, in Powershell

Visual Studio confuses me, and it is slow. I wanted to find all the assemblies/projects that contained references to System.Web.Mvc. Instead of loading up Visual Studio, I just did this. gci -recurse -filter "*.csproj" | ?{ select-string "System.Web.Mvc" -path $_ } Faster than loading Visual Studio, I promise. »

Powershell script to Register Visual Studio Schemas

I always have to Google how to add an XML schema to Visual Studio because I forget the directory (even though it is super easy). Never again will I forget. function register-vsschemas() { begin { $schema_folders = gci $env:ProgramFiles -Filter "Microsoft Visual Studio*" | gci -Filter "Xml" | gci -Filter "Schemas" } process { if(!$_ -or !(Test-Path $_)) { Write-Error "File '$_' not found" break; } if((gi $_).Extension -ine ".xsd") { Write-Warning "File $_ does not end in an .xsd extension" } foreach($folder in $schema_folders){ #It is very interesting I had to use .FullName... »

PSDefaultVariablizer revisted

A couple of days ago, I had a random post about _PSDefaultVariablizer _because I thought the name was funny. Well, I did some pretty hardcore refactoring of the class, and am pretty happy with the outcome. Overall, I think it is 100x better. Check it out. public class PSDefaultVariablizer<t> where T: class { T innerValue; IList<func<t>> defaultValues; public PSDefaultVariablizer(params Func<t>[] _defaultValues) { defaultValues = _defaultValues.ToList(); defaultValues.Insert(0, () => this.innerValue); } public T Value { get { return defaultValues.Select(v => v.Invoke()) .Where(v => v != null) .FirstOrDefault(); } set { innerValue = value; } } } //I am using it in a cmdlet similar to this [Cmdlet("Some", "Command")] public class SomeCommand : PSCmdlet { private PSDefaultVariablizer<string> someParam; public SomeCommand () { someParam = new PSDefaultVariablizer<string>(() => (string)this.SessionState.PSVariable.GetValue("SomeParam", null)); } [Parameter()] public string SomeParam { get { return someParam.Value; } set { someParam.Value = value; } } } Thought it was cool to use Lamba’s to eliminate an entire dependency. »