从运行空间内的模块调用函数
Call function from module inside a runspace
我想从运行空间调用的模块中有一些函数,但它不工作。我假设我必须以某种方式将模块发送到运行空间。
下面的例子工作正常。
$hash = [hashtable]::Synchronized(@{})
$hash.OutData
$runspace = [runspacefactory]::CreateRunspace()
$runspace.Open()
$runspace.SessionStateProxy.SetVariable('Hash',$hash)
$powershell = [powershell]::Create()
$powershell.Runspace = $runspace
$powershell.AddScript({
$hash.OutData = Get-Date
}) | Out-Null
$handle = $powershell.BeginInvoke()
While (-Not $handle.IsCompleted) {
Start-Sleep -Milliseconds 100
}
$powershell.EndInvoke($handle)
$runspace.Close()
$powershell.Dispose()
但是如果我像这样调用我自己的函数,OutData 是空白的。该函数在运行空间之外工作正常。
$powershell.AddScript({
$hash.OutData = Get-customData
}) | Out-Null
我需要做什么才能调用我的函数?
如果您的模块不在 $env:PSModulePath
中列出的目录之一(或者未定义后一个环境变量,如果您在外部托管 PowerShell SDK,则在 Unix 上可能会发生这种情况可执行文件),您必须 明确地 :
导入它
$yourFullModulePath = '<your-full-module-path-here>'
# Create a default session state and import a module into it.
$iss = [InitialSessionState]::CreateDefault()
$iss.ImportPSModule($yourFullModulePath)
# Create the runspace with the initial session state and open it.
$runspace = [runspacefactory]::CreateRunspace($iss)
$runspace.Open()
# Create a PowerShell instance and assign the runspace to it.
$powershell = [powershell]::Create($runspace)
# ...
请注意,您可以利用 [powershell]
实例 自动 创建运行空间这一事实来简化代码:
# Pass the initial session state directly to [powershell]::Create(),
# which automatically provides a runspace.
$powershell = [powershell]::Create($iss)
# Access the [powershell] instance's runspace via the `.Runspace` property.
$powerShell.Runspace.SessionStateProxy.SetVariable('Hash', $hash)
# ...
我想从运行空间调用的模块中有一些函数,但它不工作。我假设我必须以某种方式将模块发送到运行空间。
下面的例子工作正常。
$hash = [hashtable]::Synchronized(@{})
$hash.OutData
$runspace = [runspacefactory]::CreateRunspace()
$runspace.Open()
$runspace.SessionStateProxy.SetVariable('Hash',$hash)
$powershell = [powershell]::Create()
$powershell.Runspace = $runspace
$powershell.AddScript({
$hash.OutData = Get-Date
}) | Out-Null
$handle = $powershell.BeginInvoke()
While (-Not $handle.IsCompleted) {
Start-Sleep -Milliseconds 100
}
$powershell.EndInvoke($handle)
$runspace.Close()
$powershell.Dispose()
但是如果我像这样调用我自己的函数,OutData 是空白的。该函数在运行空间之外工作正常。
$powershell.AddScript({
$hash.OutData = Get-customData
}) | Out-Null
我需要做什么才能调用我的函数?
如果您的模块不在 $env:PSModulePath
中列出的目录之一(或者未定义后一个环境变量,如果您在外部托管 PowerShell SDK,则在 Unix 上可能会发生这种情况可执行文件),您必须 明确地 :
$yourFullModulePath = '<your-full-module-path-here>'
# Create a default session state and import a module into it.
$iss = [InitialSessionState]::CreateDefault()
$iss.ImportPSModule($yourFullModulePath)
# Create the runspace with the initial session state and open it.
$runspace = [runspacefactory]::CreateRunspace($iss)
$runspace.Open()
# Create a PowerShell instance and assign the runspace to it.
$powershell = [powershell]::Create($runspace)
# ...
请注意,您可以利用 [powershell]
实例 自动 创建运行空间这一事实来简化代码:
# Pass the initial session state directly to [powershell]::Create(),
# which automatically provides a runspace.
$powershell = [powershell]::Create($iss)
# Access the [powershell] instance's runspace via the `.Runspace` property.
$powerShell.Runspace.SessionStateProxy.SetVariable('Hash', $hash)
# ...