running powershell cmdlet from x86 app?

PowerShell script exchange

running powershell cmdlet from x86 app?

Veeam Logoby fgw » Thu Jan 17, 2013 3:35 am

i'm finally forced into using powershell for monitoring vbr jobs. first word i hate it! used to program c/c++ against win32 api and this is a a childrens party compared to using powershell:

anyway, my problem is as follows:

using vbr v6.5 with latest patch (although i dont think that matters here) on win2008r2

starting powershell and executing cmdlets/scripts is working. as i'm on an x64 system, this is all running in 64bit environment.

but here comes the interesting part:

i have this self written application to monitor our systems. this application is written in 32bit for compatibility with older x86 systems. this application obviously runs in 32bit environment thus when executing powershell cmdlets/scripts from within this application they are executed in a 32bit environment. found out, that i had to set execution policy for both environments separately. this are really funny guys at microsoft!

the point is, the veeam snapin is registered during installation for 64bit powershell environment only:

64 bit powershell:
Code: Select all
PS C:\Users\user> Get-PSSnapin -Registered

Name        : VeeamPSSnapIn
PSVersion   : 2.0
Description : This is a PowerShell snap-in that includes the Veeam's cmdlet.

PS C:\Users\user>


32 bit powershell:
Code: Select all
PS C:\Windows\SysWOW64\WindowsPowerShell\v1.0> Get-PSSnapin -Registered
PS C:\Windows\SysWOW64\WindowsPowerShell\v1.0>


how can i register the veeam powershell snapin for the 32bit environment? if thats even possible. may be veeam cmdlets require 64bit?

after playing around with this for several hours i'm somehow, well, let say not amused with this powershell stuff. shortly before dropping all this
and moving to accessing the veeam database directly to gather job status and statistics.
fgw
Enthusiast
 
Posts: 65
Liked: 2 times
Joined: Thu Jun 11, 2009 8:39 pm
Full Name: Franz Glatzer

Re: running powershell cmdlet from x86 app?

Veeam Logoby Martin9700 » Thu Jan 17, 2013 3:53 am

What happens when you add the snapin from the 32bit Window?
Martin
www.thesurlyadmin.com
@thesurlyadm1n
Martin9700
Influencer
 
Posts: 17
Liked: 3 times
Joined: Wed Nov 10, 2010 2:18 pm
Location: Massachusetts
Full Name: Martin Pugh

Re: running powershell cmdlet from x86 app?

Veeam Logoby tsightler » Thu Jan 17, 2013 4:38 am 1 person likes this post

So the reason that this happens is because windows silently redirects 32-bit applications that attempt to access C:\Windows\System32 to C:\Windows\SysWow64, however, assuming you can configure your 32-bit application to call Powershell using a specific path you should be able to work around this to have the application start a 64-bit powershell:

Code: Select all
C:\Windows\sysnative\windowspowershell\v1.0\powershell.exe -File "<Path>\<Script.PS1>"


The "sysnative" portion of the path overides the default redirection and will allow a 64-bit Powershell.exe to be spawned by a 32-bit application. If your 32-bit application is hard coded to simply call Powershell.exe using the default "system32" path then it's a little more annoying, but you can create a simple "wrapper" script that does nothing but runs the above command to "escape" the 32-bit environment and executes the real script.

If you need a more detailed example let me know.
tsightler
Veeam Software
 
Posts: 4769
Liked: 1738 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: running powershell cmdlet from x86 app?

Veeam Logoby fgw » Thu Jan 17, 2013 10:56 am

Martin9700 wrote:What happens when you add the snapin from the 32bit Window?


as i'm new to powershell i have no idea how to do this. played around with this, but the commands i used failed.
also i don't know if this had even worked ...

tsightler, you are my hero! expected there should something like this exist as you can use the WOW64 stuff to force 32bit in an 64bit environment. using your hint i also found the api functions Wow64DisableWow64FsRedirection and Wow64EnableWow64FsRedirection to force this redirection within my program for the part where powershell gets executed.

anyway, to use C:\Windows\sysnative\... seems the simpler solution and as it works as expected i use this now!

THANKS
fgw
Enthusiast
 
