August 10, 2015 at 12:50 pm
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!
August 12, 2015 at 6:48 am
Just this I think:
$ScriptItems | select Scriptfile -Unique
August 12, 2015 at 6:52 am
D'oh! Too easy.
Thanks Gazareth!
August 12, 2015 at 7:24 am
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
August 23, 2015 at 2:00 am
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
August 24, 2015 at 4:57 am
I like that, a little easier on the eyes.
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy