Is it possible to invoke-command with in a workflow?

4.5k views Asked by At

Do anyone know if I can use Invoke-Command in a PowerShell workflow?

Currently I have script that loops through a text file with the list of services but I would like it push to all of the servers at once verses going through one by one. Is this possible?

This is the current script block I am working with:

{
    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

There are 2 answers

0
AudioBubble On BEST ANSWER

The PowerShell Workflow engine is not capable of directly invoking PowerShell cmdlets. Instead, if a script writer calls a PowerShell cmdlet inside a Workflow definition, the PowerShell Workflow engine will automatically wrap invocations of PowerShell cmdlets inside the InlineScript Workflow Activity.

workflow test
{
  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..."

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

As for whether or not it will work, you'll have to try it out, as suggested by Mathias.

0
Sergey Nudnov On

@Trevor's response is good as an overall skeleton, but it won't work as it is. There are several things missing or incorrect:

  • Passing arguments to workflow
  • Passing arguments to InlineScript
  • Passing ScriptBlock as an argument;
  • Using Out-Null in workflow

The working example:

$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