0

I need to modify the below script to execute the Windows Update so it downloads and updates the remote server:

Invoke-RemoteExecution($computername){
    $cred = get-credential

    invoke-command -computername $computername -Credential $cred -scriptblock {


    Function Search-Updates
    {
    $Criteria = "IsInstalled=0 and Type='Software'"
    #Search for relevant updates.
    $Searcher = New-Object -ComObject Microsoft.Update.Searcher
    $SearchResult = $Searcher.Search($Criteria).Updates
    
    return [System.MarshalByRefObject]$SearchResult
    }

    Function Download-Updates
    {
    Param ($SearchResult)
    $Session = New-Object -ComObject Microsoft.Update.Session
    $Downloader = $Session.CreateUpdateDownloader()
    $Downloader.Updates = $SearchResult
    $Downloader.Download()
    }

    [System.MarshalByRefObject]$SearchResult = Search-Updates

    Download-Updates -SearchResult $SearchResult

    }

}

Invoke-RemoteExecution -computername yourcomputername

The error I am facing is like the below:

Invoke-RemoteExecution -computername PRD-SVR01-VM
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:


Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    + CategoryInfo          : OperationStopped: (:) [], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException
    + PSComputerName        : PRD-SVR01-VM


The property 'Updates' cannot be found on this object. Verify that the property exists and can be set.
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
    + PSComputerName        : PRD-SVR01-VM

You cannot call a method on a null-valued expression.
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
    + PSComputerName        : PRD-SVR01-VM

I have tried to use the remoting but it is still failed as above.

halfer
  • 19,824
  • 17
  • 99
  • 186
Senior Systems Engineer
  • 1,061
  • 2
  • 27
  • 63
  • 1
    It looks like you're not supplying credentials that will give you access to PRD-SVR01-VM. Once you clear up the Access Denied, it is likely that the the other errors will clear. – Jeff Zeitlin Sep 02 '22 at 12:12
  • Actually it ask for the credentials, and yes I have supplied the domain admins. – Senior Systems Engineer Sep 02 '22 at 12:17
  • 2
    This may be one of those situations where you have to use task scheduler to run it remotely. – js2010 Sep 02 '22 at 13:14
  • 1
    Does this answer your question? [Powershell Remote: Microsoft.Update.Session, Access Denied: 0x80070005](https://stackoverflow.com/questions/7078958/powershell-remote-microsoft-update-session-access-denied-0x80070005) – Joshua McKinnon Sep 02 '22 at 23:06
  • 1
    Scheduled task over remote powershell example: https://stackoverflow.com/a/73452495/6654942 – js2010 Sep 03 '22 at 13:57

0 Answers0