PScredentials重载错误

时间:2017-08-29 12:51:31

标签: powershell

我使用invoke命令删除远程虚拟机上的服务。

我使用PScredentials在invoke命令中传递凭据。

我从XML文件中获取用户名和密码。

当我只用1个远程虚拟机运行脚本时没有问题。

当我试图在超过1个远程虚拟机上运行脚本时(使用foreach循环)我收到错误:

New-Object : Cannot find an overload for "PSCredential" and the argument 
count: "2".
At line:17 char:15
+ ... redential = New-Object System.Management.Automation.PSCredential($use 
...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [New-Object], 
MethodException
+ FullyQualifiedErrorId : 
ConstructorInvokedThrowException,Microsoft
.PowerShell.Commands.NewObjectCommand

我将问题分离到了一个小帖子:

[xml]$xml = Get-Content "PUT Your Path Here" 
$vmList = $xml.configuration.vmList



forEach ($vm in $vmList) {

$vmUser = $vm.vm.vmUser
$vmPass = $vm.vm.vmPass



#Creating PScredentials
$password = $vmPass | ConvertTo-SecureString -asPlainText -Force
$username = $vmUser
$credential = New-Object 
System.Management.Automation.PSCredential($username,$password)

Echo $credential

}

XML:

<?xml version="1.0"?>
<configuration>
    <vCenterServerName>Server IP</vCenterServerName>
    <vCenterUser>server user</vCenterUser>
    <vCenterPass>Server pass</vCenterPass>
    <localTempDirectory>Your Local Host</localTempDirectory>
    <vmList>
        <vm>
            <vmName>VMname</vmName>
            <vmIP>Put Server IP</vmIP>
            <vmUser>User</vmUser>
            <vmPass>pass</vmPass>
            <vmTargetFolder>Put Your Path</vmTargetFolder>
               </vm>
        <vm>
            <vmName>VMname</vmName>
            <vmIP>Put Server IP</vmIP>
            <vmUser>User</vmUser>
            <vmPass>pass</vmPass>
            <vmTargetFolder>Put Your Path</vmTargetFolder>
               </vm>
    </vmList>
</configuration>

1 个答案:

答案 0 :(得分:0)

我发现了问题...对于今后读取它的人来说,问题是我对xml文件的缩进。

我没有把它称为正确的地方。