Re: [PS] - Help with becoming better



Thanks for the input Andrew. I will spend some time looking over.
Unfortunately it doesn't seem to work for me. Here are some examples of the
problems that I have with it:

If I give it ps for input, then I get back Get-Process which is of course
correct. If however I give it ps | ft * then I get back:

Get-Process |

When I would expect to get back:

Get-Process | Format-Table *

"Andrew Watt [MVP]" wrote:

Hi,

I think this does pretty much the same as your script. It feels a bit
more "Powershelly" to me. :)

Save the following as a script
#Script begins

function global:expand-aliases{
$aliases = (read-host "Enter a command").Split("|")
for ($i=0; $i -lt $aliases.length; $i++){
$aliases[$i] = $aliases[$i].Trim(" ")
$aliases[$i] = $(get-alias $aliases[$i]).definition
} # end of for loop

$output = ""
for ($i=0; $i -lt ($aliases.length-1); $i++)
{
$output += $aliases[$i]
$output += " | "
} # end of for loop
$output += $aliases[($aliases.length-1)]

write-host $output

}

#Script ends

Run
expand-aliases
from the command line after running the script.

I have left cmdlet names fully expanded, aiming for clarity rather
than brevity. Hopefully you should be able to figure it out.

Now the next step would be figuring out how to handle the parameters.
:)

Andrew Watt MVP

On Thu, 11 May 2006 09:06:02 -0700, ebgreen
<ebgreen@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Ok, so I am exploring the Power Shell and trying to become more efficient. I
can tell that I still think too much like a VBScripter (for one thing having
to put functions at the top of the file still annoys me). I do a lot of my
exploration at the command line and when I do I rely heavily on aliases. When
I write an actual script however, I would rather avoid the use of aliases as
a matter of habit to avoid misunderstandings when I or somone else reads the
code in 2 years and the aliases have changed by then. I know that the cmdlet
names could just have easily changed by then, but bear with me this is my
story not yours. :P Anyway to this end I wanted to create a function that
would expand the aliases in a string that I hand it. I have put the below
functions in my profile and it does what I want. What I am looking for here
is love, understanding, and for you to admit how yo...err I mean I would like
some constructive input on how I can make this more Power Shelly. I am sure
that it is not what someone that knows the shell well would have written. If
nothing else, it feels to me as if I'm just not getting all of the new
capabilities available to me. Thanks in advance for any help and input that
you can give me. Here is the code along with what it will do:

function Check-Word {
Param ([String]$word )
$result = $word | Get-Alias -ErrorAction SilentlyContinue
if ($result.Name -ne $nul) {
return $result.Definition + ' '
}
else{
return "$word "
}
}

function Check-Pipe {
Param ([String]$pipe )
$out = ""
ForEach ($word in $pipe.Split(" ")){
$out += Check-Word($word)
}
return $out.Trim()
}

function Expand-Alias {
Param ([String]$command = (read-host "Enter the command") )
$pipes = $command.Split("|")
[string[]]$out = @()
ForEach ($item in $pipes){
$out += Check-Pipe($item)
}
[string]::Join(" | ", $out)
}

PS(6)wps:\>Expand-Alias "ps | ft *"
Get-Process | Format-Table *

I realize that it is not foolproof and that it can be tripped up but for the
time being it is "good enough" for me.

.



Relevant Pages

  • Re: [PS] - Help with becoming better
    ... If I give it ps for input, then I get back Get-Process which is of course ... I think this does pretty much the same as your script. ... I write an actual script however, I would rather avoid the use of aliases as ...
    (microsoft.public.windows.server.scripting)
  • Re: [PS] - Help with becoming better
    ... I didn't take it to handling parameters, ... I think this does pretty much the same as your script. ... I write an actual script however, I would rather avoid the use of aliases as ...
    (microsoft.public.windows.server.scripting)
  • Re: [PS] - Help with becoming better
    ... If I give it ps for input, then I get back Get-Process which is of course ... I think this does pretty much the same as your script. ... exploration at the command line and when I do I rely heavily on aliases. ...
    (microsoft.public.windows.server.scripting)
  • Re: [PS] - Help with becoming better
    ... I think this does pretty much the same as your script. ... I write an actual script however, I would rather avoid the use of aliases as ... ForEach ){ ...
    (microsoft.public.windows.server.scripting)
  • Re: [opensuse] alias failure
    ... So it's just an accident that the following aliases all work as I ... It's uncommon for interactive shells to have any positional parameters ... Unlike the very limited capabilities of aliases, shell procedures ... I made a script with nothing but that in it, ...
    (SuSE)