是否可以在工作流程中调用命令?

时间:2017-01-03 18:13:20

标签: windows powershell invoke-command

有人知道我是否可以在PowerShell工作流程中使用Invoke-Command

目前我的脚本循环遍历带有服务列表的文本文件,但我希望它能够一次又一次地推送到所有服务器。这可能吗?

这是我正在使用的当前脚本块:

{
    ForEach ($Server in $Servers) {
        Write-Host "Copying code to $Server..."

        If (!(Test-Path -path \\$Server\c$\Websites\Versions\v$version)) {
            New-Item \\$Server\c$\Websites\Versions\v$version -Type Directory | Out-Null
        }

        Copy-Item .\Packages\v$version\* \\$Server\c$\Websites\Versions\v$version -Force -Recurse

        Write-Host "Converting to application on $Server..."

        Invoke-Command -ComputerName $Server -ScriptBlock $Script -Argumentlist $Version | Out-Null
    }
}

2 个答案:

答案 0 :(得分:1)

PowerShell工作流引擎无法直接调用PowerShell cmdlet。相反,如果脚本编写器在工作流定义中调用PowerShell cmdlet,则PowerShell工作流引擎将自动在Dictionary<string, object>工作流活动中包装PowerShell cmdlet的调用。

var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
token.Payload["profilePicture"] = "http://url/user.jpg"
var handler = new JwtSecurityTokenHandler();
var jwt = handler.WriteToken(token);

至于它是否有效,你必须按照Mathias的建议进行试用。

答案 1 :(得分:0)

@Trevor的response可以很好地用作整体骨架,但它不能按原样工作。 有几件事丢失或不正确:

  • 将参数传递给工作流程
  • 将参数传递给InlineScript
  • 将ScriptBlock作为参数传递;
  • 在工作流程中使用空值

工作示例:

$serversProd=@"
server1
server2
server3
server4
"@-split'\r\n'

$reportScript = "report.script.ps1"


$generateReport = {
    param($reportScript)

    cd D:\Automations\ConnectivityCheck
    powershell -file $reportScript
}

workflow check-connectivity {
    Param ($servers, $actionBlock, $reportScript)

    # Prepare the results folder
    $resultsFolder = "D:\Automations\ConnectivityCheckResults"
    $unused1 = mkdir -Force $resultsFolder

    # Run on all servers in parallel
    foreach -parallel ($server in $servers) {
        # Upload script to the server
        $unused2 = mkdir -Force \\$server\D$\Automations\ConnectivityCheck
        cp -Force $reportScript \\$server\D$\Automations\ConnectivityCheck\

        "Starting on $server..."

        # Execute script on the server. It should contain Start-Transcript and Stop-Transcript commands
        # For example: 
        # $hostname = $(Get-Wmiobject -Class Win32_ComputerSystem).Name
        # $date = (Get-Date).ToString("yyyyMMdd")
        # Start-Transcript -path ".\$date.$hostname.connectivity.report.txt"
        # ...Code...
        # Stop-Transcript

        $results = InlineScript {
            $scriptBlock = [scriptblock]::Create($Using:actionBlock)
            Invoke-Command -computername $Using:server -ScriptBlock $scriptBlock -ArgumentList $Using:reportScript
        }

        # Download transcript file from the server
        $transcript = [regex]::Match($results,"Transcript started.+?file is \.\\([^\s]+)").groups[1].value
        "Completed on $server. Transcript file: $transcript"
        cp -Force \\$server\D$\Automations\ConnectivityCheck\$transcript $resultsFolder\
    }
}

cls
# Execute workflow
check-connectivity $serversProd $generateReport $reportScript