[Show all top banners]
Back to: Kurakani General Refresh page to view new replies
 powershell
[VIEWED 4236 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 200 days
Recommended Popular Threads Controvertial Threads
Conservative discussions
TPS Re-registration case still pending ..
TPS for Nepal likely to extend next week
सालीको चाक
ChatSansar.com Naya Nepal Chat
TPS for Venezuela is terminated, only 60 days extension for transition period
TPS to F1 Status.
TPS To F-1 COS
Venezuela TPS lawuit
tesla stock OMG !!
TPS Sakiyo Tara Case is in Court.
Got my F1 reinstatement approved within 3 months(was out of F1 for almost 2 years)
Homeland Security revokes temporary status for 532,000 Cubans, Haitians, Nicaraguans and Venezuelans
Has anyone here successfully reinstated to F-1 status after a year-long gap following a drop from F-1?
I hope all the fake Nepali refugee get deported
Court Hearing Approval.
Need Help of IT consultancies
Nepal TPS decision
Any input on remote jobs(IT related or Sales or Marketing)?
Supreme Court allows Trump to end TPS for Venezuelans
Looking for girl
Who is hottest nepali female?
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