[英]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}
}
}
请参阅此处了解Workflow中的限制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.