none
issue with passing value to script block RRS feed

  • Question

  • Hi guys,
    I'm trying to pass string into scriptblock and in spite of fact that i read many articles about that but I cannot find any solution for this puzzle. Moreover inside of function I can use only full path to save output, e.g. this won't work ".\savehere.txt"

    any ideas?

    $server_list = Get-Content ".\serversList.txt"
    function ForEach-Parallel {
        param(
            [Parameter(Mandatory=$true,position=0)]
            [System.Management.Automation.ScriptBlock] $ScriptBlock,
            [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
            [PSObject]$InputObject,
            [Parameter(Mandatory=$true)]
            [String]$PassedValue,
            [Parameter(Mandatory=$false)]
            [int]$MaxThreads=5

        )
        BEGIN {
            $iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
            $pool = [Runspacefactory]::CreateRunspacePool(1, $maxthreads, $iss, $host)
            $pool.open()
            $threads = @()
            $ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock("param(`$_)`r`n" + $Scriptblock.ToString())
        }
        PROCESS {
            $powershell = [powershell]::Create().addscript({$scriptblock}).addargument($InputObject).addargument($passedValue)
            $powershell.runspacepool=$pool
            $threads+= @{
                instance = $powershell
                handle = $powershell.begininvoke()
            }
        }
        END {
            $notdone = $true
            while ($notdone) {
                $notdone = $false
                for ($i=0; $i -lt $threads.count; $i++) {
                    $thread = $threads[$i]
                    if ($thread) {
                        if ($thread.handle.iscompleted) {
                            $thread.instance.endinvoke($thread.handle)
                            $thread.instance.dispose()
                            $threads[$i] = $null
                        }
                        else {
                            $notdone = $true
                        }
                    }
                }
            }
        }
    }

    $valueToPass="some text"

    $Output = $server_list | ForEach-Parallel -Maxthreads 5 -ifABC $valuetopass{

    write-host $valueToPass  #output is empty :/

    }

    • Moved by Bill_Stewart Friday, January 26, 2018 3:16 PM This is not "fix/debug/rewrite my script for me" forum
    Wednesday, December 6, 2017 6:22 PM

All replies

  • I do not see where you are passing any strings into any scriptblocks.

    Perhaps you should ask the author of the function for help.  The function attempts to define parameters that are reserved names and its design is suspicious.

    In PowerShell we would not use threads as they are not easily managed in PS.  Look into using runspaces as they are more flexible.  If you just want parallelism then use a workspace as it is already all set up to do all of the threading and parameter management.


    \_(ツ)_/

    Wednesday, December 6, 2017 7:34 PM