在powershell工作流中传递凭据

问题描述 投票:0回答:1

我有一个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 workflow
1个回答
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}
    }
}

有关Workflow中的限制,请参阅here

© www.soinside.com 2019 - 2024. All rights reserved.