Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 17, 2019 5:58 PM
Hi guys,
I should use a Function inside a runspace, I'm trying in every way to insert it and even looking in google I haven't found a clear example, could you tell me what am I wrong?
CLS
Function ConvertTo-Hex {
Param([int]$Number)
‘0x{0:x}’ -f $Number
}
$Definition = Get-Content Function:\ConvertTo-Hex
$Definition
$SessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList ‘ConvertTo-Hex’, $Definition
$synchash = [Hashtable]::Synchronized(@{ })
$synchash.Textbox1 = $textbox1
$synchash.Textbox2 = $textbox2
$synchash.OutputBox = $outputBox
$synchash.PathDesktop = "$LogPath\$csv"
$runspace = [RunspaceFactory]::CreateRunspace()
$runspace.ApartmentState = 'STA'
$runspace.ThreadOptions = 'ReuseThread'
$runspace.Open()
$Powershell = [PowerShell]::Create()
$Powershell.Runspace = $runspace
$Powershell.AddScript($BGScriptBlock) | Out-Null
$Powershell.Commands.AddCommand($SessionStateFunction)
All replies (7)
Friday, May 17, 2019 6:48 PM ✅Answered | 1 vote
Just add the function to you runspace script.
\(ツ)_/
Friday, May 17, 2019 6:23 PM
I tried something like this, but still nothing:
#$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::Create()
$initialSessionState = [InitialSessionState]::CreateDefault()
$definition = Get-Content Function:\Icmp-Ping -ErrorAction Stop
$addMessageSessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'Icmp-Ping', $definition
$initialSessionState.Commands.Add($addMessageSessionStateFunction)
$synchash = [Hashtable]::Synchronized(@{ })
$synchash.Textbox1 = $textbox1
$synchash.Textbox2 = $textbox2
$synchash.OutputBox = $outputBox
$synchash.PathDesktop = "$LogPath\$csv"
$runspace = [RunspaceFactory]::CreateRunspace($initialSessionState)
$runspace.ApartmentState = 'STA'
$runspace.ThreadOptions = 'ReuseThread'
$runspace.Open()
$Powershell = [PowerShell]::Create()
$Powershell.Runspace = $runspace
$Powershell.AddScript($BGScriptBlock) | Out-Null
$runspace.SessionStateProxy.SetVariable('synchash', $synchash)
Friday, May 17, 2019 6:41 PM
To use a function in a runspace you have to include the function code in the script that you are running in the runspace.
Functions must be added as script. Hexing the script makes the script unusable.
\(ツ)_/
Friday, May 17, 2019 6:43 PM
Could you make an example related to my code ?
Friday, May 17, 2019 7:12 PM
Ok, but how, using this lines or others ?
The question is this.
#$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::Create()
$initialSessionState = [InitialSessionState]::CreateDefault()
$definition = Get-Content Function:\Icmp-Ping -ErrorAction Stop
$addMessageSessionStateFunction = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList 'Icmp-Ping', $definition
$initialSessionState.Commands.Add($addMessageSessionStateFunction)
$synchash = [Hashtable]::Synchronized(@{ })
$synchash.Textbox1 = $textbox1
$synchash.Textbox2 = $textbox2
$synchash.OutputBox = $outputBox
$synchash.PathDesktop = "$LogPath\$csv"
$runspace = [RunspaceFactory]::CreateRunspace($initialSessionState)
$runspace.ApartmentState = 'STA'
$runspace.ThreadOptions = 'ReuseThread'
$runspace.Open()
$Powershell = [PowerShell]::Create()
$Powershell.Runspace = $runspace
$Powershell.AddScript($BGScriptBlock) | Out-Null
$runspace.SessionStateProxy.SetVariable('synchash', $synchash)
Friday, May 17, 2019 7:20 PM
Just add you function to this script block - "$BGScriptBlock". I cannot make it any clearer.
\(ツ)_/
Saturday, May 18, 2019 1:33 AM
Ok right, it works, thanks a lot !