how to write invoke-vmscript output which contains multiple lines

This post was originally published on this site

When I execute the code which is in $script  inside a guest os I can get the expected results but using Invoke-VMScript the results are not writing properly.

 

$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
'@
foreach($templatevm in (Get-VM "*Template*")){
$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 ='Result'; E={$_.Task.Result.ScriptOutput.Split("`n") |Where-Object { $_ -ne '' } | %{$_.Trim("`r`n")}}}
}

 

Result of $script inside the guest OS.

 

 

But when I executed using invoke-vmscript the result is below:

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.