PowerShell script exchange
Post Reply
fgw
Enthusiast
Posts: 84
Liked: 2 times
Joined: Jun 11, 2009 8:39 pm
Full Name: Franz Glatzer
Contact:

running powershell cmdlet from x86 app?

Post by fgw »

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.
Martin9700
Influencer
Posts: 17
Liked: 3 times
Joined: Nov 10, 2010 2:18 pm
Full Name: Martin Pugh
Location: Massachusetts
Contact:

Re: running powershell cmdlet from x86 app?

Post by Martin9700 »

What happens when you add the snapin from the 32bit Window?
Martin
www.thesurlyadmin.com
@thesurlyadm1n
tsightler
VP, Product Management
Posts: 6009
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: running powershell cmdlet from x86 app?

Post by tsightler » 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.
fgw
Enthusiast
Posts: 84
Liked: 2 times
Joined: Jun 11, 2009 8:39 pm
Full Name: Franz Glatzer
Contact:

Re: running powershell cmdlet from x86 app?

Post by fgw »

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
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: running powershell cmdlet from x86 app?

Post by veremin »

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.
Martin9700
Influencer
Posts: 17
Liked: 3 times
Joined: Nov 10, 2010 2:18 pm
Full Name: Martin Pugh
Location: Massachusetts
Contact:

Re: running powershell cmdlet from x86 app?

Post by Martin9700 »

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
fgw
Enthusiast
Posts: 84
Liked: 2 times
Joined: Jun 11, 2009 8:39 pm
Full Name: Franz Glatzer
Contact:

Re: running powershell cmdlet from x86 app?

Post by fgw » 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.
tsightler
VP, Product Management
Posts: 6009
Liked: 2843 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: running powershell cmdlet from x86 app?

Post by tsightler »

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.
Martin9700
Influencer
Posts: 17
Liked: 3 times
Joined: Nov 10, 2010 2:18 pm
Full Name: Martin Pugh
Location: Massachusetts
Contact:

Re: running powershell cmdlet from x86 app?

Post by Martin9700 »

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
Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests