用于下载更新的powershell脚本

时间:2016-12-28 04:48:32

标签: powershell

我编写了一个PowerShell脚本来下载和安装Windows更新,如下所示。此脚本将运行powershell以管理员身份运行。我在Windows 7计算机上运行但它仍然有一个打开powershell窗口的循环。请指教

enter code here

If (-NOT ([Security.Principal.WindowsPrincipal]   
[Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole   
([Security.Principal.WindowsBuiltInRole] "Administrator")) {

$arguments = "& '" + $myinvocation.mycommand.definition + "'"
 Start-Process powershell -Verb runAs -ArgumentList $arguments




  #define type of updates

   $criteria="Isinstalled=0 and Type='driver'"

  #Search for relevant updates


     $Searcher=New-Object -ComObject Microsoft.Update.Searcher
      $SearchResult=$Searcher.Search($criteria).updates

       if($SearchResult.Count -gt 0) { 


       $Session=New-Object -ComObject Microsoft.Update.Session
      $Downloader=$Session.CreateUpdateDownloader()
   $Downloader.Updates=$SearchResult
   $Downloader.Download()

   $installer=New-Object -ComObject Microsoft.Update.Installer
   $installer.Updates=$SearchResult
   $Install_Begin=$installer.Install()

     }

  break

  }

0 个答案:

没有答案