简体   繁体   English

使用 PowerShell 远程安装 Windows 更新。 (一些 PowerCLI)

[英]Installing windows updates remotely, using PowerShell. (some PowerCLI)

I am having a problem with a script for remote windows updates.我的远程 Windows 更新脚本有问题。 My goal: update all the windows servers, without using WSUS or SCCM, from PowerShell without having to log into all of them and start the update process.我的目标是:在不使用 WSUS 或 SCCM 的情况下,从 PowerShell 更新所有 Windows 服务器,而无需登录所有 Windows 服务器并启动更新过程。

For my script I am using powercli and PSWindowsUpdate modules.对于我的脚本,我使用了 powercli 和 PSWindowsUpdate 模块。 For test purposes I am using a single host in the $hostname variable.出于测试目的,我在 $hostname 变量中使用单个主机。

So here is what I have.所以这就是我所拥有的。

$Cred = Get-Credential
Import-Module PSWindowsUpdate
Invoke-Command -ComputerName $HostName {Import-Module PSWindowsUpdate} -Verbose -Credential $Cred
Invoke-Command -ComputerName $HostName {Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d -Confirm:$false} -Verbose -Credential $Cred
Invoke-Command -ComputerName $HostName -ScriptBlock {Get-WUInstall -MicrosoftUpdate -IgnoreUserInput -AcceptAll -AutoReboot -Confirm:$FALSE -Verbose | Out-File C:\Setup\PSWindowsUpdate.log } -credential $cred
Get-Content \\$HostName\c$\Setup\PSWindowsUpdate.log

After running the script everything works from the shut down, snapshot, power on but I can't install any updates.运行脚本后,从关机、快照到开机,一切正常,但我无法安装任何更新。 I am getting the error below :我收到以下错误:

WARNING: To perform some operations you must run an elevated Windows PowerShell console.
WARNING: Can't access to machine "hostName". Try use: winrm qc

As I was searching I see that I can not make any elevation in PowerShell itself and I am reading some posts about running a CMD bat to start PowerShell in elevated rights.在搜索时,我发现我无法在 PowerShell 本身中进行任何提升,我正在阅读一些关于运行 CMD bat 以提升权限启动 PowerShell 的帖子。 It is not a problem for me to copy to every windows server the update script and have a batch to run it in elevated rights but is this the only way?将更新脚本复制到每个 Windows 服务器并让批处理以提升的权限运行它对我来说不是问题,但这是唯一的方法吗?

I will do the batch solution to see what I can achieve but is there any other solution in the problem I am facing?我将执行批处理解决方案以查看我可以实现的目标,但是我面临的问题还有其他解决方案吗? Has anyone tried that out?有没有人试过?

Thank you very much for your time!非常感谢您的宝贵时间! I am also open for any comment on my code or fixes!我也愿意对我的代码或修复程序发表任何评论!

If Admin right, elevated prompts are your issue, the following may help you.如果管理员权限,提升的提示是您的问题,以下内容可能对您有所帮助。

PS Code to check if running as an Administrator.用于检查是否以管理员身份运行的 PS 代码。 If not, It will relaunch as an Administrator.如果没有,它将以管理员身份重新启动。

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

You will also likely need to modify ConsentPromptBehaviorAdmin at HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\ in the Registry您可能还需要在ConsentPromptBehaviorAdmin中的HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\处修改ConsentPromptBehaviorAdmin

- ——

Here is a simple BAT file that will will get around the PS Execution policy and then launch the PS Script (Assuming they are named the same).这是一个简单的 BAT 文件,它将绕过 PS 执行策略,然后启动 PS 脚本(假设它们的名称相同)。

REG ADD "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /T REG_SZ /V ExecutionPolicy /D Unrestricted /F

Start PowerShell.exe -Command "& '%~dpn0.ps1'"

Add the end of your PS Script you can use Set-ExecutionPolicy -ExecutionPolicy Restricted -Scope LocalMachine -Force to set the execution policy back to restricted or what you have ti set to.添加您的 PS 脚本的末尾,您可以使用Set-ExecutionPolicy -ExecutionPolicy Restricted -Scope LocalMachine -Force将执行策略设置回受限或您设置的内容。

I ran into this issue when trying to import modules on remote machines using Invoke-Command .我在尝试使用Invoke-Command在远程机器上导入模块时遇到了这个问题。 There's a security function that doesn't allow you to do it.有一项安全功能不允许您这样做。 You might try running the following to see if you're more successful at importing a new module.您可以尝试运行以下命令,看看您在导入新模块方面是否更成功。

$myPsSession = New-PSSession -ComputerName $HostName
Import-Module -Name PSWindowsUpdate -PSSession $myPsSession

I went with the task scheduler solution as it seems easier.我选择了任务调度程序解决方案,因为它看起来更容易。

I created a script named WUpdates.ps1我创建了一个名为 WUpdates.ps1 的脚本

$Timestamp=((Get-Date).ToString('dd_MM_yyyy_HH_mm'))
Import-Module PSWindowsUpdate -Verbose
Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d -Confirm:$false -Verbose
Get-WUInstall -MicrosoftUpdate -IgnoreUserInput -AcceptAll -AutoReboot -Confirm:$FALSE -Verbose | Format-Table -AutoSize | Out-File C:\Setup\WUpdates\PSWindowsUpdate_$Timestamp.log

and a script to create a schedule task named WinUpdateScheduler.ps1和一个脚本来创建一个名为 WinUpdateScheduler.ps1 的计划任务

Unregister-ScheduledTask -TaskName "WindowsUpdates" -Confirm:$False    
$Action = New-ScheduledTaskAction -Execute C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Argument '-f "C:\Setup\WUpdates\WUpdates.ps1"'
$Trigger =  New-ScheduledTaskTrigger -Once -At (get-date).AddSeconds(30)
Register-ScheduledTask -Action $Action -Trigger $Trigger -RunLevel Highest -User system -TaskName "WindowsUpdates" -Description "Running Windows updates."

then in the main script I call the schedule script and have my updates installed.然后在主脚本中我调用调度脚本并安装我的更新。

Invoke-Command -ComputerName $HostName { c:\setup\WUpdates\WinUpdateScheduler.ps1 } -Credential $Cred

If anyone want the full script I can provide it.如果有人想要完整的脚本,我可以提供。 As of the Nixphoe and Backin answers, I will check them and I will come back later to comment on them.至于 Nixphoe 和 Backin 的回答,我会检查它们,稍后我会回来评论它们。

Thank you very much for your time.非常感谢您的宝贵时间。

Invoke-WUInstall -ComputerName Test-1 -Script { ipmo PSWindowsUpdate; Get-WUInstall -AcceptAll | Out-File C:\PSWindowsUpdate.log } 
-Confirm:$false –Verbose

https://4sysops.com/archives/install-windows-updates-remotely-with-the-powershell/ https://4sysops.com/archives/install-windows-updates-remotely-with-the-powershell/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM