How to define a function in a Powershell runspace and execute it
<p>I'would like to define a function in a powershell and execute it in a different runspace. 
I tried this:</p>

<pre><code>Function Add-Message {
    param(
    [parameter(mandatory=$true)][String]$pMessage
    )   
    ("--$pMessage--") | Out-File -FilePath "D:\Temp\test.log" -Append           
}

$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::Create()
$definition = Get-Content Function:\Add-Message -ErrorAction Stop   
$addMessageSessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'Add-Message', $definition
$initialSessionState.Commands.Add($addMessageSessionStateFunction)

$newRunspace = [runspacefactory]::CreateRunspace($initialSessionState)
$newRunspace.ThreadOptions = "ReuseThread"  
$newRunspace.Open()
$newPowershell = [PowerShell]::Create()
$newPowershell.AddScript({  
    Add-Message -pMessage "New runspace"
}) | Out-Null
$newPowershell.Runspace = $newRunspace
$newPowershell.BeginInvoke() | Out-Null
</code></pre>

<p>The message "New runspace" does not appear in the file test.log
Any advice to do something like this?
Thanks</p>