在 powershell 工作流中传递凭据

Pass credentials in 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}
    }
}

您不能在 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