[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 2112 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 7 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
मन भित्र को पत्रै पत्र!
emergency donation needed
ढ्याउ गर्दा दसैँको खसी गनाउच
nrn citizenship
जाडो, बा र म……
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