Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell-获取凭据PScredential对象不同于新对象-TypeName“;系统、管理、自动化、PSCredential“;_Powershell - Fatal编程技术网

powershell-获取凭据PScredential对象不同于新对象-TypeName“;系统、管理、自动化、PSCredential“;

powershell-获取凭据PScredential对象不同于新对象-TypeName“;系统、管理、自动化、PSCredential“;,powershell,Powershell,我正在调用使用PowerShell的ntlm身份验证的web api。 脚本使用get credential获取用户凭据。它很好用。代码如下 $cred_getcred_approach= get-credential $response = Invoke-RestMethod $uri -Method 'POST' -Headers $headers -Body $body -Credential $cred_getcred_approach 为了避免凭证提示,我创建了一个具有相同用户ID和

我正在调用使用PowerShell的ntlm身份验证的web api。 脚本使用get credential获取用户凭据。它很好用。代码如下

$cred_getcred_approach= get-credential
$response = Invoke-RestMethod $uri -Method 'POST' -Headers $headers -Body $body -Credential $cred_getcred_approach
为了避免凭证提示,我创建了一个具有相同用户ID和密码的PSCredential对象(我在get credential提示中填写了该用户ID和密码)

在这种情况下,身份验证失败。对这种行为感到惊讶,我使用下面的代码将这两个PSCredential对象导出到xml文件

$cred_getcred_approach | Export-CliXml -Path "c:\path\cred_getcred_approach.xml" 
$cred_createcred_approach | Export-CliXml -Path "c:\path\cred_createcred_approach.xml"
密码值在这两个xml文件之间不匹配。我尝试导入xml文件并调用web api

$cred_getcred_approach= Import-CliXml -Path "c:\path\cred_getcred_approach.xml" 
$cred_createcred_approach= Import-CliXml -Path "c:\path\cred_createcred_approach.xml" 
正如所料,当我在第一个案例中导入时,身份验证成功。(来自从get credential PSCredential对象导出的xml(cred_getcred_approach.xml),但在另一个案例中失败。原因可能是什么?安全字符串算法中是否存在任何差异?是否有任何方法修复此问题

更新


安全字符串有问题。下面的代码修复了它

$pass = ConvertTo-SecureString -AsPlainText "P@sSwOrd" -Force
$pass = ConvertTo-SecureString -AsPlainText "P@sSwOrd" -Force
我从中得到了原始的有问题的代码。下面是有问题的代码

$pass = ConvertTo-SecureString -String "P@sSwOrd" -AsPlainText -Force
$pass = ConvertTo-SecureString -String "P@sSwOrd" -AsPlainText -Force

感谢@tomalak的指针。

安全字符串有问题。下面的代码修复了它

$pass = ConvertTo-SecureString -AsPlainText "P@sSwOrd" -Force
$pass = ConvertTo-SecureString -AsPlainText "P@sSwOrd" -Force
我从中得到了原始的有问题的代码。下面是有问题的代码

$pass = ConvertTo-SecureString -String "P@sSwOrd" -AsPlainText -Force
$pass = ConvertTo-SecureString -String "P@sSwOrd" -AsPlainText -Force

感谢@tomalak提供的指针。

您可以使用
调用WebRequest-UseDefaultCredentials
来使用集成身份验证,即使用运行脚本的帐户的凭据。这样,您根本不必依赖于获取或保存任何显式凭据。>使用运行脚本的帐户的凭据