Using @splat on ScriptMethod arguments



  • 1. get-help formating output
    Is there a way to make this look cleaner? When doing a: get-help the-command -detail | more The description is written to the edge of the window then words get cut off and continue on the next line. Is there a way to format this so that words wont cut off and just start on the next line, similar to a text editor?
  • 2. Passing parameters to a PS script from VB 2005
    I need now to invoke a PS script with parameters from a VB 2005 program. I tried both versioned mentioned above: Shell("powershell.exe e:\test.ps1 -param1 pathA -param2 computerB ") Shell(""powershell.exe -command "& e:\test.ps1 -param1 pathA -param2 computerB """). Both are accepted syntactically, both execute the script but without passing the parameters. What am I doing wrong? Thank you very much, Dan
  • 3. DMO: List tabel names from SQL Server 2000 db
    Hi! I'm trying to create a script which will return all tables and field in a SQL Server 2000 database. I've tried: ----------------------------------- $strServer = "myServer" $strDatabase = "Northwind" $sql=New-Object -comobject "SQLDMO.SQLServer" $db =New-Object -comobject "SQLDMO.Database" $tbl=New-Object -comobject "SQLDMO.Table" # Log into SQL Server $sql.loginsecure=$true $sql.connect($strServer) # Assign Northwind database to variable. $db=$sql.Databases($strDatabase) ----------------------------------- I can log into SQL Server just fine. But the final line returns an error: "Method invocation failed because [System.___ComObject#{bunch_o_stuff} doesn't contain a method named 'Databases'." If I type $sql.Databases, PowerShell returns info on all 22 databases in that instance. But I cannot assign a database to a variable. Any help or hints would be greatly appreciated. -- TIA, DaveS
  • 4. Save settings? Profile? autoexec?!
    is there some file i can edit that will load at startup? much like autoexec.bat does for windows? basically i want to save any function i make. better yet why dont you MS guys just include a "save-state" or something along those lines that will keep any function, alias, variable, and what not stored for my next session? :) that would be hot :) Thanks Justin

Using @splat on ScriptMethod arguments

Postby RickB » Sun, 28 Sep 2008 09:01:09 GMT

This obviously doesn't work yet.

Are the only workarounds the same as those we use for functions in
Is there any plan to make this work?

PS > $s=new-object psobject
PS > add-member -in $s ScriptMethod test {param ($a,$b,$c);write-host
'$a = ' $a ', $b = ' $b ', $c = ' $c}
PS > $s.test(1,2,3)
$a =  1 , $b =  2 , $c =  3
PS > $l = 1,2,3
PS > $s.test($l)
$a =  1 2 3 , $b =  , $c =
PS > $s.test(@l)
Use '$l' instead of '@l' when referencing variables in expressions.
'@l' can only be used as an argument to a command.
At line:1 char:11
+ $s.test(@l <<<< )

Re: Using @splat on ScriptMethod arguments

Postby Hal Rottenberg » Sun, 28 Sep 2008 21:40:26 GMT

Well, all we know at this point are the plans for CTP2 and they certainly 
include the splat feature according to the readme and blog articles that came 
out when this version was released. Perhaps its already working in the post-CTP2 
code and we'll see it in CTP3.

