我在一个模块sysinfo.psm1中创建了一个函数"Get-Uptime“,并导入了该模块。
C:/pstools> get-command -Module sysinfo
CommandType Name Definition
----------- ---- ----------
Function Get-Uptime ...该函数在Powershell中运行良好。但是,每当我在启动作业-scriptblock {Get-Uptime $servernae}中使用Get-Uptime函数时,作业都会失败,并显示以下错误
Receive-Job : The term 'get-uptime' 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.有人能告诉我我错过了什么吗?我在网上搜索了一下,发现了一个建议,用scriptblock编写所有代码,而不是使用函数,但我尝试了一下,得到了类似的错误。
谢谢。
发布于 2012-09-06 19:06:36
您可以使用InitializationScript导入模块:
PS II> Start-Job -InitializationScript {import-module "c:\module.psm1"} -script {Uptime}发布于 2012-09-06 13:59:51
在调用函数之前,您必须在ScriptBlock中显式导入您的模块。
发布于 2012-09-06 15:01:38
PowerShell作业在单独的进程中运行,为每个作业对象创建一个新的powershell.exe,并且该进程不知道在另一个会话中导入了一个模块。
为了需要Get-Uptime函数,请在Start-Job命令中加载该模块。
https://stackoverflow.com/questions/12293701
复制相似问题