Powershell – Script of the Day – Menu-Plus
Yesterday, we created a simple Powershell menu
http://www.get-virtual.info/2011/01/26/script-of-the-day-powershell-menu-select-list/
Today’s script is a feeder for the menu, that allows you use any filed of an object as your menu source and return a different field of it as your return value.
For Example:
the following query will give you a list of the filenames in the current directory, once you select an item, it will return that ‘LastWriteTime’ of that file. – Simple
PS:8 >menu-plus -object (gci) -displayfield "name" -menuTitle Please select a file -returnfield "LastWriteTime"
Function menu-plus ($object, $displayfield, $menuTitle, $returnfield) { <# .SYNOPSIS Feeder object to Menu Function to Enable return of different field in Object .DESCRIPTION Requires:Menu Function Creates a menu that allows return of a different object field. .PARAMETER $object - the array that we're sselecting from $Displayfield : Name of the field that will be displayed for selection $MenuTitle : The prompt to included at top of menu $Returnfield : Field to be returned - defauilts to all fields .EXAMPLE PS: >$a = gci PS: >$b=menu-plus -object $a -displayfield "name" -menuTitle Please select a file -returnfield "LastWriteTime" ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦ Please select a file ¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ CDB Compare Decom edit-table temp.csv PS:87 >$b.LastWriteTime 25 March 2010 15:20:53 #> $menulist = @() ForEach ($item in $object){$menulist += $item.$displayfield} $returnval = menu $menulist $menuTitle $returnfield $output = $object | where {$_.$displayfield -eq $returnval} | Select $returnfield return $output }