다음을 통해 공유


Visual Studio 2008 PowerShell

Ever since I first saw a demo of PowerShell some five years ago, I've been wanting to use it for development work; such as automating code generation or whatever other task for which small scripts are needed. It's such a powerful environment, and I simply love the fact that it's based on .NET and that you can easily create new Cmdlets! What's been holding me back so far is that none of the tools I regularly use (SvcUtil, InstallUtil, etc.) are available in the default shell.

Sure, I could manually add the necessary directories to the shell's path, but since there's always been a Visual Studio Command Prompt (driven by vsvars32.bat), that seemed to me like unnecessary duplication of effort. Luckily, Chris Tavares has a solution! ...or, that's at least what I thought when I first read his post.

Unfortunately, it didn't work for me. While I'm sure that Chris tested his solution, my registry simply doesn't contain the InstallDir node that he uses. Whether that's because I'm running on Vista x64 (which causes many funky issues from time to time), or my Visual Studio installation is just plain weird, I don't know... At least, Chris' post solved the most difficult problem for me: Getting the vsvars32.bat path and other environment variables into PowerShell.

On every machine that I've ever had Visual Studio installed, there's always been an environment variable called VS90COMNTOOLS that points to exactly the folder containing vsvars32.bat, so why not use that instead of the registry approach?

While I kept Chris' Get-BatchFile function unmodified, I reimplemented the VsVars32 function as:

 function VsVars32()
 {
     $vs90comntools = (Get-ChildItem env:VS90COMNTOOLS).Value
     $batchFile = [System.IO.Path]::Combine($vs90comntools, "vsvars32.bat")
     Get-Batchfile $BatchFile
  
     [System.Console]::Title = "Visual Studio 2008 Windows PowerShell"
 }

As you can see, getting the correct path to vsvars32.bat is as simple as extracting the VS90COMNTOOLS environment variable and then using Path.Combine to construct the full path to the file.

To get a Visual Studio 2008 PowerShell (as an equivalent to the Visual Studio 2008 Command Prompt), I combined it all in a vsvars32.ps1 file. To run the script file and keep the shell open, you need to open it like this, since double-clicking a .ps1 file will just open it in Notepad:

 powershell.exe -noexit <path>\vsvars32.ps1

where <path> indicates the path to the vsvars32.ps1 file. To make it easier to quickly open a Visual Studio 2008 PowerShell, I created a shortcut with this command, and added the shortcut to my Start menu. Now I can simply type Visual Power in my Vista Start menu search box, and I then have instant access to my Visual Studio 2008 PowerShell.

As a convenience to you, my dear reader, I'm attaching the script file for your reuse - use at your own peril.

If you are new to PowerShell, note that you must change your execution policy to RemoteSigned or less (or sign my script with a certificate you trust) to be able to run scripts at all, and that you probably also need to go to the file's properties and unblock it, since you downloaded it from the internet.

Update: You can also move the VsVars32 (and corresponding Get-BatchFile) function into your profile.ps1, and just call VsVars32 from your vsvars32.ps1 file. That enables you to use VsVars32 from other scripts without having to explicitly open a Visual Studio 2008 PowerShell.

vsvars32.ps1

Comments

  • Anonymous
    April 10, 2008
    With LINQ to SQL, a couple of questions quickly arise: Since you can create a data model directly in

  • Anonymous
    May 13, 2008
    Thanks, I'm learning powershell and this helped out a lot.

  • Anonymous
    May 03, 2009
    &#160; Over the past couple of weeks I’ve been wanting to get into playing with some PowerShell for scripting

  • Anonymous
    June 26, 2011
    The comment has been removed