![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Converting scripts I posted this in reply to "directory and file date" discussion between Brandon and ebgreen. Couldn't see any post backs, so you might have missed it. Anyway, I took up the gauntlet and wrote this two functions to convert scripts containing command-let names to aliases and vice versa. This is the very starting working code and I'm sure you guys will find plenty of ways to improve it (and you sure welcomed to). Basically, the functions looks up for command-let names/aliases as a regex word either wrapped by a space (\s) or by the word boundary (\b). I didn't check them extensively, can you check it on your own scripts?. BTW, stumbled on a weird thing. Getting the alias for foreach-object is working fine PS C:\Scripts> get-alias -name "%" CommandType Name Definition ----------- ---- ---------- Alias % ForEach-Object Shouldnt this return only where-object? get-alias -name "?" -or- get-alias -name '?' It returns 4 differnet aliases CommandType Name Definition ----------- ---- ---------- Alias % ForEach-Object Alias ? Where-Object Alias h Get-History Alias r Invoke-History Only this return the expected PS C:\Scripts> get-alias | where {$_.name -eq "?"} CommandType Name Definition ----------- ---- ---------- Alias ? Where-Object ############################### Usage: Convert-CmdletToAlias <filepath.ps1> Convert-AliasToCmdlet <filepath.ps1> ############################### function Convert-CmdletToAlias{ param([string]$filepath) if(!(test-path $filepath)){ write-warning "Error: File doesnt exist"; break; } $src = [System.IO.File]::ReadAllText($filepath); $cmdlets = get-command | where {$_.CommandType -eq "Cmdlet"} | select name # where {$_.CommandType -eq "Cmdlet" -and $_.PSSnapIn.name -like "Microsoft.PowerShell*"} $cmdlets | foreach { $cmd = $_.name; if($src -match "\b$cmd\b") { $alias = @(get-alias | where {$_.definition -eq $cmd}); $src=$src -replace($cmd,$alias[0]); } } $src; } ############################## function Convert-AliasToCmdlet{ param([string]$filepath) if(!(test-path $filepath)) { write-warning "Error: File doesnt exist"; break; } #cls $src = [System.IO.File]::ReadAllText($filepath); $aliases = get-alias | select Name,Definition $aliases | foreach { trap{write-host "ERROR cant process alias: <$alias> -f red -b black"; continue} $alias = @($_)[0]; $regex=[regex]::escape($alias.name); # for some reason this is not working "\b" +$regex +"\b" if($src -match "\s" +$regex +"\s"){ $src=$src -replace($regex,$alias.definition); } } $src; } ############################## Shay http://scriptolog.blogspot.com |
My System Specs![]() |
| | #2 (permalink) | ||||||||||||
| Guest | Re: Converting scripts > BTW, stumbled on a weird thing. Getting the alias for foreach-object is working
mark is being treated as a wildcard, not a literal name. It is matching all the aliases with just one character. "get-alias -name '??' will match all aliases with two characters. If you escape the question mark, you will get what you expect: PSH$ get-alias -name '`?' CommandType Name Definition ----------- ---- ---------- Alias ? Where-Object - Jeff | ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Converting scripts Thanks Jeff Shay http://scriptolog.blogspot.com
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: Converting scripts When I was looking at this a while back http://richardsiddaway.spaces.live.c...3E96!543.entry I found that % can cause problems as it is also the symbol for modulo arithmetic -- Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty Blog: http://richardsiddaway.spaces.live.com/ PowerShell User Group: http://www.get-psuguk.org.uk "Shay Levi" wrote:
| ||||||||||||||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: Converting scripts "RichS" <RichS@xxxxxx> wrote in message news:27458183-7786-416B-95E1-1810B655B6DD@xxxxxx
is probably one reason why the PrimalScript folks implemented a parser. Their editor provides this feature - it is very slick. -- Keith | ||||||||||||
My System Specs![]() | |||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||
| Guest | Re: Converting scripts Thats what I effectively ended up doing. You need to break up each part of you pipeline and examine it for aliases -- Richard Siddaway Please note that all scripts are supplied "as is" and with no warranty Blog: http://richardsiddaway.spaces.live.com/ PowerShell User Group: http://www.get-psuguk.org.uk "Keith Hill" wrote:
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Converting FROM .wma and .wav TO .mp3 using WMC 11 | Bruce - USAOz Services | Vista music pictures video | 2 | 02-23-2008 12:11 AM |
| Converting MP4 to MP3 | AnnieH | Vista music pictures video | 0 | 12-13-2007 10:20 PM |
| Converting cda to mp3 | Jeff Ciaccio | Vista General | 2 | 11-24-2007 12:20 AM |
| Converting a .Wim To a ISO | MB14 | Vista General | 1 | 10-24-2007 11:42 PM |
| Converting to PDF | Donna | Vista General | 26 | 03-30-2007 02:11 PM |