Tag: Powershell
Analyzing Visual Studio Build Performance
by bhartsock on Dec.22, 2009, under Uncategorized
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. From there, you can change the MSBuild verbosity to diagnostic.
Now just build (or rebuild). Then copy the build output to a txt file and save it somewhere.
The only problem with the output is the amount of information (300K lines on a rebuild of a medium size project). It has ton’s of valuable information, but an easy way to process it is needed. The biggest value, in my opinion, is the section at the end of every project where the Summary information about projects, targets, and tasks are displayed. To help retrieve this info, I wrote a quick little Powershell script.
param ( [string] $file = "" ) if(!(test-path $file)){ throw "$file not found" } function accumulate_and_print_project_info(){ begin{ $curr_project = "" $should_print = $false } process{ if($_ -match "----- (re)?build (all )?started: project: (.*) -----"){ $curr_project = $matches[3] "----- $curr_project -----" $should_print = $false }elseif($curr_project -and !$should_print -and $_.EndsWith("Summary:")){ $_ $should_print = $true }elseif($curr_project -and $should_print -and $_ -eq ""){ $should_print = $false }elseif($should_print){ $_ } } } gc $file | accumulate_and_print_project_info
The output is going to look something like the following.
----- ControlPanel, Configuration: Development Any CPU -----
Project Performance Summary:
2672 ms C:\Users\brian.hartsock\Documents\Visual Studio 2008\Projects\Api\cp3\ControlPanel\ControlPanel.csproj 1 calls
Target Performance Summary:
0 ms CreateSatelliteAssemblies 1 calls
0 ms _ComputeNonExistentFileProperty 1 calls
0 ms Clean 1 calls
0 ms _AfterCompileWinFXInternal 1 calls
0 ms BeforeCompile 1 calls
0 ms BeforeClean 1 calls
0 ms AfterBuild 1 calls
0 ms GetTargetPath 1 calls
0 ms _CheckForCompileOutputs 1 calls
0 ms EntityDeploy 1 calls
0 ms ResolveVCProjectReferences 1 calls
0 ms _SetEmbeddedWin32ManifestProperties 1 calls
0 ms StyleCopForceFullAnalysis 1 calls
0 ms CleanPublishFolder 1 calls
0 ms BeforeBuild 1 calls
0 ms CoreResGen 1 calls
0 ms BeforeRebuild 1 calls
0 ms _CopySourceItemsToOutputDirectory 1 calls
0 ms AfterResGen 1 calls
0 ms Build 1 calls
0 ms SetWin32ManifestProperties 1 calls
0 ms AssignTargetPaths 1 calls
0 ms CoreBuild 1 calls
0 ms AfterResolveReferences 1 calls
0 ms Compile 1 calls
0 ms ResGen 1 calls
0 ms AfterClean 1 calls
0 ms Rebuild 1 calls
0 ms PrepareResourceNames 1 calls
0 ms BuildOnlySettings 1 calls
0 ms PrepareResources 1 calls
0 ms CleanReferencedProjects 1 calls
0 ms AfterMarkupCompilePass1 1 calls
0 ms FileClassification 1 calls
0 ms AfterCompile 1 calls
0 ms CompileRdlFiles 1 calls
0 ms BeforeResolveReferences 1 calls
0 ms CreateCustomManifestResourceNames 1 calls
0 ms ResolveReferences 1 calls
0 ms DesignTimeMarkupCompilation 1 calls
0 ms AfterRebuild 1 calls
0 ms GetReferenceAssemblyPaths 1 calls
0 ms PrepareForRun 1 calls
0 ms AfterCompileWinFX 1 calls
0 ms BeforeResGen 1 calls
1 ms PrepareRdlFiles 1 calls
1 ms GetCopyToOutputDirectoryItems 1 calls
1 ms SplitResourcesByCulture 1 calls
1 ms _SplitProjectReferencesByFileExistence 1 calls
1 ms _GenerateCompileInputs 1 calls
1 ms _GenerateSatelliteAssemblyInputs 1 calls
1 ms GetWinFXPath 1 calls
1 ms EntityClean 1 calls
1 ms _CheckForInvalidConfigurationAndPlatform 1 calls
1 ms ResolveProjectReferences 1 calls
1 ms PrepareForBuild 1 calls
3 ms GetFrameworkPaths 1 calls
8 ms CoreClean 1 calls
14 ms IncrementalClean 1 calls
17 ms CopyFilesToOutputDirectory 1 calls
17 ms _CleanGetCurrentAndPriorFileWrites 1 calls
66 ms ResolveAssemblyReferences 1 calls
399 ms _CopyFilesMarkedCopyLocal 1 calls
519 ms StyleCop 1 calls
636 ms CoreCompile 1 calls
974 ms PostBuildEvent 1 calls
Task Performance Summary:
0 ms Delete 2 calls
0 ms Message 6 calls
0 ms EntityDeploy 1 calls
0 ms GetFrameworkSdkPath 1 calls
0 ms CreateProperty 1 calls
0 ms AssignTargetPath 5 calls
0 ms MakeDir 2 calls
1 ms ConvertToAbsolutePath 1 calls
1 ms AssignCulture 1 calls
1 ms GetWinFXPath 1 calls
1 ms FindAppConfigFile 1 calls
1 ms ReadLinesFromFile 2 calls
1 ms EntityClean 1 calls
2 ms CreateItem 3 calls
2 ms WriteLinesToFile 2 calls
3 ms GetFrameworkPath 2 calls
13 ms RemoveDuplicates 3 calls
20 ms FindUnderPath 7 calls
66 ms ResolveAssemblyReference 1 calls
415 ms Copy 6 calls
515 ms StyleCopTask 1 calls
635 ms Csc 1 calls
973 ms Exec 1 calls
Now you should have all the data you need to figure out what is taking so long to build your project.
Find Orphaned Solution Files
by bhartsock on Dec.07, 2009, under Uncategorized
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 in a Visual Studio project file to the actual files in the directory # # Usage: # .\find-orphanedFiles.ps1 Project\FooBar.csproj # .\find-orphanedFiles.ps1 Project\FooBar.csproj -exclude "*.csproj","*.csproj.user","svn.ignore" param ( $csProjFile, $exclude = ("*.csproj*") ) function custom-gci($path=(get-location)){ gci $path -exclude $exclude | %{ if($_.PSIsContainer -and ($_.name -ne "bin" -and $_.name -ne "obj")){ custom-gci $_ }elseif(!($_.PSIsContainer)){ $_ } } } if(!(test-path $csProjFile)){ throw "File $csprojFile does not exist" } $dir = split-path $csProjFile $csProjfile = split-path $csProjFile -leaf push-location $dir $x = [xml] (gc $csProjFile) $filesInProject = $x.Project.ItemGroup | foreach { if($_.Compile) { $_.Compile }elseif($_.None){ $_.None }elseif($_.Content){ $_.Content }elseif($_.EmbeddedResource){ $_.EmbeddedResource } } | select -expand include | resolve-path | gi | select -expand fullname $filesInDir = custom-gci | select -expand fullname compare-object $filesInProject $filesInDir pop-location
Restoring Transaction Logs in Powershell
by bhartsock on Nov.17, 2009, under Uncategorized
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.
gci c:\MyTransactionLogDir | sort LastWriteTime | .\restore-transactionLog.ps1 "myDb"
Using Powershell scripts from MSBuild, Scheduled Tasks, etc.
by bhartsock on Oct.20, 2009, under Uncategorized
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. Well guess what happens when I call it from powershell.exe like above?
> powershell .\TestScript.ps1 "hello world" hello
A little odd. I have a very cordial script but it is a little tongue tied. I didn’t spend a lot of time trying to figure out why this was occurring, I instead used powershell -? to help me find an alternate method, and probably better way to call Powershell scripts from the legacy cmd shell.
> powershell -Command "& { .\TestScript.ps1 'hello world' }" hello world
This worked like a charm. Note the quotes, as script blocks aren’t interpreted from the cmd shell properly, and will cause odd behavior. And from MSBuild, a little bit of XML escapage and you can easily use Powershell.
<Target Name="AfterBuild"> <Exec Command="powershell.exe -command "& {.\Register-EmailApiSnapIn.ps1 '$(TargetPath)'}"" /> </Target>
Thoughts on #RichCC
by bhartsock on Oct.14, 2009, under Uncategorized
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. Here is a summary of just a few.
Know your audience
One of the two talks I gave was on Powershell. In this talk, I didn’t understand the audience that would be in attendance. I am a coder, not a system administrator. Powershell was designed for both, but a large percentage of attendees were sysadmins, not straight up developers. This assumption caused most of my examples to be irrelevant. Instead of showing how to create a Runspace and call Powershell from within C#, I should have focused more on things like interacting with a database, remoting, and common sysadmin type tasks.
Maybe next time my abstract should be a little more well-defined so that the audience and I are on the same page.
Code very little
In my NHibernate talk, I had about 25 test cases I worked through. Each of those tests started off failing, and required one or two additional lines to make them pass. I would go through each test case, explain what it was doing, why it was failing, and type a couple lines to make it pass. This strategy worked WONDERS!
In my Powershell talk, I tried to code a script from scratch that queried the Twitter API. Bomb. I got so flustered I just copy and pasted from my helper file and made it work. I had to code too much during this demo. Here is my new mantra:
The more critics, the exponentially higher chance of a typo.
(NOTE: Please don’t think I am saying use PowerPoint. Just make it hard for you to screw up the code you have to write)
Figure out how to get some interaction
If you haven’t guessed, I think my NHibernate talk went a lot better than the Powershell talk. One reason I think this is I had almost no audience interaction in Powershell. When I did, they were asking a question that I couldn’t easily answer during my talk. NHibernate had a ton more interaction, and I felt everyone left the room with more info than when they came in.
Wow factor
The wow factor is crucial. Something that is going to make the audience remember your talk.
For NHibernate, the last thing I did was switch my config from MySQL to MSSQL (just the config, no code) and all the tests still passed. Definitely caught peoples attention.
For Powershell, I data bound a WPF app with Powershell scripts. I don’t think it achieved the wow I wanted, but I think its pretty damn cool.


