There is java package which I want to import in my powershell script and use the classes available in the package.
Can someone help me achieving this?
PowerShell is based on the .NET runtime. (See the Wikipedia article.) You cannot use Java classes in it. You may, however, use .NET classes. They can be loaded as described here.
Your options are basically:
Find a .NET equivalent. There is often one available, actually.
Write a Java program and execute it using the Java runtime. (java.exe MyClass or whatever command would be used to run it.)
Related
I have a need to create a Module that can Run in Powershell 7 and use commandlets from Powershell 5.
I want to save this module as an artifact and publish in AzureDevOps Artifacts.
The Module is for auditing cross platform system information. The problem is that some of the cmdlets are Windows platform specific such as Get-WindowsFeature. I also want to use PowerShell Core functions such as Azure Cosmos communication cmdlets.
How do I load functions only on certain platforms?
Do you need to write something in C# to achieve this, or nest a module for a specific platform in my main module?
The comments mention correctly you can wrap up a command with a version check.
That's a great option for a small use command.
I'd recommend as a better module design to just have two modules, one for each platform.
This would allow you to better seperate your work, and not rely on many embedded logic steps that conditionally run actions on different platforms. To me this is just cleaner.
As you get started on modules, I'd highly recommend you use a template to bootstrap your project. I've found that it saves a lot of time, and sets me up for best practices.
My personal favorite is PSModuleDevelopment which you can use like this:
Install-Module PSModuleDevelopment -Scope CurrentUser
Get-Help 'Invoke-PSMDTemplate'
This is very similar to the loading structure some very mature projects like dbatools and PSFramework use. If you use this, you benefit primarily from:
Being able to seperate all your functions into their own files and load them easily
Some nice enhancements to preload configurations in your module
Pester test template included
I stopped trying to write my own module structure and just leveraged a development module like this, and it's been very helpful for me.
Good luck!
The Saxon website says Saxon/C can be invoked from Perl, but I can't find any examples. The only thing I've found that interfaces to Saxon is one old Perl module (XML::Saxon::XSLT2) which uses Inline::Java and apparently is very slow. But I can find nothing that uses Saxon/C. Has anyone had any success in doing this who can share some tips?
we have not yet officially done the integration work needed to extend Saxon/C on perl it is still on our todo list. Therefore we currently don't support it. I don't know of anyone who has done this work as yet but I know it is can be done.
On the Saxon website we state that it is possible to create extensions in languages like Perl since Saxon/C has a C/C++ interface. Currently, we only have extensions for PHP and Python (available in the next release).
As a workaround you could run the transform command from Saxon/C using the exec function in Perl instead of the Java version, therefore avoiding the need to run Java VM.
We have set of unix shell(ksh) scripts used for deployment of our product.
Actually there is a mixture of ksh+sed+awk+ant code.
Our product works on AIX only so we did not try to do our scripts portable.
But now we need to run part of our scripts not only on AIX but on Windows also.
We need to have ability to create/update DB both from AIX and from Windows.
Now for this part of functionality we use ksh+ant.
We have a requirement to install as few as possible tools on this Windows box.
In the best case it should be JRE+our products only.
What do you propose to use instead of ksh?
As I know we can put Groovy jar into our project and write this part of functionality on Groovy.
In this case this code will be portable.
May be there are better solutions than Groovy?
Any JVM language such as Jython or Scala should work as well as Groovy so it’s really a choice of what the developers are comfortable with. I’ve had good success with Groovy and have been able to bundle Groovy as a jar file and execute any script I wanted in the following way
Java -jar groovy.jar myscript.groovy
I’ve been able to do this on z/OS, Windows, and Linux.
I'm trying to use a python package from IronPython.
Everything works fine if I import regular python modules.
But when I try to do the following:
import win32ui
I get:
No module named win32ui
I've hunted through the code in IronPython.Runtime.Importer and there's no mention of .pyd
Anyone know a way around this?
You can check out IronClad which is working to provide this support. It may or may not work w/ your PYD of choice.
A .pyd file is a DLL. So unless IronPython (which is written in .net) can correctly load C DLLs written for CPython, you might be out of luck.
Update
In fact, according to the IronPython FAQ, you are unfortunately unable to import .pyd files:
Q: How do I build and call into PYD libraries?
A: IronPython does not support using PYDs built for CPython since they
leverage implementation details of CPython. You can get a similar
effect for new "PYD"s you would like to implement by writing them in C#
or VB and building a DLL for .NET.
Selenium has some nice additional libraries, as long you are using Java to write your tests, e.g. LoggingSelenium. However, these are not usable if you are writing in Perl. How do you normally do proper reporting, possibly with screenshots after every significant step etc.?
Not an ideal answer but have you looked at the Java module on CPAN? You could then use the original Java Selenium libraries such as LoggingSelenium to do your reporting. You'll need to run a Java server with the jar provided by the CPAN Java module (but if you're already using Selenium...)
You could also try Java::Import which might avoid the need to run a server.
TAP::Formatter::HTML produces nice-looking html reports. It does not do screenshots, though.