Windows PowerShell call with Xojo's shell class

Hello,

is there a way to access the PowerShell under Windows with Xojo’s Shell class? So far I have not succeeded.

[code]Var myShell As New Shell

myShell.Execute(“compress-archive -Path C:\Test -DestinationPath C:\Test.zip”)
MessageBox(myShell.Result)[/code]

Try putting powershell before your call to compress-archive ?

E.g. this works for me:

[code]Var myShell As New Shell

myShell.Execute(“powershell get-help”)
MessageBox(myShell.Result)[/code]

Good suggestion, Julian. PowerShell 5 is only available for Windows 10 and later.

By means of System.Version I have since Xojo 2019.3 the possibility to query the version of the operating system. From Windows 8 on PowerShell was included, but uses a different syntax. So my above command to create a zip file will probably not work on Windows 8 (I have Windows 10 as a test system). Any suggestions?

You want to zip on older platforms? Uhhh either a plugin, dll or write your own using the api here https://docs.microsoft.com/en-us/windows/win32/cmpapi/-compression-portal I guess?

When we tried it, we had the problem that we need some permissions with windows to be allowed to execute powershell from the application.

Hi @Christian_Schmitz,

can you please specify your description? When exactly did the error occur? Did you find a workaround without plugins? In my case, creating a zip file sometimes works and sometimes doesn’t. It happens completely randomly, it is always the same code.

@anon20074439 - Do you have a Xojo implementation for your proposal?

That was long ago. The execution policy is restricted and you need to change it.

e.g. see here:
https://winaero.com/blog/change-powershell-execution-policy-windows-10/

Thank you @Christian_Schmitz, for the quick feedback. I mainly work under macOS, but do I read correctly that the item “Change the PowerShell Execution policy for a process” might be the right one for me? Do I understand correctly that the PowerShell restrictions are only temporarily changed for the Xojo Shell process to be executed? That would be great.

Would my shell call have to look like this? Would you do it the same way?

Var myShell As New Shell

myShell.Execute("powershell -ExecutionPolicy Unrestricted " + _
  "compress-archive -Path C:\Test -DestinationPath C:\Test.zip")
MessageBox(myShell.Result)

Sorry, I can’t help you there. The restrictions must be lower to call powershell from your app. Whether this would be temporary or permanent, may be your choice.

You could consider using 7za (command-line version of 7zip). You can include it with your application and should work on all recent versions of Windows.

You could consider MBS Xojo Compression Plugin.

You could consider Einhugur Zip Archiver Plugin (under e-cryptit)

1 Like

Thanks @LangueR. I would like to try to get along without plugins.

Is this a plugin? Is 7za part of Windows? Is it open source? Do you have a working example?

1 Like

From 7-Zip.org:

Can I use the EXE or DLL files from 7-Zip in a Commercial Application?
Yes, but you are required to specify in your documentation (1) that you used parts of the 7-Zip program, (2) that 7-Zip is licensed under the GNU LGPL license and (3) you must give a link to www.7-zip.org, where the source code can be found.

How can I add support for 7z archives to my application?
One way is to use the 7z.dll or 7za.dll (available from sf.net for download). The 7za.dll works via COM interfaces. It, however, doesn’t use standard COM interfaces for creating objects. You can find a small example in “CPP\7zip\UI\Client7z” folder in the source code. A full example is 7-Zip itself, since 7-Zip works via this dll also. There are other applications that use 7za.dll such as WinRAR, PowerArchiver and others.

The other way is to call the command line version: 7za.exe.

You would better tell customers that if 7 zip is installed, your app may be able to use it. Alternatively use unzip if present.
Then do not include it.

Of course we have a plugin to do archives…

Using plugins, which allows for xplat, there is the MBS Xojo Compression and Einhugur Zip Archiver, as I suggested previously. I think if the goal is to make an app with xplat support that will be used commercially, then either of those represents probably the simplest method.

In the company I work for they use (almost universally) 7-zip for any in-house application (and even for some distributions outside). They promote it because of its licensing terms (which are really as simple as the FAQ spits out). That’s how I learned to use it. Including the command-line EXE adds less than 500k to the application; that is really hard to beat.

@Martin_T

Newer versions of PS, 7 and all dot releases since are cross platform.

I use some cross platform Powershell libraries in my Xojo Web Apps. I just tried some sample code of mine that reads values from Excel (tested with mods to file path on Win, Mac and Linux) and works fine.

From memory I had to play around with the ExecutionPolicy and settled on ByPass and also used the NoProfile switch (does not attempt to load the PowerShell profile).

My quick example below is otherwise irrelevant other than for the form of the execute statement

Var sheet As String = "1" // default
Var row As String = pmRow.Text
Var col As String = pmCol.Text

Var sh As Shell
sh = New Shell

sh.Execute("powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\ps_scripts\excel_get.ps1 -sheet " + sheet + " -row " + row + " -col " + col + " -Verb RunAs")

If sh.ErrorCode = 0 Then
  tfResult.Text = sh.Result
Else
  MsgBox("Error code: " + sh.ErrorCode.ToString)
End If

sh.Close

I hope that helps.

1 Like