none
Powershell workflow with "ForEach - Parallel" issue (Assignment DOES NOT work) RRS feed

  • Question

  • Hello dear colleagues!

    I need your help to explore issue with powershell script with workflow.

    Here is script without workflow and "ForEach - Parallel", which works correctly.

    $erroractionpreference = "Continue"
    import-module activedirectory
    $objOU = '...'
    $Computers = Get-ADComputer -Filter '*' -SearchBase $objOU | Select -ExpandProperty DNSHostName
    $Computers.Count
          
        foreach ($computer in $computers) {
            #Now get rid of domain suffix ".domain.com"
            #Replace function in assignment works!!!
            $Computer = $Computer.replace(".domain.com","")
            write "ComputerName1" $Computer
            #Replace function in output works!!!
            write "ComputerName2" $Computer.replace(".domain.com","")
        }

    Output:

    ComputerName1
    PC1
    ComputerName2
    PC1
    ComputerName1
    PC2
    ComputerName2
    PC2
    .............
    ComputerName1
    PC999
    ComputerName2
    PC999
    Here is script with workflow and "ForEach - Parallel", which DOES NOT work correctly.
    $erroractionpreference = "Continue"
    import-module activedirectory
    $objOU = '...'
    $Computers = Get-ADComputer -Filter '*' -SearchBase $objOU | Select -ExpandProperty DNSHostName
    $Computers.Count
    
    workflow Test-WFConnection {
    
      param(
    
        [string[]]$Computers
    
      )
          
        foreach -parallel -ThrottleLimit 20 ($computer in $computers) {
            #Now get rid of domain suffix ".domain.com"
            #Replace function in assignment DOES NOT work!!!
            $Computer = $Computer.replace(".domain.com","")
            write "ComputerName1" $Computer
            #Replace function in output works!!!
            write "ComputerName2" $Computer.replace(".domain.com","")
        }
    }
    
    Test-WFConnection -Computers $Computers
    Here is output:
    ComputerName1
    PC1.domain.com
    ComputerName2
    PC1
    Why Replace function in assignment DOES NOT work and variable $Computer does not changes and output with suffix ".domain.com"? 

    PS. If to remove "-parallel -ThrottleLimit 20" from second script with workflow, script works correctly also. What wrong with "-parallel" execution?


    • Edited by fapw Wednesday, December 17, 2014 11:17 AM
    Wednesday, December 17, 2014 10:55 AM

Answers