Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Add to Powershell DataRow

Posted on 2013-01-17
4
739 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 69

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

840 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