tags:

views:

814

answers:

4

I'm asking because PowerShell confuses me.

I've been trying to write some deployment scripts using PowerShell and I've been less than enthused by the result. I have a co-worker who loves PowerShell and defends it at every turn. Said co-worker claims PowerShell was never written to be a strong shell, but instead was written to:

a) Allow you to peek and poke at .NET assemblies on the command-line (why is this a reason for PowerShell to exist?)

b) To be hosted in .NET applications for automation, similar to DCOP in KDE and how Gnome is using CORBA.

c) to be treated as ".NET script" rather than as an actual shell (related to b).

I've always felt like Windows was missing a decent way to bang out automation scripts. cmd is too simplistic in many cases, and WSH is too obtuse (although the combination can be used successfully, I'm not a fan). When I first heard about PowerShell I felt like Windows was finally getting a decent shell that would be able to help with automation of many tasks, but recent experiences, and my co-worker, tell me otherwise.

To be clear, I don't take issue with the fact that it's built on .NET, or that it passes objects around rather than text (despite my Unix background :]), and I'm not arguing that PowerShell is useless, but from what I can see, it doesn't solve the problem I was hoping it would solve very well. As soon as you step outside of the .NET/Powershell world, things quit being nice and cozy for you.

So with all that out of the way, what problem did MS solve by creating PowerShell, or is it some political bastard child as I suspect? I've googled and haven't hit upon anything that sufficiently answered that for me, but the more citations the better.

+22  A: 

Powershell was actually built as several things: A mature and extensible automation platform and a modern administration shell.

The former is primarily used for the administration GUIs for Exchange and other server products of recent times. The GUI is just a wrapper around Powershell which does the heavy lifting behind (kinda like UNIX GUI programs come to be, as a wrapper for a commandline program).

Jeffrey Snover (Powershell inventor) elaborates a little on how Powershell was created with which goals and problems it should solve.

In my opinion, Powershell as a shell is clearly intended as a replacement for cmd (easy to see) and WSH (WSH didn't get much attention in recent years, even though it had similar concepts as .NET back in its day [one platform, multiple languages with ActiveScripting], but with .NET MS basically put that to rest and resurrection probably wasn't an option for them).

It unifies most aspects of Windows administration in common concepts and methods you only have to learn once. Also, the power in PS for me stems greatly from it passing around objects which might explain why you get into problems when you step out of the .NET/PS world where you only get a String[] from a command. But for many things you would call an external program for in cmd there is a cmdlet which will do it for you.

Joey
Thank you Johannes, that answered my question pretty well.
Fred
+6  A: 

As a developer, I can tell you I no longer have a bunch of ConsoleApplication42 projects laying around in a folder.

As a developer at a small company where I pretty much do all things IT (DBA, manipulate routers, pull call detail records from the switch, monitor and graph bandwidth for customers, etc...) I can tell you that PowerShell fills a sorely needed gap in Windows and the fact that it's built on .NET provides a seamless upgrade path when the PowerShell pipeline is too slow to handle millions of iterations or a more permanent, strongly typed implementation is needed.

Anyway, I guess the question is why are you switching to PowerShell if you don't have a pressing need? I mean it's good to learn it now since it's basically the new management interface for all things Microsoft. But if that doesn't affect you then don't bother if you don't think you're gaining anything.

EDIT (In response to comments below)

It sounds like you're trying to use the .NET Process class to launch an exe and redirect it's stdout so it can be read by the caller. I agree that is a pain in .NET but fortunately PowerShell does all this for you pretty simply. As for capturing the result and still writing it to the display, that's pretty simple too though the command isn't a very well-known one because it isn't used that often. Here's an example:

# I always find it easier to use aliases for external commands
Set-Alias csc C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe

# Create some source file
Set-Content test.cs @"
class Program {
    static void Main() {
        System.Console.WriteLine("Hello World");
    }
}
"@

# Call CSC.EXE
# the output of csc.exe is written to results.txt and piped
# to the host (or select-string if you prefer)
csc test.cs | Tee-Object -file results.txt

