Thursday, February 25, 2010

Changing the Powershell prompt

I wanted a way to change the powershell prompt, in particular to:
- Display the current path in the Shell window
- Change the prompt to "[LineNumber]:"
- Show the stcck level when "pushd" is used

Then a colleague showed me this.

Add the following function to your current profile - it's an override for the prompt method:

function prompt {

#the old script...

#function prompt { "$" }



# FIRST, make a note if there was an error in the previous command

$err = !$?



# Make sure Windows and .Net know where we are (they can only handle the FileSystem)

[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath

# Also, put the path in the title ... (don't restrict this to the FileSystem

$Host.UI.RawUI.WindowTitle = "> {0} ({1})" -f $pwd.Path,$pwd.Provider.Name



# Determine what nesting level we are at (if any)

$Nesting = "$([char]0xB7)" * $NestedPromptLevel



# Generate PUSHD(push-location) Stack level string

$Stack = "+" * (Get-Location -Stack).count



# my New-Script and Get-PerformanceHistory functions use history IDs

# So, put the ID of the command in, so we can get/invoke-history easier

# eg: "r 4" will re-run the command that has [4]: in the prompt

$nextCommandId = (Get-History -count 1).Id + 1

# Output prompt string

# If there's an error, set the prompt foreground to "Red", otherwise, "Yellow"

if($err) { $fg = "Red" } else { $fg = "Yellow" }

# Notice: no angle brackets, makes it easy to paste my buffer to the web

Write-Host "[${Nesting}${nextCommandId}${Stack}]:" -NoNewLine -Fore $fg



return " "

}
The easiest way to do this is:
1. In PS type "$profile" this will show you the location of the current active profile
2. Open that file in notepad and paste the function above into it
3. In PS type ".$profile" to reload the altered profile

Wednesday, February 17, 2010

A Powershell GREP

Here's a handy one for doing a GREP in PowerShell

Example 1
Get-ChildItem -include *.txt -recurse | Select-String "Hello"

Example 2
Get-ChildItem -include *.txt -recurse | Select-String "Hello" | Format-Table

Example 3
Get-ChildItem -include *.txt -recurse | Select-String "Hello" | Format-Table -Property Path, LineNumber, Line -Autosize

This gets a collection of all "txt" files in the current directory and its sub directories, passes it to the "Select-String" which opens the file and look for the pattern "Hello".

The Format-Table option allows you to display the results in a more readable format. You can also use "Format-List"

In example 3 I added the AutoSize property so the Path info wouldn't get truncated.

Tuesday, February 16, 2010

Using jQuery to add a templated row to a html table

I wanted a quick easy way of adding rows to a table - preferably by designing the layout in html and then copying to where i needed it and changing the elements values where needed. This took me longer to figure out than it should have - so I thought I'd share it.

The snippet copies a row from one table and inserts it to the target table.
<table id="dataTable">
</table>
<table id="templateTable" cellspacing="0" style="display: none;">
<tr id="templateRow">
<td id="nameRow" class="rowItem" width="25%">
<a></a>
</td>
<td id="positionRow" class="rowItem" width="60%">
</td>
<td id="numberRow" class="rowItem" width="15%">
</td>
</tr>
</table>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).click(function() {
addNewRow("mailto://johnny@here.com", "John Ryan", "Developer", "555 123456");
addNewRow("mailto://johnny@here.com", "Bill Smith", "Developer", "555 768648");
addNewRow("mailto://johnny@here.com", "Buck Rogers", "Developer", "555 675843");
addNewRow("mailto://johnny@here.com", "Frank Wlliams", "Developer", "555 675843");
});

// Copy the template row and insert it to the target table.
function addNewRow(link, name, position,number){
var clonedRow = $('#templateRow').clone(true);
$("#nameRow a", clonedRow).attr("href", link);
$("#nameRow a", clonedRow).html(name);
$("#positionRow", clonedRow).html(position);
$("#numberRow", clonedRow).html(number);
$(dataTable).append(clonedRow);
}
</script>