locked
Exchange data inside\between PowerShell workflows RRS feed

  • Question

  • I have a problem with exchanging data between workflows and main script.

    Background:

    I have computer/server with 3 network interfaces:

    1. Wireless | Wi-Fi | Connected to 5GHz wifi
    2. Wireless | Wi-Fi 2 | Connected to 2.4GHz wifi
    3. LAN | LAN | Connected by lan cable

    I want to in PowerShell script that:

    • Start function which start first workflow
    • First workflow start parallel three instances of Second Workflow(Each for one interface)
    • Second workflow start two pings parallel(to gateway and do google) and reports results to one "db" which is at the and exported(append) to csv

    So i theory i should have 6 simultaneous tasks running. This I already achieve.

    Now I cant collect data :/

    Draft:

    workflow pings {
        Param(
            $ip,
            $interfa
        )
        $hosts = @(
            @{name='google';ip='8.8.8.8'}
            @{name='gw';ip='192.168.0.1'}
        )
        Foreach -parallel ($hosto in $hosts) {
            $ping1 = ping -S $ip $hosto.ip -n 1
            $sobju =   "$($($interfa.name).ToLower().Replace(" ",'').Replace('-',''))_status"
    
            if ($($ping1 | Select-String "Destination host unreachable").count){   <# $status.($sobju) = "Unreacheble" #>   }
            elseif ($($ping1 | Select-String "Lost = 1").count){  <# $status.($sobju) = "TimeOut" #>  }
            elseif ($($ping1 | Select-String "Lost = 0").count){                        
                $p1 = $($ping1 | Select-String "Average").ToString().IndexOf('Average')
                $p1a = $($ping1 | Select-String "Average").ToString().IndexOf('ms',$p1)
                $p1b = $p1a-$p1-10
                $sobjp =   "$($($interfa.name).ToLower().Replace(" ",'').Replace('-',''))_ping_$($hosto.name)"
                $sval = $($ping1 | Select-String "Average").ToString().Substring($p1+10,$p1b)
                <# $status.($sobjp) = $sval #>
            }
        }
    }
    
    Workflow InterfacesL {
        Param (
            $interfacesl
        )
        Foreach -parallel ($interf in $interfacesl) {
            if (($interf.name -eq "Wi-Fi") -or ($interf.name -eq "Wi-Fi 2") -or ($interf.name -eq "LAN")){
                If ($interf.MediaConnectState -eq "1"){ pings -ip $(Get-NetIPAddress -InterfaceAlias $interf.name -AddressFamily ipv4 |select ipaddress).ipaddress -interfa $interf }
                else{
                    $sobjd =   "$($($interf.name).ToLower().Replace(" ",'').Replace('-',''))_status"
                     <#  $status.($sobju) = "Interface Down"  #>
                }
            }
        }         
    }
    
    if ($status -ne $null){rv status}
    $status = [pscustomobject]@{date="";time="";wifi2_status="1";wifi2_ping_gw="2";wifi2_ping_google="3";wifi_status="4";wifi_ping_gw="5";wifi_ping_google="6";lan_status="7";lan_ping_gw="8";lan_ping_google="9";}
    $status.date = get-date -Format yyyy-MM-dd
    $status.time = Get-Date -Format HH:mm:ss
    $int = Get-NetAdapter -Physical
    InterfacesL($int)
    $status | export-csv -Path "C:\NetLogs\$($status.date).csv" -Delimiter ";" -Append

    I don't want to discus about sense of this, but I want to find how to do it :)

    Saturday, June 2, 2018 3:38 PM

All replies

  • Why two workflows.  A workflow is a parallel task and tasks can be sequenced.

    You cannot send data between workflows.  There is no such communication mechanism.

    Start be designing a workflow that does what you want using parallel tasks and sequences.


    \_(ツ)_/

    Saturday, June 2, 2018 3:51 PM
  • I found this clean solution(Hashtable sorting method included ;) ):

    workflow Y-Ping {
        sequence{
            $workflow:status = @{}
            $workflow:intcfg =  @{'Wi-Fi'='ssid_wifi_5GHz';'Wi-Fi 2'='ssid_wifi_2.4GHz'}
    
            $workflow:hosts = @(
                @{name='google';ip='8.8.8.8'}
                @{name='gw';ip='192.168.0.1'}
            )
    
            $workflow:status += @{date = get-date -Format yyyy-MM-dd}
            $workflow:status += @{time = Get-Date -Format HH:mm:ss}
            $workflow:interfaces = Get-NetAdapter -Physical
    
            Foreach -parallel ($interface in $workflow:interfaces) {
                $interface_key = "$($($interface.name).ToLower().Replace(" ",'').Replace('-',''))"
    
                if ((($interface.name -eq "Wi-Fi") -or ($interface.name -eq "Wi-Fi 2") -or ($interface.name -eq "LAN")) -and ($interface.MediaConnectState -eq "1")){
                    $workflow:status += @{"$($interface_key)_status" = 'Up'}
                    $interface_ip = $(Get-NetIPAddress -InterfaceAlias $interface.name -AddressFamily ipv4 |select ipaddress).ipaddress
    
                    Foreach -parallel ($yhost in $workflow:hosts){
                        $interface_key_ping = "$($interface_key)_ping_$($yhost.Name)"
                        $ping = ping -S $interface_ip $yhost.ip -n 1
    
                        if ($($ping | Select-String "Destination host unreachable").count){$workflow:status += @{$interface_key_ping = 'Unreacheble'}}
                        elseif ($($ping | Select-String "Lost = 1").count){$workflow:status += @{$interface_key_ping =  'Timeout'}}
                        elseif ($($ping | Select-String "Lost = 0").count){                        
                            $p1 = $($ping | Select-String "Average").ToString().IndexOf('Average')
                            $p1a = $($ping | Select-String "Average").ToString().IndexOf('ms',$p1)
                            $p1b = $p1a-$p1-10
                            $sval = $($ping | Select-String "Average").ToString().Substring($p1+10,$p1b)                       
                            $workflow:status += @{$interface_key_ping = $sval}
                        }                  
                    }
                }
                else{
                    if ($interface.name -ne 'lan'){
                        $workflow:status += @{"$($interface_key)_status" = 'ReConnecting'}
                        $workflow:status += @{"$($interface_key)_ping_gw" = '-'}
                        $workflow:status += @{"$($interface_key)_ping_google" = '-'}
                        netsh wlan connect interface="$($interface.name)" ssid="$($workflow:intcfg.($interface.name))" name="$($workflow:intcfg.($interface.name))"
                    }
                    else{
                        $workflow:status += @{"$($interface_key)_status" = 'Down'}
                        $workflow:status += @{"$($interface_key)_ping_gw" = '-'}
                        $workflow:status += @{"$($interface_key)_ping_google" = '-'}
                    }
                }
            }
            If(!(test-path 'C:\NetLog\')){New-Item -ItemType Directory -Force -Path 'C:\NetLogs\'}
            InlineScript {   
                $tmp1 = $using:status
                $tmp2 = $tmp1.GetEnumerator() | Sort-Object name
                $tmp3 = [ordered]@{}
                $tmp2.getenumerator() | Foreach { $tmp3 += @{$_.name = $_.value} }
                $tmp = [pscustomobject]$tmp3 | Select-Object -Property * -ExcludeProperty PSComputerName,PSSourceJobInstanceId,PSShowComputerName
                
                        
                $tmp
                $tmp | export-csv -Path "C:\NetLogs\$($tmp.date).csv" -Delimiter ";" -Append -NoTypeInformation
            }
        }
    }
    cls
    $tmpY = Y-Ping | Select-Object -Property * -ExcludeProperty PSComputerName,PSSourceJobInstanceId,PSShowComputerName
    $tmpY
    

    Saturday, June 2, 2018 8:25 PM