Multithreading 运行空间线程中的$MyInvocation
脚本(foo.ps1)创建一个线程,该线程创建更多线程。Foo的线程是我的控制线程,它创建一个或多个工作线程。工作线程运行一个脚本块。脚本块从库脚本调用函数。库脚本有一个配置文件 脚本块通过点源加载库脚本Multithreading 运行空间线程中的$MyInvocation,multithreading,powershell,Multithreading,Powershell,脚本(foo.ps1)创建一个线程,该线程创建更多线程。Foo的线程是我的控制线程,它创建一个或多个工作线程。工作线程运行一个脚本块。脚本块从库脚本调用函数。库脚本有一个配置文件 脚本块通过点源加载库脚本 $block = { Param($library_script) . $library_script ...stuff... } 当脚本加载时,它要做的第一件事就是找到它的配置文件,该文件位于脚本的目录中。代码看起来像 ## Global variables and enume
$block = {
Param($library_script)
. $library_script
...stuff...
}
当脚本加载时,它要做的第一件事就是找到它的配置文件,该文件位于脚本的目录中。代码看起来像
## Global variables and enumerations
$script:self_location = $script:MyInvocation.MyCommand.Path
$script:configuration_file_location = "{0}.config" -f $script:self_location
我的问题是,$MyInvocation
似乎不存在。因此,库脚本无法找到其配置文件
我正在Windows 10上运行Powershell 5.1。控制线程是在运行空间中生成的。工作线程在运行空间池中生成
有人知道运行空间线程中自动$MyInvocation
变量的规则吗
创建一个文件foo.ps1
,并将以下内容添加到其中:
Write-Output '[1] Executed in-scope'
$MyInvocation.MyCommand.Path
Write-Output '[2] Executed in-thread'
$p1 = [PowerShell]::Create()
$p1.AddScript({ $MyInvocation.MyCommand.Path }) | Out-Null
$p1.Invoke()
$p1.Dispose()
Write-Output '[3] Executed in-thread in-thread'
$t = {
$p = [PowerShell]::Create()
$p.AddScript({ $MyInvocation.MyCommand.Path }) | Out-Null
$p.Invoke()
}
$p2 = [PowerShell]::Create()
$p2.AddScript( $t ) | Out-Null
$p2.Invoke()
$p2.Dispose()
运行它。您应该看到如下内容
[1] Executed in-scope
C:\Users\deezNuts\development\comcast\sandbox\thing.ps1
[2] Executed in-thread
[3] Executed in-thread in-thread
而且,我想我刚刚回答了我自己的问题。我不确定$MyInvocation变量在后台作业中是否受支持
Start-Job -Name Test -ScriptBlock {Get-Variable}
Receive-Job Test
您能将路径作为参数传递吗?我在线程中看到变量
$rsp = [runspacefactory]::CreateRunspacePool(1, 2, $iss, $Host)
$rsp.ApartmentState = "STA"
$rsp.ThreadOptions = "ReuseThread"
$rsp.Open()
$p = [PowerShell]::Create()
$p.RunspacePool = $rsp
$p.AddScript({ write-host $MyInvocation.MyCommand.Path })
$h = $p.BeginInvoke()
$p.EndInvoke($h)
$p.Dispose()
$rsp.Dispose()
您的做法有什么不同?不,在从Powershell作业进行检查时,我看不到
$MyInvocation
,但我使用的是运行空间(不是Powershell作业)。它的工作原理有点不同。