简体   繁体   English

无法使用PowerShell连接到数据库

[英]Can't connect to database using PowerShell

I'm having issues with sqlps in my PowerShell script. 我的PowerShell脚本中的sqlps有问题。 I'm trying to select rows from a table and save them to a .csv file. 我正在尝试从表中选择行并将其保存到.csv文件。

The query is 查询是

$TABLE="events"

Import-Module sqlps
$SQLquery='SELECT * FROM dbo.$TABLE'
$result=invoke-sqlcmd -query $SQLquery -HostName LOCALHOST -Password test -Username test
$result |export-csv c:\TEST.csv -notypeinformation

I get this error: 我收到此错误:

invoke-sqlcmd : Login failed for user 'test'.
At line:6 char:9
+ $result=invoke-sqlcmd -query $SQLquery -HostName LOCALHOST -Password  ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
    + FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management. 
   PowerShell.GetScriptCommand

invoke-sqlcmd : 
At line:6 char:9
+ $result=invoke-sqlcmd -query $SQLquery -HostName LOCALHOST -Password  ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Sqlcmd], ParserException
    + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Could anyone be able to help? 有人能帮忙吗?

So the main issue was the query needed to be in "" quotes not '' quotes and the server needs to be told to explicitly allow Microsoft account logins and SQL logins. 因此,主要问题是查询需要用“”引号引起,而不是用“”引起,并且需要告知服务器明确允许Microsoft帐户登录和SQL登录。 even if you have a user setup for sql login. 即使您有用于SQL登录的用户设置。 the option is under properties/security/server authentication. 该选项位于属性/安全性/服务器身份验证下。

Im leaving this answer here for if anyone ever runs into this beginner issue as i have. 我将这个答案留给我,如果有人像我一样遇到这个初学者问题。 Thanks for the help from the community to get this sorted so quickly 感谢社区的帮助,以便尽快完成此排序

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

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