http://powershell.com/cs/blogs/donjones/archive/2011/12/30/welcome-to-2012-now-let-s-get-some-work-done.aspxAs Don noted,
So, one of the things I enjoy doing is creating short videos to help with very specific PowerShell techniques. You can find 'em all at http://youtube.com/concentrateddon. But now it's 2012... and I need some ideas for more!One video in particular caught my attention,
Drop a comment on this post and let me know what you'd like to see a video of. There's only one rule: It has to be "core" PowerShell. That means no Exchange, no SharePoint, no SQL Server... I know those things are valuable, but my personal passion is the core, native, foundation PowerShell stuff that most folks never get exposed to.
So if there's something you find confusing... difficult... complex... or whatever... about PowerShell itself, let me know and I'll work up a video in the upcoming weeks.
Splatting (from TechNet Magazine, March 2011)As I started watching the video splatting really started to make sense. I had read the term and gotten a vague understanding of how it worked. To help make it clear here, I'll refer to Don's explanation of what splatting is so we are all on the same page,
splatting is a way of bundling parameters to send to a command.Yet, as Don explained and demonstrated how splatting worked I finally had the light bulb go off. The example Don used in this video demonstrated how this command,
Get-WmiObject -Class win32_LogicalDisk -ComputerName SERVER-R2 -Filter 'DriveType=3' -credential Administratorcan be effectively accomplished in a more elegant way by string the parameters and arguments into a hashtable object, then, passing the hashtable object to the cmdlet instead of the parameter/argument pairs. The video corresponds with this article,Windows PowerShell: SplattingAs noted in the video and the article, the splatting technique allows for more well-organized code:
$parms = @{'class'='Win32_BIOS';
'computername'='SERVER-R2';
'filter'='drivetype=3';
'credential'='Administrator'
}
Get-WmiObject @parmsWe can see a similar example in mjolinor's script outlined on this thread,How can I get the file count in a zipped fileIn the function definition below I have highlighted, and, changed the font color to red where I have a hashtable to define the splat, and, the usage,
function get-zipcontent {
begin {
$regex = "\s*(\d+)\s+(\w+)\s+(\d+)\s+(\d{1,2})% \s+(\S+\s+\S+)\s+(\w+)\s+(\S+)\s+(\S+)\s*$"
$props = {
@{
Name = $matches[8]
Date = $matches[5] -as [datetime]
Length = $matches[1] -as [int]
Method = $matches[2]
Size = $matches[3] -as [int]
Ratio = $matches[4] -as [int]
CRC32 = $matches[6]
Attr = $matches[7]
}
}
$proplist = $props.tostring().split(“`n”) -match “^\s*(\S+)\s*=.+$” -replace “^\s*(\S+)\s*=.+$”,’$1'
}
process {
if ($_ -match $regex){
new-object psobject -property (&$props) | select $proplist
}
}
}As you can see, the $props object contains the object definition as a hashtable variable, and, the New-Object cmdlet consumes it with (&$props).Below is another example from the Scripting Guys post,
Use Splatting to Simplify your Powershell ScriptsIn this post, they outline,
$MailMessage = @{
To = "me@mycompany.com"
From = "me@mycompany.com"
Subject = "Hi"
Body = "Hello"
Smtpserver = "smtphost"
ErrorAction = "SilentlyContinue"
}
Send-MailMessage @MailMessageIf you want to take this to the next level, you can start looking at this sort of example courtesy of Powershell.com,function Get-BIOS {
param(
$computername,
$credential
)
Get-WmiObject -Class Win32_BIOS @psboundparameters
}
0 comments:
Post a Comment