简体   繁体   English

在Powershell脚本中执行sql命令

[英]Executing sql command in powershell script

I want to execute simple sql commands that I do on command by directly entering, but I want to do that using Powershell script, don't want to do the commands using powershell API but by just invoking command 我想通过直接输入来执行我在命令上执行的简单sql命令,但是我想使用Powershell脚本来执行此操作,不想使用Powershell API来执行命令,而只是调用命令

sqlcmd -S .\SQLEXPRESS -U SA -P mypass
USE [master]
RESTORE DATABASE [easypay] FROM  DISK = N'C:\backup.trn' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 5
ALTER LOGIN [itdb] WITH PASSWORD=N'abc'
GO
USE [easypay]
GO
EXEC sp_change_users_login 'Auto_Fix', 'itdb'
Go
USE [easypay]
GO
sp_changedbowner 'sa'
GO

UPDATE itdb.passwd set password = '1a1dc91c907325c69271ddf0c944bc72'  WHERE itdb.passwd.name = 'abc'
GO

I want a powershell script that can execute these commands direclty Thanks 我想要一个可以执行这些命令的Powershell脚本,非常感谢

Here is a script which I use, little bit modified. 这是我使用的脚本,做了一些修改。 Note this code is untested: 请注意,此代码未经测试:

Made some changes after the comment from Kirill Pashkov 在Kirill Pashkov发表评论后进行了一些更改

$SQLServer = "" #your value
$SQLDBName = "" #your value

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
#Start SQL connection
$SqlCmd.Connection = $SqlConnection

#Create querys
#Query 1
$SqlQuery = "Command;"

#Query 2
$SqlQuery2 = @"
 Command
"@

#Add querys to array
$querys = $SqlQuery, $SqlQuery2

# Run Query
Foreach($query in $querys)
{
    $SqlCmd.CommandText = $query
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $OutPut = $SqlAdapter.Fill($DataSet)
    if($OutPut -ne 0){
    Write-Host $OutPut
    }

}
#Close SQL connection
$SqlConnection.Close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell脚本中的sql命令 - sql command in powershell script 在PowerShell中通过SMO执行SQL脚本时出错 - Error executing SQL script via SMO in PowerShell PowerShell 不从 SQL 服务器执行脚本 - PowerShell not executing script from SQL server 从 PowerShell 命令执行 SQL 时如何参数化登录 - How to parameterize logins when executing SQL from PowerShell command 使用EntityManager执行SQL命令 - Executing an SQL command with EntityManager SQL Server:即使报告成功,SQL Server 代理也不执行 PowerShell 脚本 - SQL Server: SQL Server Agent not executing PowerShell script even though a success is reported 使用 xp_cmdshell 从 T-SQL 脚本执行 SQLPS 或 PowerShell 时遇到问题 - Trouble executing SQLPS or PowerShell from T-SQL script using xp_cmdshell 通过SQL Server代理作业使用WinSCP执行REMOTE Powershell脚本时出现问题 - Problem executing REMOTE powershell script using WinSCP via SQL Server Agent Job 1 Powershell脚本2 SQL表 - 1 Powershell Script 2 SQL Tables Powershell Script using Invoke-SQL command,needed for SQL job, the SQL Server version of Powershell is somewhat crippled, is there a workaround? - Powershell Script using Invoke-SQL command,needed for SQL job, the SQL Server version of Powershell is somewhat crippled, is there a workaround?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM