方法调用失败,因为[System.String]不包含名为“ GetNetworkCredential”的方法

时间:2019-04-23 08:32:25

标签: powershell

我有一个PowerShell脚本,该脚本使用SQL Server数据库的用户名和密码创建一个XML文件。

脚本:

Get-Credential –Credential (Get-Credential) |
    Export-CliXml "$FileLocation\DatabaseCredentials-$SQLServer.xml"

此脚本有效的文件,但是当我尝试从XML文件读取用户名和密码时,出现以下错误:

Method invocation failed because [System.String] does not contain a method
named 'GetNetworkCredential'.
At line:18 char:1
+ $DatabasePassword = $Credentials.GetNetworkCredential().Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

脚本:

$Credentials = Import-CliXml '$FileLocation\DatabaseCredentials-$SQLServer.xml'

$DatabaseUserName = $Credentials.UserName
$DatabasePassword = $Credentials.GetNetworkCredential().Password

0 个答案:

没有答案