如何将单独文件中的函数导入主文件并将它们 运行 作为作业?
How do I import functions in separate files into a main file and run them as jobs?
我已经问了一个有点相关的问题 。
我在单独的文件中有一堆函数点源在一个主文件中,我如何将主文件中的这些函数称为作业?
这里是 func1.ps1:
function FOO { write-output "HEY" }
这里是func2.ps1:
function FOO2 { write-output "HEY2" }
这是 testjobsMain。ps1
$Functions = {
. .\func1.ps1
. .\func2.ps1
}
$var = Start-Job -InitializationScript $Functions -ScriptBlock { FOO } | Wait-Job | Receive-Job
$var
当我 运行 testjobsMain.ps1 我得到这个错误:
. : The term '.\func1.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:2 char:4
+ . .\func1.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\func1.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
. : The term '.\func2.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:3 char:4
+ . .\func2.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\func2.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Running startup script threw an error: The term '.\func2.ps1' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again..
+ CategoryInfo : OpenError: (localhost:String) [], RemoteException
+ FullyQualifiedErrorId : PSSessionStateBroken
绝对路径对我有用:
$Functions = {
. c:\foo.ps1
}
$var = Start-Job -InitializationScript $Functions -ScriptBlock { FOO } | Wait-Job | Receive-Job
$var
如果需要,在 testjobsMain.ps1 中,您可以使用 $PSScriptRoot
自动变量将相对路径替换为绝对路径。例如:
$Functions = [scriptblock]::Create(" . $PSScriptRoot\foo.ps1 `n . $PSScriptRoot\bar.ps1 `n")
我已经问了一个有点相关的问题
我在单独的文件中有一堆函数点源在一个主文件中,我如何将主文件中的这些函数称为作业?
这里是 func1.ps1:
function FOO { write-output "HEY" }
这里是func2.ps1:
function FOO2 { write-output "HEY2" }
这是 testjobsMain。ps1
$Functions = {
. .\func1.ps1
. .\func2.ps1
}
$var = Start-Job -InitializationScript $Functions -ScriptBlock { FOO } | Wait-Job | Receive-Job
$var
当我 运行 testjobsMain.ps1 我得到这个错误:
. : The term '.\func1.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:2 char:4
+ . .\func1.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\func1.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
. : The term '.\func2.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At line:3 char:4
+ . .\func2.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\func2.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Running startup script threw an error: The term '.\func2.ps1' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again..
+ CategoryInfo : OpenError: (localhost:String) [], RemoteException
+ FullyQualifiedErrorId : PSSessionStateBroken
绝对路径对我有用:
$Functions = {
. c:\foo.ps1
}
$var = Start-Job -InitializationScript $Functions -ScriptBlock { FOO } | Wait-Job | Receive-Job
$var
如果需要,在 testjobsMain.ps1 中,您可以使用 $PSScriptRoot
自动变量将相对路径替换为绝对路径。例如:
$Functions = [scriptblock]::Create(" . $PSScriptRoot\foo.ps1 `n . $PSScriptRoot\bar.ps1 `n")