More on output…

It could probably be just yet another comment under “What you see is NOT what you get” post. But I’ve decided I would give it more space and make code more readable than it’s possible in comments.

As I said before: for me nice output is less important than data itself. If I can keep data and make it look nice – I’m more than OK with that. I spent most of my time in PowerShell window so nice output make me happy camper. Winking smile That’s why whenever I write a module I spent some extra time on it to add ps1xml files to it and decide how both Format-List and Format-Table should behave with objects I create. Obviously, objects also have some custom names, usually they have some pseudo-type tattoo rather than type wrote in C# and added to PowerShell with Add-Type.

Problem with more ad-hoc scripting (or scripting in general if module is not an option) in version 2 of PowerShell is that you can’t easily create this custom formatting on the fly. Building ps1xml file is overkill in such scenario, and sending strings to the pipe is something I would rather avoid if original data is something of a different nature (numeric, DateTime, etc.). So, what I would do?

Read more…

What you see is NOT what you get.

While grading scripts in advanced category event 4 I noticed that most people assume that it’s necessary to change number into string to format it nicely. The problem with this approach: once you changed number into string you kind of kill PowerShell ability to sort, filter and group objects based on properties that are numbers. If you ever decide to use it for something else than showing to the user – you will probably have to parse results and reverse whole process. And because it’s PowerShell that you use – you do not really have to. You can get both. As advanced scripter – you should know that already. Smile Let’s take a look at some practical examples, to make it more clear when problems start to show up. If I will sort two objects:

2,10 | foreach {            
    New-Object PSObject -Property @{            
        Name = "Foo $_"            
        Size = "$_ Bars"            
    }            
} | Sort-Object -Property Size            

… PowerShell will try to convince me, that 2 Bars are greater than 10. In world of strings that’s true. In world of numbers – not so much. To be honest – for me that’s huge flaw of that approach. It looks nice, but that’s not usually what I’m after. I want PowerShell to give me information first, look nice later. So what could one do?

Read more…

“Format-{0}”–f ‘List’

Today I would like to write few words about why you shouldn’t work too hard with creating output when PowerShell can do the work for you.

Scripting, as I see it, is a way of taking stuff that “is there” and force it to do my job in a way I want it to be done. If there is something that will do job for me – I will use it. If not – I will create it. So my role is just to build the bridges, not whole road next to the old one.

Obviously, at times you do not know that road already exists. That’s where stuff like Scripting Games help a lot. I for one learned few neat tricks already. But if you fail to see highway and claim to be advanced scripter – be warned, I won’t praise your rocky road that you’ve created next to it. To the point.

Read more…

Hey! Don’t *break* my pipe!

I’m still pretty overwhelmed with number of scripts this year in Scripting Games. While I’m on my “Mission Impossible” I noticed extensive use of break keyword. I commented on it a lot in Beginner 3, but would like to show you why I do not like this as a way of ending your functions and/ or scripts.

I mean, it’s perfectly fine if you use it with what I will show you today in mind, that is: if you want your function/ script to be destructive and break things when you see an error. But if your function/ script is only getting some info from my system and I won’t get full information (like in Advanced Event 2) without admin rights – why do you break things? Isn’t exit/ return enough in such a case…? Smile

Read more…

1… 2… 3… Testing…

Scripting Games are in more or less middle. They have been huge success  this year – much more scripts that last year, which is awesome! I was hoping I will be able to grade every script submitted, but at the moment it looks like mission impossible… Winking smile

Anyway one thing that I would like to highlight today is testing. Seen many entries where single typo made huge difference, including turning 5/4* script into 1* script. And because with PowerShell we got huge power, and with power comes responsibility – testing is more important than ever.
Read more…

Scripting Games 2011 – be prepared! :)

Only 12 days to Scripting Games 2011! Be prepared!

Why would I care?

If you like PowerShell, and like to learn, and like to have fun doing both – Scripting Games are there for you. If you are just starting – read awesome posts co-authored by Scripting Wife and try it with beginner division. If you are scripting a while already and feel that you can do more than solve simple tasks – there is division for you (us… :D) as well! If you are worried what good can you get from solving some fictional problems – take a look at last year’s scripting games. Do you see something that will never-ever happen to you? If you are admin you probably had such and/ or similar problems on your plate in the past. Wouldn’t it be great to have skills required to automate solution next time they will come? And trust me, knowing techniques when you need a prompt solution is good thing. Learning new things while your head is on fire is straight way to heart attack or something worse than that… 😉

Where do I start?

The best place to start is 2011 Scripting Games: All Links on One Page article on Hey, Scripting Guys! blog. You will get there from any other blog that is supporting Scripting Games. All you need is click on Mr Scripto. Suspect looks like that and is wanted alive:

2011 Scripting Games

Grab this badge here!

You will see all articles tagged for Scripting Games there. 🙂 I would suggest starting with study guide, and don’t miss any of ‘Scripting Wife’ articles. Even if you knew ‘all that’ already, it’s really fun to read.

But you see… I’m busy around here?

If you reading this you probably know that PowerShell is there to make your life easier. So… First of all you can make title of your PowerShell window remind you about days left till Scripting Games will start:

$Host.UI.RawUI.WindowTitle = "Only {0} days to Scripting Games!" -f (New-TimeSpan -End '4/4/11').Days

If you live in a PowerShell.exe you may want to have it your prompt, so you won’t miss it:

function prompt {            
<#
    Whatever you need goes here, eg:
#>            
    Write-Host "$pwd To SG: " -NoNewline            
# piece of code to produce info about days to SG with proper colour. 😉            
    $DaysToSG = (New-TimeSpan -End '4/4/11').Days            
    switch ($DaysToSG) {            
        { $_ -lt 3 } { $Fore = 'Red'; break }            
        { $_ -lt 7 } { $Fore = 'Yellow'; break }            
        { $_ -lt 12} { $Fore = 'Green'; break }            
        default { $Fore = 'Gray' }            
    }            
            
    Write-Host $DaysToSG -Fore $fore -NoNewline            
# and close the prompt in a way you want it to be closed, such as '> '            
    return ' > '            
}

ScriptingGames

As you can see mine is a bit different (that’s because I’m using Jaykuls prompt function) – but it tells me how much time left. I could use calendar for that too, but well… I prefer to see every day how this number decreases. The reason is simple – I can’t wait when the Scripting Games will start! 🙂