Author Topic: Calling PowerShell - x86 vs. x64  (Read 1116 times)

seckner

  • Jr. Member
  • **
  • Posts: 57
Calling PowerShell - x86 vs. x64
« on: November 18, 2016, 11:07:23 am »
With thanks to Tony I've got Winbatch calling a PowerShell script to delete a bunch of bloatware (3dbuilder, bingfinance, etc.. ) The code that works is below. This will ONLY work if I compile the script as x86 - if I compile it as x64 it throws a COM/CLR exception  System.Management.Automation Object reference not set to an instance of an object. It only happens on an x64 compile - x86 breezes right on through. What am doing wrong?

cRun=FileGet(cScript)
ObjectClrOption("use", "System.Management.Automation,version=1.0.0.0,publicKeyToken=31bf3856ad364e35,culture=neutral")
objAutoPs = ObjectClrNew("System.Management.Automation.PowerShell")
oPshell = objAutoPs.Create()
oScope = ObjectType("BOOL",@TRUE)
oPshell.AddScript(cRun,oScope)
objAsync = oPshell.BeginInvoke()
oPShell.EndInvoke(objAsync)
oPshell.Dispose()
oPshell=0

td

  • Tech Support
  • *****
  • Posts: 2189
    • WinBatch
Re: Calling PowerShell - x86 vs. x64
« Reply #1 on: November 18, 2016, 11:33:02 am »
The "System.Management.Automation" assembly has MSIL processor architecture so bitness isn't the issue with regard to the assembly's compatibility.   The script originally posted back in , I think, January that demonstrated using the assembly, works equally well in either 32 or 64 bit WinBatch.  That suggests that the cause may be something in your PowerShell script hat is sensitive to bitness.  First, which line in your WinBatch script  is causing the error?  Obviously, if the error is occurring before you pass the PowerShell script to the assembly class object, the PowerShell script cannot be the problem.
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates


seckner

  • Jr. Member
  • **
  • Posts: 57
Re: Calling PowerShell - x86 vs. x64
« Reply #2 on: November 18, 2016, 12:02:36 pm »
I'm sorry - should have put that in there - duh! Also included the ps1 file. This is the error line: oPShell.EndInvoke(objAsync)

td

  • Tech Support
  • *****
  • Posts: 2189
    • WinBatch
Re: Calling PowerShell - x86 vs. x64
« Reply #3 on: November 18, 2016, 02:01:22 pm »
Nice way to get rid of the bloat but there is nothing that jumps out as bit related in the ps script.  You may (or may not) be running into permission issues since you are working in a Windows 8/10 environment.  Check your compiler settings to make sure your 64-bit UAC settings match your 32-bit compiler UAC settings.   UAC settings seem to be the most common problem users have post Windows 7.  If this isn't the cause, let us know and we will look into it some more.   

 
"Success is a lousy teacher. It seduces smart people into thinking they can't lose."
  - Bill Gates