Posts: 65
Liked: 2 times
Joined: Thu Jun 11, 2009 8:39 pm
Full Name: Franz Glatzer

Re: running powershell cmdlet from x86 app?

Veeam Logoby v.Eremin » Thu Jan 17, 2013 1:30 pm

As far as I know, only Windows Server 2008 ( +newer versions) makes the original %SystemRoot%\System32 directory available as %SystemRoot%\Sysnative.

If you’re willing to have similar functionality in x64 instances of Windows Servers 2003 or Windows XP, you have to install this Microsoft hotfix.

Furthermore, for those who have problems installing this hotfix the issue of 32-bit applications being unable to access the system32 folder might be solved by utilizing junction links.

Hope this helps.
Thanks.
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 971 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: running powershell cmdlet from x86 app?

Veeam Logoby Martin9700 » Thu Jan 17, 2013 2:07 pm

fgw wrote:as i'm new to powershell i have no idea how to do this. played around with this, but the commands i used failed.
also i don't know if this had even worked ...


Sorry, sounds like you got the solution, but just for the sake of thoroughness, here's how you would add the snapin:

Code: Select all
Add-PSSnapin VeeamPSSnapin


When you launch Powershell.exe it doesn't load any snapins by default, you have to tell it what to load (or change your $Profile to add them for you).
Martin
www.thesurlyadmin.com
@thesurlyadm1n
Martin9700
Influencer
 
Posts: 17
Liked: 3 times
Joined: Wed Nov 10, 2010 2:18 pm
Location: Massachusetts
Full Name: Martin Pugh

Re: running powershell cmdlet from x86 app?

Veeam Logoby fgw » Thu Jan 17, 2013 2:29 pm 1 person likes this post

v.Eremin

Thanks for the info!

Fortunately, although the monitoring application is running on a variety of operating systems, the powershell scripts are executed on the veeam backup servers only. These servers are win2k8 r2, so everything is fine now!

Martin9700

Yes, my monitoring application runs as expected now. no need to install the plugin in the 32bit powershell environment.
Thanks.
fgw
Enthusiast
 
Posts: 65
Liked: 2 times
Joined: Thu Jun 11, 2009 8:39 pm
Full Name: Franz Glatzer

Re: running powershell cmdlet from x86 app?

Veeam Logoby tsightler » Thu Jan 17, 2013 3:17 pm

As far as I now, the 64-bit install of Veeam does not include the 32-bit Powershell snapin which is why I suggested the above option.

Another method I've used to work around this is to use Powershell remoting to have the 32-bit environment "remote" into the 64-bit environment on the same computer. Then you can just use "Invoke-Command" from the 32-bit environment to run scripts in the 64-bit environment. I didn't suggest this option first because I felt it was more complex and required even more setup, however, it does offer the advantage that it works the same on all platforms, including Windows 2003, which as Vladimir mentioned, typically requires a hotfix to get the "sysnative" functionality.
tsightler
Veeam Software
 
Posts: 4769
Liked: 1738 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler

Re: running powershell cmdlet from x86 app?

Veeam Logoby Martin9700 » Thu Jan 17, 2013 3:29 pm

tsightler wrote:Another method I've used to work around this is to use Powershell remoting to have the 32-bit environment "remote" into the 64-bit environment on the same computer. Then you can just use "Invoke-Command" from the 32-bit environment to run scripts in the 64-bit environment. I didn't suggest this option first because I felt it was more complex and required even more setup, however, it does offer the advantage that it works the same on all platforms, including Windows 2003, which as Vladimir mentioned, typically requires a hotfix to get the "sysnative" functionality.


Love remoting. Great way to run Veeam cmdlets from your desktop, too.

Code: Select all
$s = New-PSSession -ComputerName <veeamserver>
Invoke-Command -Session $s -Scriptblock { Add-PSSnapin VeeamPSSnapin }
Import-PSSession $s


Then when you're done you can tear it back down.

Code: Select all
Remove-PSSession $s
Martin
www.thesurlyadmin.com
@thesurlyadm1n
Martin9700
Influencer
 
Posts: 17
Liked: 3 times
Joined: Wed Nov 10, 2010 2:18 pm
Location: Massachusetts
Full Name: Martin Pugh


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 8 guests