1

I want to deploy Updates to Windows Servers in Our Domain. To achieve this i want to use the Module "PSWindowsUpdate" Here is the Official Release. I use this Module in combination with PSSessions and import it locally on all Servers outside of the default Module Path.

It should accept the updates and install them without rebooting. This Script is run using an Domain Administrator

After it Accepts the Updates it should start downloading where this happens: The Error of the Job

I started getting this error after the 2018 July Security Patch installed.

As I can't share all of the code because of Company reasons, here is the part that matters:

function invokeUpdate{
param(
    $session
)
if($Script:My.Reboot.isChecked){
    $job = Invoke-Command -Session $session -ScriptBlock {Import-Module "C:\Scripts\updateModule\$($Using:My.ModuleVersion)\PSWindowsUpdate"; get-windowsupdate -install -AcceptAll} -AsJob
}else {
    $job = Invoke-Command -Session $session -ScriptBlock {Import-Module "C:\Scripts\updateModule\$($Using:My.ModuleVersion)\PSWindowsUpdate"; get-windowsupdate -install -ignoreReboot -AcceptAll} -AsJob
    }
return $job
}

function initSession{
param(
    $serverHostname
)
$ses = New-PSSession -Computername $serverHostname
if(!(Invoke-Command -Session $ses -ScriptBlock {Test-Path "C:\Scripts\updateModule\" })){
    Copy-Item "$modpath\$($Script:My.ModuleVersion)" -Destination "C:\Scripts\updateModule\$($Script:My.ModuleVersion)" -ToSession $ses -Recurse
}
Invoke-Command -Session $ses -ScriptBlock {
    if((Get-ChildItem -Path "C:\Scripts\updateModule\").count -gt 1){
        Get-ChildItem | Where-Object Name -NotLike "$($Using:My.ModuleVersion)" | Remove-Item -Recurse -Force
    }
}
return $ses
}

$sessions =  [System.Collections.ArrayList]@()
$Script:My.ModuleVersion = "2.1.1.2"
foreach  ( $server in $Script:My.ServerActive.Items){
    $sessions.Add(  (initSession -serverHostname $server) )
}
foreach ($ses in $sessions){
   invokeUpdate -session $ses
}

$Script:My.ServerActive.Items : contains a list of server fqdns

Any Ideas or Solutions would save me, thanks!

Nik

Edit 1:

Here is the Error Message:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) + CategoryInfo : NotSpecified: (:) [Get-WindowsUpdate], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,PSWindowsUpdate.GetWindowsUpdate + PSComputerName : fs02.azubi.netz

This will break my Sessions, but the output is $true ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")

Invoke-Command : Cannot bind parameter 'Session'. Cannot convert value "True" to type "System.Management.Automation.Runspaces.PSSession". ...

2
  • Can you paste the output/error into the question instead of linking to an image of the error?
    – codewario
    Commented Aug 24, 2020 at 14:54
  • My first thought here is your remote session isn't running with elevated permissions. Try running this as your DA user on the remote system through PSRemoting, it will return $True if you have administrative permissions and $False if not: ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"). If this is the issue, reply back and I have an answer that should solve this.
    – codewario
    Commented Aug 24, 2020 at 15:02

1 Answer 1

0

To Fix This Problem I had to change the way of Copying to the other System and the Actual call of get-windowsupdate.

The Mooudle has to be in $env:PSModPath, so to fix it you have to copy into one of those folders.

Copy-Item "$modpath\$($Script:My.ModuleVersion)\" -Destination "$psmod\PSWindowsUpdate\$($Script:My.ModuleVersion)" -ToSession $ses -Recurse -ErrorAction Stop 

the Update doesnt need to run Over Invoke Command.

Get-WindowsUpdate -AcceptAll -Install  -IgnoreReboot -ComputerName $session.ComputerName

Hope this will Help if you get a similar Problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.