我正在尝试在 powershell 脚本中使用凭据连接。我尝试过:
$credential = Get-Credential
和
$credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "Domain")
但是当我执行查询时,我收到以下消息:
Invoke-SqlQuery -Query $Query -Server $SERVER -database $DataBase -credential $credential
Exception calling "Open" with "0" argument(s): "Login failed for user '\sa'."
\Modules\InvokeSqlQuery\InvokeSqlQuery.psm1:155 char:14
+ $cnn.Open <<<< ();
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
即使我使用正确的凭据,它也会失败。有人遇到过同样的问题吗? Invoke-SqlQuery 调用
$cnn = New-SqlConnection $Server $Database $Credential $ConnectionTimeout
这会产生错误。
顺便说一下,我想知道如何加载这个程序集:
new-object [System.Management.Automation.PSCredential]
New-Object : Cannot find type [[System.Management.Automation.PSCredential]]: make sure the assembly containing this type is loaded.
谢谢。
最佳答案
回答你的第二个问题:-),包含 PSCredential 的程序集已加载。它是 PowerShell 所需的程序集。试试这个:
$username = ConvertTo-SecureString username -AsPlainText -Force
$password = ConvertTo-SecureString pass!word -AsPlainText -Force
$cred = new-object management.automation.pscredential $username,$password
关于powershell - 凭据连接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9844188/