[Show all top banners]
Back to: Kurakani General Refresh page to view new replies
 powershell
[VIEWED 4022 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
TPS Re-registration case still pending ..
ChatSansar.com Naya Nepal Chat
Conservative discussions
सालीको चाक
tesla stock OMG !!
Looking for girl
Who is hottest nepali female?
TPS for Nepal likely to extend next week
I hope all the fake Nepali refugee get deported
ढ्याउ गर्दा दसैँको खसी गनाउच
Those who are in TPS, what’s your backup plan?
Venezuela TPS lawuit
interesting fact about sajha.
Homeland Security revokes temporary status for 532,000 Cubans, Haitians, Nicaraguans and Venezuelans
Embassy of Nepal might be able to help extend TPS for Nepal
TPS for Venezuela is terminated, only 60 days extension for transition period
महँगो अण्डाको पिकल्प : कुखुरा र खोर भाडामा लिने
To Sajha admin
If you got nothing to lose, fight tooth and nail explore every avenue
ICE kidnapping people off the streets over op eds
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