VFP and .NET Interop
Running Powershell from VFP
Gravatar is a globally recognized avatar based on your email address. Running Powershell from VFP
  Alejandro A Sosa
  All
  May 22, 2017 @ 08:42am

In Doug Hennig's excellent article on Powershell he gives the following code as a way to instantiate Powershell from VFP using wwDotnetBridge.

* Instantiate wwDotNetBridge.
set procedure to wwDotNetBridge
loBridge = createobject('wwDotNetBridge', 'V4')
* Load the PowerShell assembly. Get the location using
* [psobject].assembly.location in PowerShell.
llReturn = loBridge.LoadAssembly('C:\Windows\Microsoft.Net\assembly\' + ;
'GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35' + ;
'\System.Management.Automation.dll')
if not llReturn
  messagebox(loBridge.cErrorMsg)
  return
endif not llReturn
* Create a PowerShell object.
loPS = loBridge.InvokeStaticMethod('System.Management.Automation.PowerShell','Create')
Is there a way to do it without the GAC_MSIL reference? Is the address above, including the v4.0_3.0.0.0__31bf3856ad364e35 part guaranteed to exist indefinitely?

Thank you very much

Alex

Gravatar is a globally recognized avatar based on your email address. re: Running Powershell from VFP
  Rick Strahl
  Alejandro A Sosa
  May 22, 2017 @ 11:41am

Why not just shell out to powershell.exe and add command line args to run what you need?

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Running Powershell from VFP
  Alejandro A Sosa
  Rick Strahl
  May 22, 2017 @ 12:26pm

Do you mean to use the vfp run or equivalent with wsh run ot exec and capture the output via a file?

If that is the only possibility ok, but I was wondering if is was possible to avoid the black screen.

Gravatar is a globally recognized avatar based on your email address. re: Running Powershell from VFP
  Rick Strahl
  Alejandro A Sosa
  May 22, 2017 @ 12:46pm

It depends on what you need to do.

You can use the CreateProcess() or CreateProcessEx API functions in the West Wind tools to cleanly shell out.

If you need to deal with the result output you can easily pipe to file, then read the file.

That said, I'm not really sure why you can't just use the DLL name on its own when it's supposed to be in the GAC. Those dlls should just be accessible.

Actually it is, but you need to use the fully qualified assembly name like this:

loBridge = GetwwDotnetBridge()

*** Fully qualified assembly name
llReturn = loBridge.LoadAssembly('System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')

if not llReturn
  messagebox(loBridge.cErrorMsg)
  return
endif not llReturn

* Create a PowerShell object.
loPS = loBridge.InvokeStaticMethod('System.Management.Automation.PowerShell','Create')
? loPS

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Running Powershell from VFP
  Alejandro A Sosa
  Rick Strahl
  May 22, 2017 @ 06:21pm

Thank you very much Rick.

Only really getting into Powershell now in order to automate a few things in AWS. Totally AWeSome stuff. Still not finished.

Alex

Gravatar is a globally recognized avatar based on your email address. re: Running Powershell from VFP
  Alejandro A Sosa
  Rick Strahl
  May 23, 2017 @ 12:59pm

FWIW. I'd welcome a session on how to take advantage of Powershell with VS in SWFox.

© 1996-2017