Powershell 凭证连接不存在';行不通

Powershell 凭证连接不存在';行不通,powershell,load,Powershell,Load,我正在尝试在powershell脚本中使用凭据连接。我试过: $credential = Get-Credential 及 但当我执行查询时,我得到以下消息: Invoke-SqlQuery -Query $Query -Server $SERVER -database $DataBase -credential $credential Exception calling "Open" with "0" argument(s): "Login failed for user '\sa'."

我正在尝试在powershell脚本中使用凭据连接。我试过:

$credential = Get-Credential

但当我执行查询时,我得到以下消息:

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
这就产生了错误

顺便说一下,我想知道如何加载此程序集:

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
尝试以下操作(PsSnapin:SqlServerCmdletSnapin100):


查看加载了哪些程序集的一个简单技巧是:CurrentDomain.GetAssemblys()|选择ManifestModule |排序ManifestModule另一个技巧是
[appdomain]::CurrentDomain.GetAssemblys()| Where Location | Foreach{$|.GetExportedTypes()}| Where Name-match PSCredential | Format Table Name,程序集-自动调整大小
找不到与参数名称“Username”匹配的参数。事实上,允许的参数是:Invoke SqlQuery[[-Query][-File][-parameters][-Server][-Database][-Credential][-IncludeRecordSetIndex][-includeRecordScont][-ConnectionTimeout][-ExecutionTimeout][WhatIf][Confirm][-UseTransation][这就是我想要实例化凭证的原因。
new-object [System.Management.Automation.PSCredential]
New-Object : Cannot find type [[System.Management.Automation.PSCredential]]: make sure the assembly containing this type is loaded.
$username = ConvertTo-SecureString username -AsPlainText -Force
$password = ConvertTo-SecureString pass!word -AsPlainText -Force
$cred = new-object management.automation.pscredential $username,$password
$pwd = read-host -AsSecureString -Prompt "Password"

Invoke-Sqlcmd -Query $Query -Serverinstance $SERVER -database $DataBase –Username “MyLogin” –Password $pwd