powershell - 凭据连接不起作用

标签 powershell load

我正在尝试在 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/

相关文章:

powershell - 从 PowerShell 运行 MSBuild

arrays - 如何通过sencha存储发布数组

javascript - 使用 AJAX 调用从页面加载特定元素并将它们附加到列表中?

azure .vhd 图像捕获/加载

NHibernate 将保存,但不会加载实体

javascript - .load 在 IE8 中不工作

function - Powershell变量被分配了一个函数的结果和我传递给函数的参数

c# - 如何调试NuGet包的install.ps1脚本

PowerShell 访问被拒绝。 PSRemotingTransportException +fullyQualifiedErrorId : PSSessionStateBroken

powershell - 如何在 Windows 2016 上使用 Powershell IISAdministration 模块为 IIS 网站设置需要 SSL