简体   繁体   English

将 powershell 连接到 SQL Server

[英]Connecting powershell to SQL Server

I'm quite new to both SQL and Power shell , but I want to run a script which pulls data from a server within SQL - Any advice on where to start?我对SQLPower shell都很Power shell ,但我想运行一个脚本,该脚本从SQL的服务器中提取数据 - 有关从哪里开始的任何建议?

I tried using this as a starting point, but got no luck as it doesn't like my credentials even though they are correct我尝试使用它作为起点,但没有运气,因为它不喜欢我的凭据,即使它们是正确的

Test:测试:

SqlConnection -ServerName 'END-HDSQ02\DEV4' -DatabaseName 'tbl_cert_expiry' -Credential (Get-Credential)

EDIT: Since Powershell V2, you are required to manually load the necessary Snap-Ins;编辑:从 Powershell V2 开始,您需要手动加载必要的管理单元; Powershell - Invoke-Sqlcmd unable to run Powershell - Invoke-Sqlcmd 无法运行

Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100

This is what we use to query a SQL View;这是我们用来查询 SQL 视图的内容;

[string] $Server= "ServerName"
[string] $Database = "DatabaseName"
[string] $SQLQuery= $("SELECT * FROM schema.TableView order by column")


$data = Invoke-Sqlcmd -ServerInstance $server -Database $database -Username $username -Password $value1 -Query $SQLQuery

Obviously you'll need to pass the connecting user and password as well, but I've omitted them from my example.显然,您还需要传递连接用户和密码,但我在示例中省略了它们。

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

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