Solved

Add to Powershell DataRow

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

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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