Reply to Message

View discussion in a popup

Replying to:
vin01
Expert
Expert

This is what I have passed inside the guestos. Actually $task.Task.Result.ScriptOutput contains multiple values for each vm.

$script=@'

$results=@()

$ErrorActionPreference = "SilentlyContinue"

If ($Error) {

$Error.Clear()

}

$updatesession=New-Object -ComObject Microsoft.update.session

$Criteria="IsInstalled=0 and Type=Software and IsHidden=0"

$searchresult=$updateSession.CreateupdateSearcher().Search("IsInstalled=0 and Type='Software' and IsHidden=0").Updates

if(-not $searchresult.Count){

$results+="There are no applicable updates for this computer."

}

else{

$pendingdownloads=$searchresult | Where-Object {$_.IsDownloaded -eq $false}

if(($pendingdownloads |Select-Object IsDownloaded).count -ne '0'){

$downloadercall=$updatesession.CreateUpdateDownloader()

$downloadercall.Updates=New-Object -ComObject Microsoft.update.updatecoll

foreach($pendingdownload in $pendingdownloads){

[void]$downloadercall.Updates.add($pendingdownload)

$downloadercall.Download() |Out-Null

[void]$downloadercall.Updates.RemoveAt(0)

}}

$updatesession=New-Object -ComObject Microsoft.update.session

$Criteria="IsInstalled=0 and Type=Software and IsHidden=0"

$searchresult=$updateSession.CreateupdateSearcher().Search("IsInstalled=0 and Type='Software' and IsHidden=0").Updates

$downloadedupdates = $searchresult  | Where-Object {$_.IsDownloaded -eq $true}

$updatercall=$updatesession.CreateUpdateInstaller()

$updatercall.Updates= New-Object -ComObject Microsoft.update.updatecoll

$results+=foreach($singleupdate in $downloadedupdates){

[void]$updatercall.Updates.add($singleupdate)

$installstatus=$updatercall.install()

[void]$updatercall.Updates.RemoveAt(0)

''|Select-Object @{N="KB";E={$kbnumb=$singleupdate.Title;$kbnumb.Substring($kbnumb.IndexOf("KB")).Trimend(")")}},@{N="Installstatus";E={if($installstatus.ResultCode -eq '2'){

'KBInstalled'}

elseif($installstatus.ResultCode -eq '3'){

'KBInstall Succeeded with errors'}

elseif($installstatus.ResultCode -eq '4'){

'Kb Failed to install'}

elseif($installstatus.ResultCode -eq '5'){

'KBAborted'}}}

}

}

$results |ConvertTo-Csv -NoTypeInformation

'@

$templatevm=Get-VM 'TestVM01'

$sInvoke = @{

VM            = $templatevm.Name

GuestUser     = 'administrator'

GuestPassword = 'password'

ScriptText    = $script

ScriptType    = 'Powershell'

RunAsync      = $true

Confirm       = $false

}

$tasks += @{

VM = $templatevm.Name

Task = Invoke-VMScript @sInvoke

}

Write-Host "Invoke Operation is performed on all the templates and waiting for results to be collected"

while($tasks.Task.State -contains 'Running'){

sleep 2

}

foreach ($task in $tasks) {

$task | Select @{N='VM';E={$_.VM}},

@{N='State';E={$_.Task.State}},

@{N='Error';E={$_.Task.TerminatingError.Message}},

#@{N ='KB'; E={(($_.Task.Result.ScriptOutput |ConvertFrom-Csv).KB)}},

#@{N ='Installstatus'; E={(($_.Task.Result.ScriptOutput |ConvertFrom-Csv).Installstatus)}}

}

This is the actual output of the single VM.

pastedImage_1.png

------------------------------------------------------------------------------

I am expecting Output like this for single VM.

VM : VM01

State: Success

Error:

KB: KB4565628

Installstatus: KBInstalled

VM : VM01

State: Success

Error:

KB: KB4565511

Installstatus: KBInstalled

Regards Vineeth.K
Reply
0 Kudos