0
votes

I am trying to Install windows security patches on a remote machine using powershell remoting.

When i run the script on a local host, the script is successful in installing windows security patches.I have setup both the loaclhost and remote machine for remoting and able to execute other scripts remotely.

Using Scheduled Task: I am using the following script to start a scheduled task:

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 }

I have copied the below script on the target machine at the path $env:ALLUSERSPROFILE

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

After i schedule the task nothing is happening.What i am doing wrong?

1
"nothing is happening" Does the task get created? Does it run? If so, I'd suggest adding some logging to your script to output any useful information and errors to a log file.boxdog
Task is getting created. But it is not runningHarshith R

1 Answers

1
votes

In your Register-ScheduledTask command, you are specifying the user to run the command (Admin02) but no password, my bet is that TaskScheduler cannot start the task because it doesn't have the credentials to start it as the specified user. Try:

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

Unfortunately Register-ScheduledTask appears to need a plain, not Secure string for the password.