使用Powershell预定任务安装Windows安全修补程序

问题描述 投票:0回答:1

我正在尝试使用Powershell远程处理在远程计算机上安装Windows安全修补程序。

[当我在本地主机上运行脚本时,该脚本成功安装了Windows安全补丁。我已经设置了loaclhost和远程计算机以进行远程处理,并能够远程执行其他脚本。

使用计划任务:我正在使用以下脚本启动计划任务:

param(
   [parameter(Mandatory = $true)]
   [string]$IPaddress
)
$PSModulePath = $env:PSModulePath
$SplittedModulePath = $PSModulePath.Split(";")
$ModulePath = $SplittedModulePath[0]
$secpasswd = ConvertTo-SecureString "Pass@12345678" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("Admin02", $secpasswd)
#Create a Session. Replace host name with the host name of the remote machine.
$Session = New-PSSession -ComputerName $IPaddress -Credential $cred
$User= "Admin02"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "$env:ALLUSERSPROFILE\Install-WindowsUpdate.ps1"
$Trigger= New-ScheduledTaskTrigger -At 5:05am -Once
Invoke-Command -Session $Session -ScriptBlock { Register-ScheduledTask -TaskName "Install-Updates" -User $Using:User -Action $Using:Action -Trigger $Using:Trigger -RunLevel Highest –Force }

我已将以下脚本复制到目标计算机上的路径$ env:ALLUSERSPROFILE

Install-Module -Name PSWindowsUpdate -RequiredVersion 2.1.0.1 -Force
Import-Module PSWindowsUpdate -Force
Get-WindowsUpdate -install -acceptall

我安排任务后没有任何反应。我在做什么错?

powershell scheduled-tasks powershell-remoting windows-update windows-security
1个回答
1
投票

[在Register-ScheduledTask命令中,您指定要运行命令的用户(Admin02),但没有密码,我敢打赌,TaskScheduler无法启动任务,因为它没有以指定用户身份启动任务的凭据。试试:

$PlainPass = "Pass@12345678"    
Invoke-Command -Session $Session -ScriptBlock { Register-ScheduledTask -TaskName "Install-Updates" -User $Using:User -password $Using:PlainPass -Action $Using:Action -Trigger $Using:Trigger -RunLevel Highest –Force }

不幸的是,Register-ScheduledTask似乎需要简单的密码而不是安全字符串作为密码。

© www.soinside.com 2019 - 2024. All rights reserved.