【问题标题】:PowerShell workflow Invoke-Command not working (Write-Host)PowerShell 工作流调用命令不起作用(写入主机)
【发布时间】:2019-07-22 22:33:58
【问题描述】:

目前正在尝试使用 PowerShell 工作流来处理我正在并行执行的一些远程服务器工作。

我没有运气尝试让Invoke-Command 登录到服务器以实际工作。它没有抛出错误,但也没有像 Write-Host 语句那样输出到控制台。没有看到任何可以帮助我浏览旧帖子的内容。

workflow test {
    Param([System.Management.Automation.PSCredential]$cred)

    InlineScript {
        Write-Host $using:cred
        Invoke-Command -Computer "server" -Credential $using:cred  -ScriptBlock { Write-Host "hello world" } }
    }
}

#Calling the function
test $cred

【问题讨论】:

  • 不要使用工作流来并行运行东西(你可以通过作业或运行空间来做到这一点)。工作流不仅比常规 PowerShell 慢得多,而且在普通 PowerShell 中也有subtle differences,b/c 工作流在不同的引擎中运行。当您知道自己在做什么时,工作流就会发挥作用,但不要仅仅因为它们而使用它们。
  • @AnsgarWiechers 谢谢!事实证明,这是一个更优雅的解决方案,我喜欢这样一个事实,它将所有内容都保留在 powershell 环境中。

标签: powershell remote-access workflow-foundation invoke-command powershell-workflow


【解决方案1】:

write-output 而不是 write-host 的作品。请注意,这也是并行运行的:

invoke-command computer1,computer2,computer3 { 'whatever' } 

顺便说一句,你的末尾有一个额外的大括号。

另一种方法:

workflow test {
  InlineScript {
    Write-Host 'hello world'
  }
}

test -pscomputername server -pscredential $cred

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多