Solved

Add to Powershell DataRow

Posted on 2013-01-17
4
747 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
[X]
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
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 70

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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