#sqlps - 连接到服务器进行查询时出现问题

#sqlps - Problems connecting to the server to query

我正在尝试连接到远程托管的 server\instance。服务器也在我连接的域中(我可以使用 SQL 服务器身份验证和本地 SSMS 提供的凭据登录)

Import-Module SQLPS

$query = 'select GETDATE() as date'

$op = invoke-Sqlcmd  -HostName 'servername' -Database  'DBName' -Username 'UN' -Password 'PWD' -Query "$query"

$op | Format-Table

预期输出为

2016-02-12 06:54:26.410

但我得到的是

PS SQLSERVER:> Import-Module SQLPS
$query = 'select GETDATE() as date'
$op = invoke-Sqlcmd -HostName 'servername' -Database 'DBName' -Username 'UN' -Password 'Pwd' -Query "$query" #-IgnoreProviderContext
$op | Format-Table
WARNING: The names of some imported commands from the module 'SQLPS' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
invoke-Sqlcmd : Login failed for user 'UN'.
At line:3 char:7
+ $op = invoke-Sqlcmd -HostName 'servername' -Dat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
+ FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

invoke-Sqlcmd :
At line:3 char:7
+ $op = invoke-Sqlcmd -HostName 'servername' -Dat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException
+ FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

你们能帮我看看这是什么原因吗?

我是否必须在任何地方指出这是 SQL 服务器身份验证?

所以我猜您需要使用 ServerInstance 参数而不是 HostName 参数。

Import-Module SQLPS

$query = 'select GETDATE() as date'

$op = invoke-Sqlcmd -ServerInstance 'servername' -Database 'DBName' -Username 'UN' -Password 'PWD' -Query "$query"

$op | Format-Table