Using @splat on ScriptMethod arguments
by 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
ps1?
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
by 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, PowerShellCommunity.org
Co-host, PowerScripting Podcast ( http://www.**--****.com/ )
Re: Using @splat on ScriptMethod arguments
by 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 '-
debug').
I've reported this here.
A vote would sure help!
connect.microsoft.com/feedback/ViewFeedback.aspx?
FeedbackID=368512&SiteID=99
Thanks
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)
$Server1="EXCHHUB01"
$Server2="EXCHHUB02"
$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
"2009/09/23"
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:
String
String
Variable
Literal
Variable
String
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.
So:
1. Can it be done in a function/script call?
or
2. If not, can I do it in a cmdlet?
Thanks for any ideas all
Chris
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 script.pl filename1 filename2 filename3
equals
echo filename1 filename2 filename3 | perl script.pl
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
}
else
{
$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 built.in 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
variable:
> $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