Unique items from an array in order

  • How to get the unique list of Scriptfiles in the order they were added, ie,

    File2

    File1

    $ScriptPath = "File2"

    $ServerList = ("ServerB", "ServerC")

    $DB = ("DB1", "DB2")

    $ScriptItems = @()

    foreach ($Server in $ServerList)

    {

    foreach ($Database in $DB)

    {

    $ScriptItem = New-Object System.Object

    $ScriptItem | Add-Member -type NoteProperty -name ScriptFile -Value $ScriptPath

    $ScriptItem | Add-Member -type NoteProperty -name Server -value $Server

    $ScriptItem | Add-Member -type NoteProperty -name Database -value $Database

    $ScriptItems += $ScriptItem

    }

    }

    $ScriptPath = "File1"

    $ServerList = ("ServerB", "ServerC")

    $DB = ("DB1", "DB2")

    foreach ($Server in $ServerList)

    {

    foreach ($Database in $DB)

    {

    $ScriptItem = New-Object System.Object

    $ScriptItem | Add-Member -type NoteProperty -name ScriptFile -Value $ScriptPath

    $ScriptItem | Add-Member -type NoteProperty -name Server -value $Server

    $ScriptItem | Add-Member -type NoteProperty -name Database -value $Database

    $ScriptItems += $ScriptItem

    }

    }

    $ScriptItems

    Thanks!

  • Just this I think:

    $ScriptItems | select Scriptfile -Unique

  • D'oh! Too easy.

    Thanks Gazareth!

  • Yeah, powershell's full of useful little things like that 🙂

    You can actually combine your two loops into one with another foreach:

    $ScriptPath = "File2", "File1"

    $ServerList = ("ServerB", "ServerC")

    $DB = ("DB1", "DB2")

    $ScriptItems = @()

    foreach ($sp in $ScriptPath)

    {

    foreach ($Server in $ServerList)

    {

    foreach ($Database in $DB)

    {

    $ScriptItem = New-Object System.Object

    $ScriptItem | Add-Member -type NoteProperty -name ScriptFile -Value $sp

    $ScriptItem | Add-Member -type NoteProperty -name Server -value $Server

    $ScriptItem | Add-Member -type NoteProperty -name Database -value $Database

    $ScriptItems += $ScriptItem

    }

    }

    }

    $ScriptItems | select ScriptFile -Unique

    Far as I know they will always come back in order they were added; to be absolutely sure you could add another NoteProperty with a timestamp:

    $ScriptItem | Add-Member -type NoteProperty -name Added -value (get-date -Format HH:mm:ss.fff)

    And then change the last line to:

    $ScriptItems | Sort-Object Added | select ScriptFile -Unique

  • Just a note, and strictly personal preference with a bit of just best practice...use of "Add-Member" is a PowerShell 1.0 way of doing it. In 2.0 and higher you can utilize passing in your properties with the New-Object cmdlet. So a version 2.0 of your script (with the additional foreach loop noted by Gazareth):

    $ScriptPath = "File2","File1"

    $ServerList = ("ServerB", "ServerC")

    $DB = ("DB1", "DB2")

    $ScriptItems = @()

    $props = [ordered]@{ScriptFile="";Server="";Database=""}

    foreach ($script in $ScriptPath) {

    foreach ($Server in $ServerList) {

    foreach ($Database in $DB) {

    $ScriptItem = New-Object PSObject -Property $props

    $ScriptItem.ScriptFile = $script

    $ScriptItem.Server = $Server

    $ScriptItem.Database = $Database

    $ScriptItems += $ScriptItem

    }

    }

    }

    $ScriptItems

    As is, this will output your information in the order the items are shown in each variable:

    If you want to change that you can use the "Sort-Object" cmdlet:

    $ScriptItems | Sort-Object -Property ScriptFile

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • I like that, a little easier on the eyes.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply