Posts
49
Comments
83
Trackbacks
0
Running scripts that only work under 32bit cleanly in 64bit PowerShell

The scenario is as following: you have some script that can only work in 32bit powershell (the COM objects it calles only work in 32 bit for example) but you are used to running 64bit powershell. So what to do you do? Easy, just write your script to check if its being called from a 32bit process and if not, spawn a copy of 32bit powershell to run itself! Tha way you never have to think about calling 32bit powershell explicitly.

Here is the snippet:

   1: if ($env:Processor_Architecture -ne "x86")
   2: {
   3:         write-warning "Running x86 PowerShell..."
   4:         &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noninteractive 
   5:              -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
   6:         exit
   7: }
   8:  
   9: # rest of your code

 

Turns out I use this trick all the time as I frequently work with COM objects such as Office Charting Components which are only 32bit.

Technorati Tags:
posted on Wednesday, December 03, 2008 12:08 AM Print
Comments
Gravatar
# re: Running scripts that only work under 32bit cleanly in 64bit PowerShell
cornasdf
9/9/2009 9:55 AM
  
Thanks for putting me on the right track. When implementing on x64 psh v2 it didn't work for me. I made a few changes and posted here: cornasdf.blogspot.com/.../...pt-to-run-in-x86.html

basically, I replaced lines 4/5 with
&"$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line

Thanks for the post.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 4 and 1 and type the answer here:
News
A little slow these days as I'm busy working on exchangelabs.com. I will try and post tidbits when I get some time. Enjoy the older posts till then!