[Show all top banners]

ph12352
Replies to this thread:

More by ph12352
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 powershell
[VIEWED 2110 TIMES]
SAVE! for ease of future access.
Posted on 04-24-17 11:11 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Can someone help me with a power shell skript to run to update username from database? Please read instructions
$ServerName = "_ServerName_"
$DatabaseName = "_DatabaseName_"
$Query = "SELECT * FROM Table WHERE Column = '"

#Timeout parameters
$QueryTimeout = 120
$ConnectionTimeout = 30

#Action of connecting to the Database and executing the query and returning results if there were any.
$conn=New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerName,$DatabaseName,$ConnectionTimeout
$conn.ConnectionString=$ConnectionString
$conn.Open()
$cmd=New-Object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
$ds.Tables


ServerInstance = "VCOM "
$Database = "VCOM "
$ConnectionTimeout = 30
$Query = "SELECT COUNT(*) FROM [VCOM_Comm].[dbo].[COMM_ConfigStore]"
$QueryTimeout = 120

$conn=new-object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout
$conn.ConnectionString=$ConnectionString
$conn.Open()
$cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
$ds.Tables

DATABASE queries to run from skript
[Takes username ram]
Use aba
SELECT * from [dbo].[Users] where UserDBMnkr LIKE '%ram%'

[Inactivates the user (Get the UserId from the previous query)
Use abc
UPDATE [dbo].[Users]
SET UserStat = 'I'
WHERE UserID= 142

Use abc
UPDATE [dbo].[Contact]
SET CntctStts = 'I'
WHERE CntctID= 161

 
Posted on 04-24-17 11:32 AM     [Snapshot: 17]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I would suggest you to use sqlps powershell module which can directly interact with SQL server instead of generic sqlclient.
You can run those command using invoke-sqlcmd.All those lines can be compressed to few lines.
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 365 days
Recommended Popular Threads Controvertial Threads
श्राद्द
TPS Re-registration
सेक्सी कविता - पार्ट २
What are your first memories of when Nepal Television Began?
पाप न साप घोप्टो पारि थाप !!
पुलिसनी संग - आज शनिवार - अन्तिम भाग
निगुरो थाहा छ ??
ChatSansar.com Naya Nepal Chat
TPS Re-registration case still pending ..
Lets play Antakshari...........
What Happened to Dual Citizenship Bill
Basnet or Basnyat ??
Sajha has turned into MAGATs nest
NRN card pros and cons?
मेरो अम्रिका यात्रा -२
Do nepalese really need TPS?
कता जादै छ नेपाली समाज ??
susta manasthiti lai ke bhanchan english ma?
कृष्ण नै अन्तिम सत्य
पुलिसनी संग - आज शुक्रवार - भाग २
Nas and The Bokas: Coming to a Night Club near you
राजदरबार हत्या काण्ड बारे....
Mr. Dipak Gyawali-ji Talk is Cheap. US sends $ 200 million to Nepal every year.
Harvard Nepali Students Association Blame Israel for hamas terrorist attacks
TPS Update : Jajarkot earthquake
is Rato Bangala school cheating?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters