我正在努力使用 powershell(第一次在這個 evn 中作業)。并發現當我嘗試使用帶有 Parallel 選項的 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:
uj5u.com熱心網友回復:
當您使用 -parallel 時,幕后發生的事情是 Powershell 正在創建另一個“命名空間”,這意味著您定義的函式在此范圍內不可用。你不是第一個提出這個問題的人 -> 請看這里的例子: How to pass a custom function inside a ForEach-Object -Parallel
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368910.html
上一篇:Powershell輸出大括號
