Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

SQL Query

$
0
0

Hi Guys.

So i have this Powershell SQL query that i have been building up. This script connects to two seperate SQL servers and performs a SQL query looking for example a userid ABC.

Right now at the end of the script i am running the two items below. Now if it finds them it just displays ABC and if there is no record then nothing is displayed.

$DSEU.Tables
$DSUS.Tables

Really what i would like is if ABC is found in the EU DB then display

User ABC is found in EU

If the user is not found then display

User ABC is not found in EU

Also the same for the US DB

Having not really worked with this before i am a little unsure how to do it

Below is the full script

----------

# iManage SQL Server & Database
$SQLServerEU = "EU-SQL" #use ServerInstance for named SQL instances!
$SQLDBNameEU = "EU"
$SQLServerEU = "US-SQL" #use ServerInstance for named SQL instances!
$SQLDBNameEU = "US"

# SQL Query & Connection
$SqlQuery = "select userid from mhgroup.docusers where userid = 'ABC'"
$SQLConn = New-Object System.Data.SqlClient.SqlConnection

$SQLConn.ConnectionString = "Server = $SQLServerEU; Database = $SQLDBNameEU; Integrated Security = True"
$SQLConn.ConnectionString = "Server = $SQLServerUS; Database = $SQLDBNameUS; Integrated Security = True"


$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SQLConn

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

#Data Set

$DSEU = New-Object System.Data.DataSet
$SqlAdapter.Fill($DSEU)

$DSUS = New-Object System.Data.DataSet
$SqlAdapter.Fill($DSUS)

$SQLConn.Close()

clear

=====
$DSEU.Tables
$DSUS.Tables

----------------------

Thanks in advance


Viewing all articles
Browse latest Browse all 2562

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>