fbpx Program: Interactively Filter Lists of Objects in Windows PowerShell | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Program: Interactively Filter Lists of Objects in Windows PowerShell

There are times when the WhereObject cmdlet is too powerful. In those situations, the CompareProperty script provides a much simpler alternative. There are also times when the WhereObject cmdlet is too simple—when expressing your selection logic as code is more cumbersome than selecting it manually. In those situations, an interactive filter can be much more effective.

yet in the book, so feel free to just consider it a neat script for now. To learn more about a part that you don’t yet understand, look it up in the table of contents or the index.

Example 24. SelectFilteredObject.ps1

############################################################################## ## ## SelectFilteredObject.ps1 ## ## Provides an interactive window to help you select complex sets of objects. ## To do this, it takes all the input from the pipeline, and presents it in a ## notepad window. Keep any lines that represent objects you want to pass ## down the pipeline, delete the rest, then save the file and exit notepad. ##

Example 24. SelectFilteredObject.ps1 (continued)

## The script then passes the original objects that you kept along the ## pipeline. ## ## Example: ## GetProcess | SelectFilteredObject | StopProcess WhatIf ## ##############################################################################

## PowerShell runs your "begin" script block before it passes you any of the ## items in the pipeline. begin {

## Create a temporary file $filename = [System.IO.Path]::GetTempFileName()

## Define a header in a "herestring" that explains how to interact with ## the file $header = @"

############################################################ ## Keep any lines that represent objects you want to pass ## down the pipeline, and delete the rest. ## ## Once you finish selecting objects, save this file and ## exit. ############################################################

"@

## Place the instructions into the file $header > $filename

## Initialize the variables that will hold our list of objects, and ## a counter to help us keep track of the objects coming down the ## pipeline $objectList = @() $counter = 0

}

## PowerShell runs your "process" script block for each item it passes down ## the pipeline. In this block, the "$_" variable represents the current ## pipeline object process {

## Add a line to the file, using PowerShell's format (f) operator. ## When provided the ouput of GetProcess, for example, these lines look ## like: ## 30: System.Diagnostics.Process (powershell) "{0}: {1}" f $counter,$_.ToString() >> $filename

## Add the object to the list of objects, and increment our counter. $objectList += $_ $counter++

}

Example 24. SelectFilteredObject.ps1 (continued)

## PowerShell runs your "end" script block once it completes passing all ## objects down the pipeline. end {

## Start notepad, then call the process's WaitForExit() method to ## pause the script until the user exits notepad. $processStartInfo = NewObject System.Diagnostics.ProcessStartInfo "notepad" $processStartInfo.Arguments = $filename $process = [System.Diagnostics.Process]::Start($processStartInfo) $process.WaitForExit()

## Go over each line of the file foreach($line in (GetContent $filename)) {

## Check if the line is of the special format: numbers, followed by ## a colon, followed by extra text. if($line match "^(\d+?):.*") {

## If it did match the format, then $matches[1] represents the ## number a counter into the list of objects we saved during ## the "process" section. ## So, we output that object from our list of saved objects. $objectList[$matches[1]]

} }

## Finally, clean up the temporary file. RemoveItem $filename }

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say