ForEach-Object -Parallel 看不到已定义的 cmdlet
ForEach-Object -Parallel can't see defined cmdlet
我正在为 powershell 苦苦挣扎(第一次在这个 evn 中工作)。并发现当我尝试使用具有并行选项的 ForEach 时,循环看不到几行后定义的 cmdlet。你能给我指出任何可以帮助我解决这个问题的文档吗?在我的测试脚本和输出下面
1..10 | ForEach-Object -Parallel {
hi $_
sleep 2
}
function hi {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[String]$name
)
Begin {
function sayHi {
param (
[Parameter(Position=0)]
$name
)
Write-Host ">>>>"
Write-Host "Say hi $($name)"
Write-Host ">>>>"
}
}
Process {
hi $name
}
End{}
}
输出:
Desktop> .\test_paralell.ps1
hi:
Line |
2 | hi $_
| ~~
| The term 'hi' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
hi:
Line |
2 | hi $_
| ~~
| The term 'hi' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
hi:
当您使用 -parallel 时,幕后发生的事情是 Powershell 正在创建另一个 'namespace',这意味着您定义的函数在此范围内不可用。
你不是第一个提出这个问题的人 -> 请在这里查看示例:
我正在为 powershell 苦苦挣扎(第一次在这个 evn 中工作)。并发现当我尝试使用具有并行选项的 ForEach 时,循环看不到几行后定义的 cmdlet。你能给我指出任何可以帮助我解决这个问题的文档吗?在我的测试脚本和输出下面
1..10 | ForEach-Object -Parallel {
hi $_
sleep 2
}
function hi {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]
[String]$name
)
Begin {
function sayHi {
param (
[Parameter(Position=0)]
$name
)
Write-Host ">>>>"
Write-Host "Say hi $($name)"
Write-Host ">>>>"
}
}
Process {
hi $name
}
End{}
}
输出:
Desktop> .\test_paralell.ps1
hi:
Line |
2 | hi $_
| ~~
| The term 'hi' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
hi:
Line |
2 | hi $_
| ~~
| The term 'hi' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
hi:
当您使用 -parallel 时,幕后发生的事情是 Powershell 正在创建另一个 'namespace',这意味着您定义的函数在此范围内不可用。
你不是第一个提出这个问题的人 -> 请在这里查看示例: