Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用 PowerShell 给 Windows 系统安装补丁并自动重启 #146

Closed
Dream4ever opened this issue Apr 22, 2021 · 0 comments
Closed

用 PowerShell 给 Windows 系统安装补丁并自动重启 #146

Dream4ever opened this issue Apr 22, 2021 · 0 comments
Labels
Server The invisible hero

Comments

@Dream4ever
Copy link
Owner

需求描述

之前实现了 PowerShell 调用阿里云 Cli 创建手动快照并重启实例,现在由于需要时不时地给阿里云服务器上的 Windows 打补丁,之前都是等手动快照创建完成之后再人工打补丁,太麻烦了,所以研究了一下自动化实现该需求的解决方案。

整体流程

需要先在 PowerShell 中安装 PSWindowsUpdate 这个模块,有了它就可以检查当前的 Windows 系统是否有需要安装的补丁,并且还可以用它来安装补丁,并在安装完成后自动重启 Windows 系统。

然后编写下面的 PowerShell 脚本,用来检查当前 Windows 系统是否有需要安装的补丁,如果有则自动安装,并在安装完成后自动重启。

$output = Get-WindowsUpdate | Out-String
$updates = ([regex]'KB(\d)+').Matches($output)
if ($updates.Count -gt 0) {
  Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot
}

然后把上面的脚本添加到 Windows 的计划任务中,定时执行:

image

image

image

image

@Dream4ever Dream4ever added the Server The invisible hero label Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Server The invisible hero
Projects
None yet
Development

No branches or pull requests

1 participant