Home > Powershell, Scripting, Toolbox > Powershell – Script of the Day – Menu-Plus

Powershell – Script of the Day – Menu-Plus

Yesterday, we created a simple Powershell menu

https://geekseat.wordpress.com/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
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: