locked
How to store values to WorkFlow Foreach -Parallel variable, from variable inside on an inlineScript RRS feed

  • Question

  • Good Morning,

    I'm trying to show the success and failures in the following script:

    workflow Test_HTTPS_Port {
    
    $servers = Import-Csv -Path C:\users\myuser\servers.csv
    $success
    $failed
    foreach -parallel -ThrottleLimit 32 ($server in $servers)
    {
        inlinescript{
        $tcp = New-Object System.Net.Sockets.TcpClient    
        try
        {
            $tcp.connect("$using:server", 443)        
            Write-Output "HTTPS Connection successful to $using:server"
            $using:success += 1     
        }
        catch
        {
            Write-Output "HTTPS Connection Failed to $using:server"
    
            $using:failed += 1       
        }
        finally
        {
            $tcp.Dispose()        
        }          
        }    
    }
    Write-Output "Successful: $success[0]"
    Write-Output "Failed: $failed"
    }
    
    testhttps

    But for the scope I can't, because the inlinescript run in a different proccess than the workflow. You can do something like this "$a = inlinescript{$b = $using:myVariable + 1; $b}", so the $a variable can store the value of the variable inside the script, and now we can see the value of the variable outside the inlinescript. But now is my problem due to the structure of the script; I can't do the previous technique.

    Could you help me to know, how can I do to see the count of success and failures?

    Thanks a lot Guys.

    Monday, February 8, 2016 9:02 PM

Answers

  • Just return the value.

    $success+=inlinescript{ ....


    \_(ツ)_/

    • Marked as answer by JRLOPS Tuesday, February 9, 2016 3:39 AM
    Tuesday, February 9, 2016 12:36 AM
  • Other method:

    workflow test{
    	$tester=@{success=33;failed=23}
    	inlinescript{
    		$x=$using:tester
    		$x.success+=1
    		$x
    	}
    }
    test
    
    


    \_(ツ)_/

    • Marked as answer by JRLOPS Tuesday, February 9, 2016 3:39 AM
    Tuesday, February 9, 2016 3:23 AM

All replies

  • Just return the value.

    $success+=inlinescript{ ....


    \_(ツ)_/

    • Marked as answer by JRLOPS Tuesday, February 9, 2016 3:39 AM
    Tuesday, February 9, 2016 12:36 AM
  • Hi jrv,

    Thanks for your answer it worked, but you can only count one of the two options, you can count the succes or you count the failures, but you can't count both and output them. following is the modification:

    workflow Test_HTTPS_Port {
    
    $servers = Import-Csv -Path C:\users\myuser\servers.csv
    $success = $null
    $failed = $null
    foreach -parallel -ThrottleLimit 32 ($server in $servers)
    {
        $WORKFLOW:success += inlinescript{
        $tcp = New-Object System.Net.Sockets.TcpClient    
        try
        {
            $tcp.connect("$using:server", 443)        
            1     
        }
        catch
        {
            0       
        }
        finally
        {
            $tcp.Dispose()        
        }          
        }    
    }
    Write-Output "Successful: $success"
    Write-Output "Failed: $failed"
    }
    
    testhttps
    How do you think I could do it, so the workflow output can output the two counts of event (Success and Failures)?


    • Edited by JRLOPS Tuesday, February 9, 2016 2:58 AM
    Tuesday, February 9, 2016 2:53 AM
  • Return and array?

    \_(ツ)_/

    Tuesday, February 9, 2016 3:06 AM
  • Not an array, i.e $success += inlinescript{......} and $failed += inlinescript{.......}, so at the end I can see both the success and the failures.
    Tuesday, February 9, 2016 3:09 AM
  • Not an array, i.e $success += inlinescript{......} and $failed += inlinescript{.......}, so at the end I can see both the success and the failures.

    Return an array

    $xarray= inline ...

    $success+=$xarray[0]
    $failed+=$xarray[1]


    \_(ツ)_/

    Tuesday, February 9, 2016 3:13 AM
  • Another method:

    workflow test{
        inlinescript{@{success=1;failed=0}}
    }
    test
    
    #outcome
    
    Name                           Value
    ----                           -----
    success                        1
    failed                         0
    


    \_(ツ)_/

    Tuesday, February 9, 2016 3:17 AM
  • Other method:

    workflow test{
    	$tester=@{success=33;failed=23}
    	inlinescript{
    		$x=$using:tester
    		$x.success+=1
    		$x
    	}
    }
    test
    
    


    \_(ツ)_/

    • Marked as answer by JRLOPS Tuesday, February 9, 2016 3:39 AM
    Tuesday, February 9, 2016 3:23 AM
  • Thanks a lot jrv, finally thanks to your examples and instructions, It's working.

    The Working Script:

    workflow Test_HTTPS_Port {
    
    $servers = Import-Csv -Path C:\users\myuser\servers.csv
    $results = @()
    foreach -parallel -ThrottleLimit 32 ($server in $servers)
    {
        $WORKFLOW:results += inlinescript{
        $tcp = New-Object System.Net.Sockets.TcpClient    
        try
        {
            $tcp.connect("$using:server", 443)        
            return 'Successful'     
        }
        catch
        {
            return 'Failed'       
        }
        finally
        {
            $tcp.Dispose()        
        }          
        }    
    }
    #Show count of successful
    $results | where {$_ -eq "Successful"} | measure
    #Show count of failed
    $results | where {$_ -eq "Failed"} | measure
    }
    
    Test_HTTPS_Port
    Thanks again jrv

    Tuesday, February 9, 2016 3:39 AM