简体   繁体   English

Powershell执行SQL脚本

[英]Powershell to execute SQL SCRIPTS

anyone know how to rewrite below code, so it can invoke sql script. 任何人都知道如何重写下面的代码,因此它可以调用sql脚本。 For instance, instead of put 'SELECT @@SERVERNAME AS ServerName' in the powershell script, I would like to put it into sql file. 例如,我不想将'SELECT @@ SERVERNAME AS ServerName'放在powershell脚本中,而是希望将其放入sql文件中。

$Path = "D:\AdminStuff\PowerShell\Password\Password.txt"
$uid = 'sa'
$pwd = Get-Content D:\AdminStuff\PowerShell\Password\Password.txt | ConvertTo-SecureString
$pwd.MakeReadOnly()


$creds = New-Object System.Data.SqlClient.SqlCredential($uid,$pwd)

$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Server=SLB-CLMFZ52;Database=master;"
$con.Credential = $creds
$con.Open()

$sql = "SELECT @@SERVERNAME AS ServerName"

$cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$con)
$rdr = $cmd.ExecuteReader()
while($rdr.Read())
{
 $rdr["ServerName"].ToString()
}


$con.Close()

像这样简单的事情呢:

$sql=get-content $filename

While you can use the SqlCredential , SqlConnection , and SqlCommand .NET classes as you show in your question, there are simpler alternatives. 正如您在问题中所显示的,虽然可以使用SqlCredentialSqlConnectionSqlCommand .NET类,但还有其他更简单的选择。 It is a lot less work, for example, to use the Invoke-Sqlcmd cmdlet instead. 例如,使用Invoke-Sqlcmd cmdlet可以减少很多工作。 Invoke-Sqlcmd is essentially the venerable sqlcmd utility with a PowerShell disguise. Invoke-Sqlcmd本质上是具有PowerShell伪装的古老sqlcmd实用程序。 Thus, assuming your query was in a file myScript.sql, you can just run this--the InputFile parameter provides the means to store your SQL in a separate file as you requested: 因此,假设您的查询是在一个文件myScript.sql,你可以只运行这一点- InputFile参数提供了手段来存储您的SQL在单独的文件按照您的要求:

$InvokeParams = @{
    Server = 'SLB-CLMFZ52'
    Database = 'Master'
    Username = 'sa'
    Password = 'xyz'
    InputFile = 'myScript.sql'
}
Invoke-SqlCmd @InvokeParams

Now the obvious problem with that, though, is the password must be in plain text. 现在,明显的问题是密码必须为纯文本。 (Of course, the password in the OP was in plain text in a file, as well. :-) Unfortunately Invoke-Sqlcmd does not work with PowerShell credential objects, which would have made it a lot more secure. (当然,OP中的密码也以纯文本格式存储在文件中。:-)不幸的是, Invoke-Sqlcmd不适用于PowerShell凭证对象,这会使它更加安全。 The only reasonable workaround to get some security is to use Windows Authentication instead of SQL authentication. 获得某种安全性的唯一合理的解决方法是使用Windows身份验证而不是SQL身份验证。 Then you could, for example, omit the username and password, and the query will be invoked with secure credentials: 然后,例如,您可以省略用户名和密码,并使用安全的凭据调用查询:

$InvokeParams = @{
    Server = 'SLB-CLMFZ52'
    Database = 'Master'
    InputFile = 'myScript.sql'
}
Invoke-SqlCmd @InvokeParams

To use the cmdlet just Import-Module sqlps . 要使用cmdlet,只需Import-Module sqlps For the basics of Invoke-SqlCmd take a look at TechNet and for a more in-depth treatment, including the vagaries of sqlps, see part 1 of my Practical PowerShell for SQL Server Developers and DBAs article. 有关Invoke-SqlCmd的基础知识,请访问TechNet,并进行更深入的处理(包括sqlps的变化),请参阅我的《 面向SQL Server开发人员的实用PowerShell》和DBA文章的第1部分。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM