1

I am writing a Powershell script which will be integrated into a product designed for 32 bit Windows machines. So on invocation it will by default run on the x86 Powershell even on 64 bit machines. One of the lines needs to be run on a 64 bit shell for accurate results. Tried doing this :

#Above this is the ongoing script in 32bit session
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
    #write-warning "Excecuting the script under 64 bit powershell"
    if ($myInvocation.Line) {
        [System.IntPtr]::Size
        &"$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile 
        #Statement block
    }else{
        &"$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile  $args
        #Statement block
    }

}
#Below this is the remaining Script in 32 bit session

I am not able to pass the control flow to the new session and take it back. All it does is start the 64 bit shell inside the already running 32 bit shell. Any possible way to do this and also return the values printed by the new shell?

vercetti
  • 55
  • 1
  • 8
  • 1
    So, what happens on the 32-bit target machines? They all output inaccurate results? – Mathias R. Jessen Apr 28 '20 at 14:16
  • Is this Kace? Can you just run a script or a command as 64 bit, or run the whole thing as 64 bit? – js2010 Apr 28 '20 at 14:38
  • @MathiasR.Jessen The data I am trying to fetch are all the installed applications on the system. This can be fetched from the registry path `Get-Childitem "HKLM://SOFTWARE//Microsoft//Windows//CurrentVersion//Uninstall//*" ` However on a 64-bit machine if the above is run on a 32-bit shell, it does not contain all the installed applications for some reason. – vercetti Apr 28 '20 at 15:34
  • @vercetti Ahh, gotcha. Maybe [query the 64-bit registry view explicitly](https://stackoverflow.com/a/19381092/712649) when on 64-bit? – Mathias R. Jessen Apr 28 '20 at 15:39
  • @js2010 the other part of the script needs to run on a 32-bit shell. _Is this Kace?_ Are you talking about a particular script? – vercetti Apr 28 '20 at 15:42
  • There's a management software called Kace that still has a 32-bit client. I guess you don't have it. – js2010 Apr 28 '20 at 15:47
  • @MathiasR.Jessen That is still the invocation but does not specify how to send and receive arguments from the invoked child shell – vercetti Apr 28 '20 at 15:58

1 Answers1

0

I was able to do it using the -Command parameter. Invoke the respective shell(32 or 64) depending on your use case. Directly pass the command or the complete script block inside the paranthesis.

#Invoking the 64-bit Powershell shell 
& "$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -Command 
{ 
# Statement Block  
}

The output can be stored in a variable if required. The parent shell resumes once the control is returned.

Check out other examples to use the -Command argument in the official docs here.

vercetti
  • 55
  • 1
  • 8