【问题标题】:Why I cant use Get-QADComputer inside a job?为什么我不能在作业中使用 Get-QADComputer?
【发布时间】:2013-01-23 15:46:46
【问题描述】:

为什么 Quest.ActiveRoles.ADManagement 在作业中不可用?

交互运行时可​​以:

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

但是在使用作业时:

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" }  }}

它抱怨找不到命令

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 powershell-3.0 quest


    【解决方案1】:

    Powershell 作业在单独的进程中运行,因此请先尝试在脚本块中导入模块。例如:

    Start-Job -InitializationScript { Import-Module ...module here... } -Name uptime -ScriptBlock { ...your code.... }
    

    【讨论】:

    • 好点,刚刚在脚本块顶部添加了`Add-PSSnapin -name Quest.ActiveRoles.ADManagement`
    • PS V3 不应该在需要时自动加载模块吗?也许这种机制不适用于管理单元
    • 管理单元不同于模块。 PS3.0 自动从$env:PSModulePath中存储的位置加载模块
    • 是的,管理单元是过时的 v1 技术,不会自动加载
    猜你喜欢
    • 2017-03-15
    • 2010-10-15
    • 2015-02-19
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多