Python, PowerShell, or Other?

Question:

What are the advantages of Python, PowerShell, and other scripting environments? We would like to standardize our scripting and are currently using bat and cmd files as the standard. I think Python would be a better option than these, but am also researching PowerShell and other scripting tools.

The scripts would be used to trigger processes such as wget etc to call web services, or other applications/tools that need to run in a specific order with specific parameters.

We primarily work with the Windows stack, but there is a good chance we will need to support Unix in the future.

Asked By: eric.christensen

||

Answers:

The questions is kind of vague, but Python is much more portable than PowerShell; however, Python isn’t that prevalent on Windows. But on the other hand, I don’t believe PowerShell scripts will work on a Windows machine that doesn’t have PowerShell. Meaning they may not work in the old fashioned cmd shell. I think you’ll find more documentation and libraries for Python as well.

PowerShell is more like Bash than it is a programming language like Python.

Maybe you could explain what you want to do with your scripts and you’ll probably get better answers.

Answered By: jonescb

One advantage to Python is the availability of third-party libraries and an extensive built-in standard library. You can do a lot of powerful operations quickly and easily with Python on a variety of operating systems and environments. That’s one reason we use Python here at the office not only as a scripting language but for all of our database backend applications as well.

We also use it for XML and HTML scraping, using ElementTree and BeautifulSoup, which are very powerful and flexible Python-specific libraries for this sort of work.

Answered By: Brent Newey

Python works as a great, all-purpose tool if you’re looking to replace CMD and BAT scripts on your Windows boxes, and can also be written to run scripts on your (L)inux boxes, too. It’s a great, flexible language and can handle many tasks you throw at it.

That being said, PowerShell is an amazingly versatile tool for administering all manner of Windows boxes; it has all the power of .NET, with many more interfaces into MS products such as Exchange and Active Directory, which are a timesaver. Depending on your situation, you may get more use of of PS than other scripting languages just because of the interfaces available to MS products, and I know MS seems to have made a commitment to providing those APIs in a lot of products. PowerShell comes installed on all current versions of Windows (Windows 7+, Windows Server 2008+), and is fairly easily installed on older versions.

To address your edit that your scripts will be used to launch other processes, I think in that case either of the tools fit the bill. I would recommend PS if you plan on adding any admin-ish tasks to the scripts rather than just service calls, but if you stick to what you described, Python is good.

Answered By: MattGWagner

I find it sad no one yet mentioend good ol’ Perl.

Answered By: Rook

If you are working with web based scripting, then ActiveState’s ActivePython seems to have a lot of support for Windows specific API’s that would suit you and it has tons of great portable libraries for doing web based work.

Answered By: pokstad

IronPython has access to all of the same .NET assemblies as any other .NET language for writing system dependent scripts on Windows. But the same knowledge of Python can be used to write similar scripts on Linux, Solaris, BSD, or OS/X. If you use the standard C Python on Windows, then you can access any COM objects and it is straightforward to translate VBA examples into Python code. The SPAMBayes Outlook plugin is a good example of how far you can go with that. http://spambayes.sourceforge.net/

Python’s best feature is the “batteries included” standard library, and even though this is not distributed with IronPython, much of it will work if you just point IronPython to the installed library folder from CPython. In fact, most pure Python libraries, i.e. no compiled C or C++ modules, will work fine with IronPython. On Windows, you also have the choice of installing Python through Cygwin.com which then allows you to use a lot of modules that are normally considered UNIX-only. This can be of use if you have to maintain cross-platform scripts and you prefer consistency rather than special case coding for each OS.

And if you should need to leverage some Java classes, then Jython allows you to use the same Python language that you know to leverage this. Combine this with a nice message queuing system like RabbitMQ, and you can have Python, Jython and IronPython scripts on multiple machines all cooperating in getting the job done.

There is also a huge selection of 3rd party Python modules out there and you could spend several months trawling through delicious.com before you run out of new discoveries. This means that when you need something not part of standard Python libraries, a bit of Googling often comes up with a solution.

Some useful Python modules for scripting to replace bash, CMD and BAT files are PEXPECT http://www.noah.org/wiki/Pexpect and Python WMI http://timgolden.me.uk/python/wmi/index.html

But, in the end, Python also works just fine for simple straightforward scripts that don’t need any special features… yet!

Answered By: Michael Dillon

We would like to standardize our scripting and are currently using bat and cmd files as the standard.

It sounds like Windows is your predominate environment.

If so, PowerShell would be much better than Python.

  • PowerShell is included with Windows
    Server 2008. No need to
    deploy/install Python runtime on
    every new server that rolls in.
  • The entire Microsoft server related software (Exchange, Systems Center, etc) is transitioning to PowerShell cmdlets for functionality and extensions
  • 3rd party vendors (e.g. SCOM plugins) will also use PowerShell scripts/cmdlets to expose functionality

I have more experience with Python than PowerShell but the writing is on the wall as far as the Microsoft ecosystem is concerned: go with PowerShell. Otherwise, you’ll just be going against the grain and constantly interop-ing between Python and everyone else’s cmdlets.

Just because you can code import win32com.client in Python does not put it on equal footing with PowerShell in the Windows environment.

Answered By: JasDev

If all you do is spawning a lot of system specific programs with no or little programming logic behind then OS specific shell might be a better choice than a full general purpose programming language.

Answered By: jfs
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.