# Check for errors
if ($LASTEXITCODE) { 
    # this is where community extensions would come in
    # handy. powershell 2.0 also has a command to send
    # mail but in 1.0 you can grab one from poshcode.org
}
Josh Einstein
I'm involved in a project and it's getting to the point where automated builds, schema updating, deployment, etc needs to be done. I had chosen PowerShell because of my perception that it would be good at the task. My decisions are going to end up being company wide w/i the next year or two and it seems like PowerShell is being pushed heavily by MS.After having experienced some pain with PS, I'm toying with using Ruby or Perl instead (I *really* don't want to use cygwin).
Fred
Could you use MSBuild for that? I'm not too familiar with MSBuild other than the hand tweaking of csproj files that I've done to hook in ILMerg and what not. But it seems like any scripting language is going to be just as general as the rest. If it's build automation you're after there's plenty of tools that specialize in that.
Josh Einstein
Out of curiosity though, what pains have you run into. Perhaps I can help. I'm fairly active in the PowerShell newsgroup and on my blog so I try to help when I can. I found a lot of things frustrating about PowerShell at first but particularly with 2.0 CTP 3 I have pretty much decided that if it ever went away I would sit in my garage with the car running and let the engine purr me to sleep.
Josh Einstein
Some of it's nit picky: executing a console app acts differently than a win32 app, and I don't understand why it had to be that way.some of it is less nit picky: How do I call SVN, collect the output so I can mail it in the case of a failure, yet stream it to the current powershell window as a form of feedback, all the while avoiding a deadlock (it happened via the processstartinfo method)?I probably should also have mentioned that I'm using Powershell 1.0 w/o the community extensions :)
Fred
THANK YOU. I looked everywhere, Get-Command, google, etc, looking for that *exact* bit of functionality, and I somehow missed all of them. I had come to the conclusion that it didn't exist, adding to my confusion about why PowerShell had been created. I had even started looking into writing my own bit of functionality, thank goodness you posted this or I may have found myself on the dailyWTF :)
Fred
For what it's worth, you could do something similar without tee-object by doing: csc blah.cs | %{ add-content results.txt $_; $_ }In PowerShell there's usually a dozen ways to skin a cat.
Josh Einstein
That's a good example illustrating some of the problems I see with PS.That method works until you get outside of calling .Net/PS commands. Call SVN using that method and you'll find the file never gets created. Even when it does work (for things like ls/dir), you'll find the contents in the file are not what you saw on the screen. I understand why this is, but it would be *really* nice to be able to pipe objects through a command that converts the objects to a string array, displaying exactly what would be displayed by the formatters (and one that output a char stream as well)
Fred
From what I've read online, I'm not the only one to run into these problems. It makes me wonder if PS was not meant to be used outside of the .Net world unless someone had specifically written providers for it (as in the case of the new SQL Server). If so, the whole generic shell idea starts showing huge gaps, hence my question.
Fred
I don't use SVN so I can't say for sure what the problem is but if it takes stdin and writes to stdout it should work. As for the command you would like which puts objects out to strings -- yet another thing PowerShell already does. dir | out-string. I really don't see what fundamental issues you have with PowerShell but it's quickly becoming very popular and proficiency with it will become critical for any Windows-based IT work in the near future. So I'd recommend reading up some more on it and hopfully it will click.
Josh Einstein
Josh that's a strawman, criticism of PowerShell is not the same thing as having "fundamental issues". It's obvious you're a fan of PS, but I think strawmen coupled with prosletyzing may not be the best way to win people over. PS is a tool like any other, treating it as something more than a tool is silly, but then I'm venturing into strawman territory myself aren't I?Good day to you.
Fred
+2  A: 

If you're looking for something to automate some tasks like that, check out AutoIt

Zerofiz
AutoIt is amazing I had the same thought and would have posted it if you had not. +1
Copas
+2  A: 

IMHO the main advantage seems to be a sensible cut and paste in the command console.

Otherwise I use ActiveState perl for scripting on windows. Way more powerful than any windows shell script, and the OLE interface exposes the whole windows API in an easy to use way.

James Anderson
Right click to cut or paste in cmd its not ctrl+c and ctrl+v but pretty sensible.
Copas
Any links I can use to evaluate that approach James?
Fred