3

While using runspaces I would like to pass predictable arguments to my scriptblock from outside. In order to do this without utilizing $args (to avoid making the code less readable), I am storing my argument in a variable called $var and adding the variable to the InitialSessionState of the RunspacePool. To do this I am using the Add method of System.Management.Automation.Runspaces.InitialSessionState.Create().Variables

This works for my purposes, but I also noticed the PowerShell.AddParameter method of [PowerShell]::Create()which would add my parameter to each powershell process as it is created.

And finally there is the PowerShell.AddArgument which I could use like AddParameter, and use a param() block. this would work, however, each argument will be passed in order, and ends up working as well as $args.

So my question is what is the recommended way of passing arguments\parameters\variables into a scriptblock for the purposes I have defined? Is there a performance advantage of one over the other? Can you expand on what purpose you may use one over the other?

2 Answers 2

3

You can use cmdlet binding inside of the script block definition.

$ScriptBlock = {
    Param (
        [int]$NumberofHours,
        [string]$ClientName
    )
    $LogPath = 'd:\Program Files\Exchange\Logging\RPC Client Access\*.LOG'
    $today = (Get-Date).AddHours($NumberofHours)
}

Then define call these parameters when you execute the script block.

Invoke-Command -ComputerName $servers -ScriptBlock $ScriptBlock -ArgumentList -1,$cn -ErrorAction SilentlyContinue
Sign up to request clarification or add additional context in comments.

1 Comment

How would this help in the instance I gave? Using powershell runspaces, I would add the Scriptblock to the powershell process using AddScript() Method of PowerShell Class, which gives me no way of passing the defined parameters without using the AddArgument or AddParameter Method's of the PowerShell Class
2

Here's my $.02. I know this thread is old but I came across it unanswered while searching for something else.

This example uses a runspace pool to demonstrate how to pass parameters to the script block while using a runspace. The function this comes from passes a datatable to a script block for processing. Some variable names were changed to help with out-of-context readability.

$maxThreads = 5
$jobs = New-Object System.Collections.ArrayList

$rsp = [runspacefactory]::CreateRunspacePool(1,$maxThreads)
$rsp.open()

$params = New-Object 'System.Collections.Generic.Dictionary[string, string]'
$params.Add('Data', $myDataTable)
$params.Add('TableName', $myTableName)

$PS = [powershell]::Create()
$PS.Runspacepool = $rsp
$PS.AddScript($myScriptBlockCode)AddParameters($parameters)

Another option would be to forgo the iDictionary step and simply call .AddParameter repeatedly.

$PS.AddScript($myScriptBlockCode).AddParame'ter(Data',$myDataTable).AddParameter('tableName',$myTableName)

A third way is to pass the parameters as arguments in the order they are defined in your script block

$PS.AddScrip(t$myScriptBlockCode).AddArgument($myDataTable).AddArgument($myTableName)

I hope this helps someone out. I welcome comments as to ways to do this better. As long as the comment doesn't suggest turning it into some amateurish one-liner that no one can read....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.