Solved

Add to Powershell DataRow

Posted on 2013-01-17
4
737 Views
Last Modified: 2013-01-18
Just starting to teach myself PowerShell so any help is much appreciated.  I'm trying to figure out how to add to a DataRow type.  For instance if I had done something like this.

$Servers = Invoke-Sqlcmd -ServerInstance "LOCALHOST\DEFAULT" -Database "Database" -Query "SELECT * FROM Servers WHERE EnvironmentID = 1"

and I decided I wanted to add in additional servers to the $Servers variable.  I thought it might be something along the lines of

$Servers = $Servers +  Invoke-Sqlcmd -ServerInstance "LOCALHOST\DEFAULT" -Database "Database" -Query "SELECT * FROM Servers WHERE EnvironmentID = 2"

but that just throws errors.
0
Comment
Question by:tim_cs
4 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
ID: 38788458
Try by setting $Servers a array variable..
$Servers  = @()
$Servers +=  Invoke-Sqlcmd -ServerInstance "LOCALHOST\DEFAULT" -Database "Database" -Query "SELECT * FROM Servers WHERE EnvironmentID = 2"

Open in new window

0
 
LVL 7

Assisted Solution

by:flaphead_com
flaphead_com earned 75 total points
ID: 38788624
Subsun's code should work

or
$servers = @()

$tmpServers = Invoke-Sqlcmd -ServerInstance "LOCALHOST\DEFAULT" -Database "Database" -Query "SELECT * FROM Servers WHERE EnvironmentID = 1"

$servers  += $tmpServers

or change the query to get both environmentid's?

Invoke-Sqlcmd -ServerInstance "LOCALHOST\DEFAULT" -Database "Database" -Query "SELECT * FROM Servers WHERE EnvironmentID = 1 OR EnvironmentID = 2"
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 175 total points
ID: 38789362
You can also make sure you always get an array from the first query:
$servers = @(Invoke-Sqlcmd -ServerInstance "LOCALHOST\DEFAULT" -Database "Database" -Query "SELECT * FROM Servers WHERE EnvironmentID = 1")
$servers += Invoke-Sqlcmd -ServerInstance "LOCALHOST\DEFAULT" -Database "Database" -Query "SELECT * FROM Servers WHERE EnvironmentID = 2"

Open in new window

If you like, you can use the @(...) around the second query, too, to make it look like the first one.
0
 
LVL 15

Author Closing Comment

by:tim_cs
ID: 38793695
Thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now