Jump to content
Chris1992

Windows Update PS Script Automation

Recommended Posts

Posted (edited)

Good evening,

I would like to use the standard script for windows that is in pulseway automation.

But I get this error message:
Checking for Windows updates
Downloading updates
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At C:\Program Files\Pulseway\automation_9fdca742_c373_41fb_80e3_ce2b245530ca.ps1:25 char:5
+     $Downloader = $Session.CreateUpdateDownloader()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException

 

The user used to execute the script (Settings - runtime) is a domain admin.

The script works if I execute powershell as an administrator.
In pulseway, with automation I cannot state that the script must be executed as admin.

Am I missing something?

Thanks in advance!

 

Printscreens in attachement

Script:

Write-Host "Checking for Windows updates"
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Session = New-Object -ComObject Microsoft.Update.Session

$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$results = $searcher.search("Type='software' AND IsInstalled = 0 AND IsHidden = 0 AND AutoSelectOnWebSites = 1")

#Install WindowsUpdate Module
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Module PSWindowsUpdate

# Install Update
if ($results.Updates.Count -eq 0) {
    Write-Host "No updates found"
    # no updates.
} else {
    # setup update collection
    foreach ($update in $results.Updates){
        $UpdateCollection.Add($update) | out-null
    }

    # download update items
    Write-Host "Downloading updates"
    $Downloader = $Session.CreateUpdateDownloader()
    $Downloader.Updates = $UpdateCollection
    $Downloader.Download()

    # install update items
    Write-Host "Installing updates"
    $Installer = New-Object -ComObject Microsoft.Update.Installer
    $Installer.Updates = $UpdateCollection
    $InstallationResult = $Installer.Install()
    # Check Result
    if ($InstallationResult.ResultCode -eq 2){
        Write-Host "Updates installed successfully"
    } else {
        Write-Host "Some updates could not be installed"
    }
    if ($InstallationResult.RebootRequired){
        Write-Host "System needs to reboot"
        # uncomment the following line to automatically reboot the system if a reboot is required after the updates are installed
        #shutdown -r -f -t 300 -c "Rebooting in 5 minutes to apply Windows updates"
    }
}

 

2020-01-03 19_49_19-win2019-web-00.digitalkey.local - Remote Desktop Connection.jpg

2020-01-03 19_49_38-win2019-web-00.digitalkey.local - Remote Desktop Connection.jpg

Edited by Chris1992

Share this post


Link to post
Share on other sites

Hi @Chris1992,

If you have configured the PowerShell impersonation in Pulseway, then all PowerShell scripts via Pulseway will be executed using account Pulseway, which is shown into the image. Does that account has full access rights to the C:\Program Files\Pulseway\ folder on that particular system to execute the script?

Share this post


Link to post
Share on other sites

hey @Chris,

I have given the user pulseway full control on the folder but keep getting the same error.

Quote

Checking for Windows updates
Downloading updates
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At C:\Program Files\Pulseway\automation_784d8354_a1a3_40d0_94fe_18e94aeaa5de.ps1:25 char:5
+     $Downloader = $Session.CreateUpdateDownloader()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException
 
The property 'Updates' cannot be found on this object. Verify that the property exists and can be set.
At C:\Program Files\Pulseway\automation_784d8354_a1a3_40d0_94fe_18e94aeaa5de.ps1:26 char:5
+     $Downloader.Updates = $UpdateCollection
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
You cannot call a method on a null-valued expression.
At C:\Program Files\Pulseway\automation_784d8354_a1a3_40d0_94fe_18e94aeaa5de.ps1:27 char:5
+     $Downloader.Download()
+     ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Installing updates
New-Object : Creating an instance of the COM component with CLSID {D2E0FE7F-D23E-48E1-93C0-6FA8CC346474} from the IClas
sFactory failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDEN
IED)).
At C:\Program Files\Pulseway\automation_784d8354_a1a3_40d0_94fe_18e94aeaa5de.ps1:31 char:18
+     $Installer = New-Object -ComObject Microsoft.Update.Installer
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-Object], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.NewObjectCommand
 
The property 'Updates' cannot be found on this object. Verify that the property exists and can be set.
At C:\Program Files\Pulseway\automation_784d8354_a1a3_40d0_94fe_18e94aeaa5de.ps1:32 char:5
+     $Installer.Updates = $UpdateCollection
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
 
You cannot call a method on a null-valued expression.
At C:\Program Files\Pulseway\automation_784d8354_a1a3_40d0_94fe_18e94aeaa5de.ps1:33 char:5
+     $InstallationResult = $Installer.Install()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Some updates could not be installed

 

2020-01-10 17_22_42-win2019-web-00.digitalkey.local - Remote Desktop Connection.jpg

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...