Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
JeffHicks

Avatar / Picture

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 35
Reply with quote  #1 
I'd like to ask us to remember that any posted code samples should not include aliases, unless you specifically explain that you are using an alias and what it is. I know we use aliases without thinking, but after you paste in your code, take a moment to replace the alias with the full command name.

There will be many people coming through this forum who started using PowerShell yesterday and we need to make the code examples as clear as we can.

__________________
Jeff Hicks Author ~ Trainer ~ Guru
Cloud and Datacenter Management MVP



0
mxrss

Still Checking the Forum Out
Registered:
Posts: 1
Reply with quote  #2 
or how about a block to just export the aliases and remove after they have been used?

- Mike

0
JeffHicks

Avatar / Picture

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 35
Reply with quote  #3 
Here is a function that is also part of my ISE add-on module which can be found on Github.

Function ConvertFrom-Alias {

Param (
    [Parameter(Position=0)]
    [ValidateNotNullorEmpty()]
    $Text=$psISE.CurrentFile.Editor.text
)

#make sure we are using the ISE
if ($host.name -match "ISE"){

#Turn the script into syntax tokens
Write-Verbose "Tokenizing"

#verify there are no syntax errors first by Tokenizing the script
$out=$null
$tokens = [System.Management.Automation.PSparser]::Tokenize($text, [ref]$out)

#if there are errors they will be directed to $out
if ($out) {
    #enumerate each parsing error in $out
    foreach ($problem in $out) {
        Write-Warning $problem.message
        Write-Warning "Line: $($problem.Token.Startline) at character: $($problem.token.StartColumn)"
    }
}
else {
    #if no errors then proceed to convert
    $tokens | Where-Object { $_.Type -eq 'Command'} |
    Sort-Object StartLine, StartColumn -Descending |
      ForEach-Object {
          #handle the ? by escaping it
          if($_.content -eq '?') {
            Write-Verbose "Found a ?"
            $result = Get-Command -name '`?' -CommandType Alias
          }
          else {
            $result = Get-Command -name $_.Content -CommandType Alias -ErrorAction SilentlyContinue
          }

          #check and see if Get-Command returned anything
          if($result) {
            #find each command and insert the corresponding command definition
            Write-Verbose "Replacing $($result.name) with $($result.definition)"
            $psISE.CurrentFile.Editor.Select($_.StartLine,$_.StartColumn,$_.EndLine,$_.EndColumn)
            $psISE.CurrentFile.Editor.InsertText($result.Definition)
          }
      } #foreach
 } #else $tokens exists and there were no parsing errors
} #if ISE
else {
    Write-Warning "You must be using the PowerShell ISE"
}

Write-Verbose "Finished"

} #end Function



Select the text in the ISE and run this function.

__________________
Jeff Hicks Author ~ Trainer ~ Guru
Cloud and Datacenter Management MVP



0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.