Powershell jobs run in a seperate process, so try importing the module first in your scriptblock. Ex:
Start-Job -InitializationScript { Import-Module ...module here... } -Name uptime -ScriptBlock { ...your code.... }
题
why the Quest.ActiveRoles.ADManagement is not available inside a job ?
Ok when running interractively :
PS>Get-QADComputer -SearchRoot '$ou' -SizeLimit 200 |
%{ if (Test-Connection -Quiet $_.name) {
$lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $_.name).LastBootUpTime;
$sysuptime = (Get-Date) - System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)
write-host "$($_.name) allumé depuis $($sysuptime.days) jours" } }
domain\comp1$ allumé depuis 8 jours
domain\comp2$ allumé depuis 0 jours
But when using job :
PS>start-job -Name uptime -ScriptBlock{Get-QADComputer -SearchRoot '$ou' -SizeLimit 200 |
%{ if (Test-Connection -Quiet $_.name) {
$lastboottime = (Get-WmiObject -Class Win32_OperatingSystem -computername $_.name).LastBootUpTime;
$sysuptime = (Get-Date) - System.Management.ManagementDateTimeconverter]::ToDateTime($lastboottime)
write-host "$($_.name) allumé depuis $($sysuptime.days) jours" } }}
it complains to not find the command
PS>Receive-Job uptime -Keep
Le terme «Get-QADComputer» n'est pas reconnu comme nom d'applet de commande,
fonction, fichier de script ou programme exécutable. Vérifiez l'orthographe du
nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès est correct et
réessayez.
+ CategoryInfo : ObjectNotFound: (Get-QADComputer:String) [], Command
NotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName : localhost
解决方案
Powershell jobs run in a seperate process, so try importing the module first in your scriptblock. Ex:
Start-Job -InitializationScript { Import-Module ...module here... } -Name uptime -ScriptBlock { ...your code.... }