Update Windows security remotely using powershell scheduled task

Welcome Forums General PowerShell Q&A Update Windows security remotely using powershell scheduled task

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
2 weeks, 6 days ago.

  • Author
    Posts
  • #177754

    Participant
    Topics: 5
    Replies: 2
    Points: 9
    Rank: Member

    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?

  • #180432

    Participant
    Topics: 1
    Replies: 96
    Points: 514
    Helping Hand
    Rank: Major Contributor

    Install-Module should run with the elevated permissions, not sure the run account for the job is having the elevated rights on the remote host. Please check.

You must be logged in to reply to this topic.