get-comman...">

Function not recognized at startup

I created the "Get-Uptime" function in the sysinfo.psm1 module and imported the module.

C:/pstools> get-command -Module sysinfo CommandType Name Definition ----------- ---- ---------- Function Get-Uptime ... 

The function worked fine in Powershell. However, whenever I used the Get-Uptime function in Start-job -scriptblock {Get-Uptime $ servernae}, the operation failed with the following error

 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. 

Can someone please let me know what I missed? I searched the network and found a suggestion to write all the codes in the script block instead of using the function, but I tried and received similar errors.

Thanks.

+1
source share
3 answers

you can use InitializationScript to import the module:

 PS II> Start-Job -InitializationScript {import-module "c:\module.psm1"} -script {Uptime} 
+3
source

Before calling the function, you need to explicitly import your module into ScriptBlock.

+2
source

PowerShell tasks run in a separate process, a new powershell.exe file is created for each task object, and this process has no idea about the module that was imported into another session.

To get the Get-Uptime function, load the module in the Start-Job command.

+2
source

All Articles