Sign in to follow this  
Followers 0
vickerps

using ActivexPosh to run Powershell statements problem

1 post in this topic

Hi Guys

I have recently found that autoit can now link into Powershell which is great news. 

However things are never simple and i have found a flaw which im hoping someone can solve for me.

I have a problem with any dll compiled from .net 4. For example the following script work fine with .net 2 dll but doesn't work .net 4.

#include <GUIConstants.au3>
#include <Array.au3>
#include <File.au3>

Dim $ActiveXPosh

Const $OUTPUT_CONSOLE = 0
Const $OUTPUT_WINDOW = 1
Const $OUTPUT_BUFFER = 2

CreateActiveXPosh()
$File = FileOpen('result.txt',10)
    $dll = "C:\Windows\system32\mfcm100.dll"
        $sPSCmd = '[Reflection.Assembly]::LoadFrom("' & $dll &'").ImageRuntimeVersion'
        $val = ExecuteCMD($sPSCmd)
        Filewrite($File,$Dll & '    ' & $val & @CRLF)
    $dll = "C:\Windows\system32\dnscmmc.dll"
        $sPSCmd = '[Reflection.Assembly]::LoadFrom("' & $dll &'").ImageRuntimeVersion'
        $val = ExecuteCMD($sPSCmd)
        Filewrite($File,$Dll & '    ' & $val & @CRLF)

Func CreateActiveXPosh()
 Local $success
 ; Create the PowerShell connector object
 $ActiveXPosh = ObjCreate("SAPIEN.ActiveXPoSH")
 $success = $ActiveXPosh.Init(False) ;Do not load profiles
 If $success <> 0 then
  Consolewrite( "Init failed" & @CR)
 endif
 If $ActiveXPosh.IsPowerShellInstalled Then
  Consolewrite( "Ready to run PowerShell commands" & @CR & @CR)
 Else
  Consolewrite( "PowerShell not installed" & @CR & @CR)
 EndIf
 ; Set the output mode
 $ActiveXPosh.OutputMode = $OUTPUT_CONSOLE
 $ActiveXPosh.OutputWidth = 250
EndFunc

Func ExecuteCMD($sPSCmd)
 Local $outtext = ''
 ; Set the $OUTPUT mode to $BUFFER
 $ActiveXPosh.OutputMode = $OUTPUT_BUFFER
 $ActiveXPosh.Execute($sPSCmd)
 $outtext = $ActiveXPosh.OutputString
 $ActiveXPosh.ClearOutput()
 Return $outtext
EndFunc

By trawling the internet I have found to interigate .net4 dll's with powershell you need to add a config file (Powershell.exe.config)

<?xml version="1.0"?> 
<configuration> 
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4.0.30319"/> 
        <supportedRuntime version="v2.0.50727"/> 
    </startup> 
</configuration>

However adding this doesn't work from a autoit script. Can anyone help?

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0