Solved

Add to Powershell DataRow

Posted on 2013-01-17
4
734 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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