在Powershell工作流程中传递凭据

时间:2019-04-10 02:23:50

标签: powershell workflow

我有一个Powershell工作流程可以在服务器上远程运行,但是在工作流程中传递凭据时遇到了问题。密码应在执行时来自用户。

workflow Gethost{
    param(
        [string[]]$Servers
    )
   $credential = (Get-Credential)
    foreach -parallel ($server in $Servers) {
        $session = inlineScript{New-PSSession -ComputerName $using:server -Credential $using:credential} 
        $id = $session.id
        inlineScript{Invoke-Command -ComputerName $using:server -Credential $using:credential -Filepath C:\Checkhost.ps1}
        inlineScript {Exit-PSSession}
        inlineScript{Remove-PSSession -id $using:id}
    }
}

1 个答案:

答案 0 :(得分:0)

您不能在PowerShell工作流程中使用Get-Credential cmdlet。在工作流中只能使用少数有限的cmdlet集。您可以将凭证移到外部,然后使用参数传递该凭证。

workflow Gethost{
    param(
        [string[]]$Servers,
        [PSCredential]$Credential
    )       
    foreach -parallel ($server in $Servers) {
    $session = inlineScript{New-PSSession -ComputerName $using:server -Credential $using:credential} 
    $id = $session.id
    inlineScript{Invoke-Command -ComputerName $using:server -Credential $using:credential -Filepath C:\Checkhost.ps1}
        inlineScript {Exit-PSSession}
        inlineScript{Remove-PSSession -id $using:id}
    }
}

有关工作流中的限制,请参见here