Author, Tech Prosaic blog ( http://www.**--****.com/ )
Webmaster, Psi ( http://www.**--****.com/ )
Community Director,
Co-host, PowerScripting Podcast ( http://www.**--****.com/ )

Re: Using @splat on ScriptMethod arguments

Postby RickB » Tue, 30 Sep 2008 23:05:29 GMT

It's a terribly useful concept and I'm very happy that the operator is
even proposed much less being worked on.

It's easy to see that the current implementation of splat would solve
this problem perfectly well.

Thanks for giving me some hope!

Unfortunately I can't see how splat can ever work properly for
functions without a change to the way arguments are represented.

Specifically, I think that a property will need to be added to
[string] args that allow the splat operator to tell if a string was
originally quoted or not.

Without this knowledge, switches and other named arguments will never
be able to be handled properly.

I see splat as being quite useless for functions if it can't deal with
named args.  They are quite indespensible.  Take [-whatif] or [-debug]
for example.
This is the only part of passing arguments thru one function to
another that can't be worked around.
There is no way to tell the difference between (fcn -debug) and (fcn '-

I've reported this here.
A vote would sure help!


Similar Threads:

1.Passing Argument Names as arguments

I have a situation where I would like to pass cmdlet argument names as 
arguments to a PS script.

File TrackMsg.ps1

$ArgList = [string]::join(" ",$args)

$List   = Get-MessageTrackingLog -Server $Server1 $ArgList
$List += Get-MessageTrackingLog -Server $Server2 $ArgList

If I try this:
PS> ./TrackMsg.ps1 -Sender  XXXX@XXXXX.COM  -Start "2009/09/01" -End 

This fails as the arguments are passed to the cmdlet as a string and are not 
interpreted properly.

Any idea how I can accomplish this?

2.Variable arguments and argument types

I want to write a script which will process a variable number and
variable types of arguments - so I want to be able to call my
script/function like:

foo "some argument" "another argument" $a 6 $b "arg string"

...and be able to pull off the individual arguments (easy with $args)
but also get their type, so in the above example I'd want to know that
the argument types were:


It's easy to use the -is operator, but for the variables I'm only able
to find out the type the variable contains (i.e. not that the argument
was specified on the function call *as* a variable).

I can wrap the whole thing in single quotes and parse it myself but
that's (a) harder than I think it should be! and (b) unnatural from
the function call perspective.


1.  Can it be done in a function/script call?


2.  If not, can I do it in a cmdlet?

Thanks for any ideas all


3.Getting arguments from STDIN when command line arguments are missing

I am trying to convert a Perl script to Powershell, but have trouble 
translating the code that takes arguments (i.e. filenames to be processed) 
from stdin whenever they are not specified on the command line. E.g.

    perl filename1 filename2 filename3


    echo filename1 filename2 filename3 | perl

In Perl this argument parsing is handled by a neat one-liner.

    chomp(@ARGV = <STDIN>) unless @ARGV;

My best effort so far in Powershell is:

    if( !$args )
        $filenames = get-content
         $filenames = [string]::join("`n",$args);

but I am stopped by "get-content", which insist that I provide the filename 
path to an interactive prompt during script execution.

Perhaps I could avoid the prompt by specifing stdin as a argument to 
get-content, similarly to <STDIN> in Perl, but I don't know how to do this 
in Powershell? Is there a standard name for STDIN/STDOUT/STDERR in 
Powershell, similar to e.g. CON: in DOS?

Any suggestions on a way ahead on the above problem?

PS. I have noticed that Powershell.exe itself abides by the above convention 
with its "-command -" argument. The "-" here denotes that arguments are to 
be read from stdin. Maybe there is a way in Powershell to achieve 
this behaviour also in Powershell scripts? 

4.Schtasks: Using quotes in arguments

5.[PS] Using & to run a command with an argument in a foreach loop

I'm trying to run a command passing an argument which is contained in a 

> $computers | foreach { &$("nslookup") $_ }
'nslookup server1' is not recognized as a cmdlet, function, operable 
program, or script file.
At line:1 char:2
+ &$ <<<< ("nslookup server1")

But I can run this at the command line:
PS P:\> nslookup server1
Server:  dnsserver
Address:  #######

Name:    server1
Address:  #######

same (correct) result with 
> &$("nslookup") server1

This just leaves me at the nslookup prompt:
PS P:\> $computers | foreach { &$("nslookup") $_.tostring() }
Default Server:  dnsserver
Address:  #########


What am I doing wrong here?

6. Using & to run a command with an argument in a foreach loop

7. Schtasks: Using quotes in arguments

8. Re[2]: Using reinterept_cast Instead of Macros in Arguments for Connect

Return to powershell


Who is online

Users browsing this forum: No registered users and 46 guest