Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

The splat operator

2009-09-26

I must admit that I am not completely up to date with PowerShell 2.0 (PS2), even though the CTP (community technology preview) has been out for a while. The first thing that you will notice, is that you are no longer bound to the old console window. PS2 comes with a graphical editor that separates runspaces in tabs, and each tab has three sections; a script editor, an output window and a direct window that executes what you type, in the same way that the console window did. Since this is a WPF application (as apposed to a console application), you can use the regular cut and paste features in Windows.

Some of the other new features includes the splat operator, remoting, Add-Type and ScriptCmdlets.

The splat operator allows you to pass multiple pairs of keys and values to one single parameter.

This is the basic idea of hash tables in PowerShell:

Here, I want a function that can take one parameter. That parameter will be a hash table. The only line in the function write outs the value that is assigned to the key “test”:

function splatMe($arg) { write-output $arg["test"] }

When that function is added to the runspace, I create a hash table using the splat operator (@):

$hello = @{"test"="1"; "tjo"="2"}

I can now call the splatMe function, passing $hello (my hash table) as a parameter like so:

splatMe $hello

The result will be “1”. I also have the option of creating the hash table on the fly:

splatMe @{"x"="hello"; "test"="Mr. Bean"; "z"="yepp!"}

The output will be “Mr. Bean”.

Remoting allows execution of cmdlets and scripts on remote machines.

Add-Type lets you compile and use fragments of code from any .NET language.

ScriptCmdlets lets you build new cmdlets using PowerShell script, as regular cmdlets had to be written in Visual Basic or C# and had to be compiled.

Categories: PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips