设为首页 收藏本站
查看: 1002|回复: 0

18. PowerShell -- Start-Job, Get-Job, Remove-Job 等

[复制链接]

尚未签到

发表于 2018-9-2 12:09:06 | 显示全部楼层 |阅读模式
  ·         PowerShell Job Overview
  This topic explains how to run background jobs in Windows PowerShell on a
  local computer. For information about running background jobs on remote
  computers, see about_Remote_Jobs.
  When you start a background job, the command prompt returns immediately,
  even if the job takes an extended time to complete. You can continue to
  work in the session without interruption while the job runs.
  ·      PowerShell Job CMDLETS
  Start-Job:
  Starts a background job on a local computer.
  Get-Job:
  Gets the background jobs that were started in the current session.
  Receive-Job:
  Gets the results of background jobs.
  Stop-Job:
  Stops a background job.
  Wait-Job:
  Suppresses the command prompt until one or all jobs are                  complete.
  Remove-Job:
  Deletes a background job.
  Invoke-Command:
  The AsJob parameter runs any command as a background job on a
  Remote computer. You can also use Invoke-Command to run
  Any job command remotely, including a Start-Job command.

  •   PowerShell Job 实例
  o    实例一:  Start-Job -ScriptBlock {Get-Process}
  $job = Start-Job -ScriptBlock {Get-Process}
  The above command starts a background job that runs a Get-Process    command on the local computer.
  o    实例二:   PS C:\>Get-Job

  >  --  ----  ------------- -----      -----------  --------   -------
  Job1  ·BackgroundJob ·Running    ·True   ·localhost  ·Get-··Process
  To get object that represent the background jobs that were started in the  current session, use the Get-Job cmdlet. Without parameters, Get-Job returns all of the jobs that were started in the current session.

  The following command gets the job with>  $job = Get-Job -Id 1
  The job object contains the state of the job, which indicates whether the
  job has finished. A finished job has a state of "Complete" or "Failed". A
  job might also be blocked or running.
  o    实例三:   Receive-Job -Job $job
  1. The Receive-Job cmdlet returns the results of the job.

  Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)   >  -------  ------    -----      ----- -----   ------    -- -----------
  103       4    11328       9692    56           1176 audiodg
  804      14    12228      14108   100   101.74  1740 CcmExec
  668       7     2672       6168   104    32.26   488 csrss
  ...
  2. Save the results of a job in a variable.

  •   $results = Receive-Job -Job $job
  3. Save the results of the job in a file by using the redirection   operator (>) or the Out-File cmdlet.

  •   Receive-Job -Job $job > results.txt
  4. When Receive-Job returns results, by default, it deletes those results from   the cache where job results are stored.
  C:\PS> Receive-Job -Job $job

  Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)    >  -------  ------    -----      ----- -----   ------     -- -----------
  103       4    11328       9692    56            1176 audiodg
  804      14    12228      14108   100   101.74   1740 CcmExec
  5. To prevent Receive-Job from deleting the job results that it has returned, use the Keep parameter.
  C:\PS> Receive-Job -Job $job -Keep

  Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)    >  -------  ------    -----      ----- -----   ------     -- -----------
  103       4    11328       9692    56            1176 audiodg
  804      14    12228      14108   100   101.74   1740 CcmExec
  o    实例四:   Wait-Job -ID 10
  The above command uses the Wait-Job cmdlet to wait for a job with
  ID 10.
  As a result, the Windows PowerShell prompt is suppressed until the job
  is completed.
  You can also wait for a predetermined period of time. This command uses
  the Timeout parameter to limit the wait to 120 seconds. When the time
  expires, the command prompt returns, but the job continues to run in the
  background.
  Wait-Job -ID 10 -Timeout 120
  o    实例五:      STOPPING A JOB
  $job = Start-Job -ScriptBlock {Get-EventLog -Log System}
  $job | Stop-Job
  To stop a background job, use the Stop-Job cmdlet. The above command
  starts a job to get every entry in the System event log. It saves the job
  object in the $job variable.
  The above command stops the job. It uses a pipeline operator (|) to
  send the job in the $job variable to Stop-Job.
  o    实例六:   DELETING A JOB
  To delete a background job, use the Remove-Job cmdlet. The following
  command deletes the job in the $job variable.
  Remove-Job -Job $job
  o    实例七:   INVESTIGATING A FAILED JOB
  To find out why a job failed, use the Reason subproperty of the job object.
  The following command starts a job without the required credentials. It
  saves the job object in the $job variable.
  $job = Start-Job -ScriptBlock
  {New-Item -Path HKLM:\Software\MyCompany}

  >  --   ----  ------------- -----    -----------  --------   -------
  1    Job1  BackgroundJob Failed   False        localhost  New-Item -Path HKLM:\S...
  The following command uses the Reason property to find the error that
  caused the job to fail.
  $job.ChildJobs[0].JobStateInfo.Reason
  In this case, the job failed because the remote computer required explicit
  credentials to run the command. The value of the Reason property is:
  Connecting to remote server failed with the following error
  message : Access is denied.
  o    实例八:   Waiting  JOB
  Write-Host "Waiting job..."
  Get-Job|Wait-Job|Remove-Job
  SEE ALSO
  about_Remote_Jobs
  about_Job_Details
  about_Remote
  about_PSSessions
  Start-Job
  Get-Job
  Receive-Job
  Stop-Job
  Wait-Job
  Remove-Job
  Invoke-Command
  参考:
  https://technet.microsoft.com/zh-cn/library/hh847783(v=wps.620).aspx#
  https://technet.microsoft.com/en-us/library/dd347692.aspx


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-561542-1-1.html 上篇帖子: 15. PowerShell -- 远程管理(1) 下篇帖子: 19. PowerShell-- 资源汇总
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表