Tag Archives: PowerShell

The PowerShell automation language

So you just typed something epic…

… and you want to save it for posterity.

You know what it’s like, you’re prototyping something at the PowerShell command line, and it’s getting a bit complicated. It’s the greatest one-liner in the history of PowerShell, nay! Scripting itself! You’ve been honing this for the last two hours. It’s got fifteen Cmdlets strung together in a giant pipeline, with selecting and sorting and all manner of wizardry. It’s pretty damn awesome.

And you think “Hey, I should be saving this for later re-use or something”

You could copy it out of the host with careful mouse drags, or you could take a screenshot, or you could laboriously re-type it. Except they’re all boooring. And anyway, this one-liner returns such a massive swathe of output that your last command has gone shooting off up the screen somewhere and you can’t find it just by scrolling. And you could always hit the up arrow and everything but who cares? That’s not cool. There has to be a cooler way.

Consider this

Get-History | Select-Object -Last 1 | Out-File History.ps1

Or, better

Get-History | Select-Object -Last 1 -Property CommandLine | Out-File History.ps1

Or, better still

(Get-History | Select-Object -Last 1).CommandLine | Out-File History.ps1

Or, even better than that

(h | select -Last 1).CommandLine | Out-Clipboard

Yes, you can easily access your command history from PowerShell. And just like any other object, you can filter, sort and select or pipe it off to other Cmdlets for consumption. And if you were particularly nerdy and not at all obsessive-compulsive, you could encapsulate this into a cmdlet called “Save-History”, alias that cmdlet to “sh” and put the whole lot into your profile so it’s available onload, and you could save your fiddlings around at the command line for ever and ever and ever. But no-one would be that nerdy, right?

Function Save-History
{
    (Get-History | Select-Object -Last 10).CommandLine | Out-File $home\History.ps1 -Append
}

Write-Host "sh -> Save-History is available"
Set-Alias -Name sh -description "sh -> Save-History" -Value Save-History

 

My current favourite cmdlet is…

Out-Clipboard

Yes, it’s a humble wee cmdlet, but so powerful. So awesome.

When someone emails me a request to, say, check their IAM account’s group membership, I just run the following

Get-IAMGroupForUser -username <namehere> | Out-Clipboard

And then ctrl+v into the email and WE’RE DONE HERE NOTHING MORE TO SEE

 

 

(Oh, I forgot to mention – this command is part of the Powershell Community Extensions. You do have those, right?)

The Domain Train

So anyway, Innovation Day was coming up at work. Every so often, we all take a day out from our normal sprints and get let loose on ideas.

This is usually aimed at developers and product guys rather than the ops side, so what was I to do?

Well, I figured I’d take the concept of a software release train and… make it real. Every time we deploy new code, I want a train to leave the station and do a little loop before shutting down until the next deployment.

So on the Saturday before Innovation Day, I went shopping, and came home with an Arduino experimenter’s kit, a relay driver, some bits and pieces of wire and a Hornby Caledonian Belle model railway set. And this is what I built (apologies for the portrait mode film, I’ll have a better one after demo day tomorrow)

Continue reading →

Powershell DSC to configure Octopus Deploy Tentacles? Check

I wanted to make sure of sharing this because it really saved my bacon this week. There’s a longer post in the pipeline, possibly over at Domain’s forthcoming Tech Blog, but here’s the meat.

For the last couple of sprints I’ve been working on an end-to-end CloudFormation/DSC solution to stand-up new load-balanced, auto-scaling, self-healing, IIS8.x clusters ready to host various websites and microservices.

End-to-end of course means everything. No-touch from running the initial New-CFStack call to, 20 minutes later or so, hitting the LB in a browser and seeing our app-code in production.

Among the many pre-requisites for this is a configured Octopus Deploy Tentacle, and that’s what I’ll chat about here for a moment.

Continue reading →

Want to learn PowerShell quickly, easily and without getting bored?

Try my two favourite PowerShell learning resources, courtesy of Microsoft Virtual Academy. They’re getting on a bit, but good content ages slowly.

Getting Started with Powershell 3.0 Jump Start

and

Advanced Tools and Scripting with Powershell 3.0 Jump Start

They feature Geoff Snover, the father of PowerShell, and MVP Jason Helmick in a genuinely warm and entertaining back-forth and are an exemplar for how technical content should be presented online. Even if you don’t much care about PowerShell, I still think you should have a look.

Supercharging your PowerShell Profile

ise $profile; Remember this command

One of the things that would separate a true ninja of the PowerShell world from a mere dabbler may be the intelligent use of Powershell’s Profile features to save work.

After all, the whole point of an automation language like PowerShell is to cut down on unnecessary work. I mean, who wants to type Import-Module a hundred times a week when they can type ipmo?

Indeed, who needs to type ipmo at all? And if you use your profiles smartly, you don’t have to.

Continue reading →