The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

soostibi (11) [Avatar] Offline
#1
Hi,

I'm just playing with PS and created a "repeat-commandline" function, that repeats the previous commands in a pipeline expression at a given number of times:

function repeat-commandline ([int] $x = 1)
{
begin
{
$exprs = $myinvocation.line.Split("|")
if($exprs.count -lt 2) {throw "Nothing to repeat"}
$ofs="|"
$cropped = [string] $exprs[0..($exprs.count-2)]
}
end
{
for ($r = 0; $r -lt $x; $r++)
{
invoke-expression $cropped
}
}
}

For example this produces the following:
[84] PS C:old>"Text" | repeat-commandline 3
Text
Text
Text

This works fine until I use the pipe symbol between qoutes. Is there a .NET static method somewhere that helps parse a PS expression? Or I must use [regex]...