I'm using SPE to install the TDS .update package file.

How do I get detailed installation log remotely in the script below?

Current PowerShell Script:

$SitecoreInstanceUri = "https://helixsite.local"
$Username = "admin"
$Password = "b"
$SitecorePackagePath = "C:\inetpub\wwwroot\helixsite.local\App_Data\packages\Helix.Project.Website.Master.update"

#Create a new script session
$session = New-ScriptSession -Username $Username -Password $Password -ConnectionUri $SitecoreInstanceUri

Write-Host "Installing Sitecore update package '$SitecorePackagePath' on '$SitecoreInstanceUri'"

$jobId = Invoke-RemoteScript -Session $session -ScriptBlock {
    $package = $using:SitecorePackagePath
    Write-Log "Installing Sitecore update package '$package'"
    Install-UpdatePackage -Path $package -UpgradeAction Upgrade -InstallMode Install
} -AsJob -Verbose

if (!$jobId) { 
    Write-Host "No jobId was created. Please check if your Powershell Remoting is activated on the target instance '$SitecoreInstanceUri'"
}

#doneScript is the script that gets executed every 5 seconds and checks the state of the executed script. It returns a custom PowerShell object with Name, IsDone and Status
$doneScript = {
        $backgroundScriptSession = Get-ScriptSession -Id $using:jobId
        $isDone = $backgroundScriptSession -eq $null -or $backgroundScriptSession.State -ne "Busy"
        [PSCustomObject]@{
            "Name" = $backgroundScriptSession.Id
            "IsDone" = $isDone
            "Status" = "$($backgroundScriptSession.State)"
        }
    }

try {
    $keepRunning = $true
    while($keepRunning) {
        #Execute the doneScript and check the response.
        $response = Invoke-RemoteScript -Session $session -ScriptBlock $doneScript
        if($response -and $response.IsDone) {
            $keepRunning = $false
            Write-Host "Polling job $($response.Name). Status : $($response.Status)."
            Write-Host "Finished polling job $($id)."
        } else {
            Write-Host "Polling job $($response.Name). Status : $($response.Status)."
            Start-Sleep -Seconds 5
        }
    }   
} catch {
    if ($_.Exception.Message -eq "ScriptSessionNotFound,Cognifide.PowerShell.Commandlets.ScriptSessions.GetScriptSessionCommand") {
        Write-Host "Package installation triggered application pool recycling."
    }
    else
    {
     throw
    }
}


Write-Host "Sitecore update package installed."
Stop-ScriptSession -Session $session

Current output is as below:

Polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6. Status : Busy.
Polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6. Status : Busy.
Polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6. Status : Busy.
Polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6. Status : Busy.
... This keeps running for few minutes ...
Polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6. Status : Busy.
Polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6. Status : Busy.
Polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6. Status : Available.
Finished polling job d8400253-9f9e-4718-89d5-0dd633bf3bf6.
Sitecore update package installed.

Expected output:

I'm assuming remotely somehow we should be able to get installation logs as below.

Installing item '/sitecore/system/Settings/Forms/Submit Actions/Send E-mail'  
Installing item '/sitecore/system/Settings/Forms/Submit Actions/Submit Action'

I was reading about the output type from Install-UpdatePackage command is List<ContingencyEntry> but I'm not sure how to read it remotely and display that in the output locally.

What I have tried so far:

I have tried below variation in the script above but it doesn't change anything in output in the console.

$jobId = Invoke-RemoteScript -Session $session -ScriptBlock {
    $package = $using:SitecorePackagePath
    Write-Log "Installing Sitecore update package '$package'"
    Install-UpdatePackage -Path $package -UpgradeAction Upgrade -InstallMode Install | ForEach-Object { Write-Verbose $_.ShortDescription; }
} -AsJob -Verbose
up vote 1 down vote accepted

I have got the PowerShell script working as follows:

Script:

$SitecoreInstanceUri = "https://helixsite.local"
$Username = "admin"
$Password = "b"
$SitecorePackagePath = "C:\inetpub\wwwroot\helixsite.local\App_Data\packages\Helix.Project.Website.Master.update"

#Create a new script session
$session = New-ScriptSession -Username $Username -Password $Password -ConnectionUri $SitecoreInstanceUri

Write-Host "Installing Sitecore update package '$SitecorePackagePath' on '$SitecoreInstanceUri'"

$jobId = Invoke-RemoteScript -Session $session -ScriptBlock {
    $package = $using:SitecorePackagePath
    Write-Log "Installing Sitecore update package '$package'"
    Install-UpdatePackage -Path $package -UpgradeAction Upgrade -InstallMode Install | Format-Table ShortDescription -Verbose
} -AsJob -Verbose

if (!$jobId) { 
    Write-Host "No jobId was created. Please check if your Powershell Remoting is activated on the target instance '$SitecoreInstanceUri'"
}

#doneScript is the script that gets executed every 5 seconds and checks the state of the executed script. It returns a custom PowerShell object with Name, IsDone and Status
$doneScript = {
        $backgroundScriptSession = Get-ScriptSession -Id $using:jobId
        $isDone = $backgroundScriptSession -eq $null -or $backgroundScriptSession.State -ne "Busy"
        [PSCustomObject]@{
            "Name" = $backgroundScriptSession.Id
            "IsDone" = $isDone
            "Status" = "$($backgroundScriptSession.State)"
        }
    }

try {
    $keepRunning = $true
    while($keepRunning) {
        #Execute the doneScript and check the response.
        $response = Invoke-RemoteScript -Session $session -ScriptBlock $doneScript
        if($response -and $response.IsDone) {
            $keepRunning = $false
            Write-Host "Polling job $($response.Name). Status : $($response.Status)."
            Write-Host "Finished polling job $($id)."
        } else {
            Write-Host "Polling job $($response.Name). Status : $($response.Status)."
            Start-Sleep -Seconds 5
        }
    }   
} catch {
    if ($_.Exception.Message -eq "ScriptSessionNotFound,Cognifide.PowerShell.Commandlets.ScriptSessions.GetScriptSessionCommand") {
        Write-Host "Package installation triggered application pool recycling."
    }
    else
    {
     throw
    }
}

Invoke-RemoteScript -Session $session -ScriptBlock {
    $ss = Get-ScriptSession -Id $using:JobId
    $ss | Receive-ScriptSession
}

Write-Host "Sitecore update package installed."
Stop-ScriptSession -Session $session

What changed here

Changed from this,

Install-UpdatePackage -Path $package -UpgradeAction Upgrade -InstallMode Install

To this script,

Install-UpdatePackage -Path $package -UpgradeAction Upgrade -InstallMode Install | Format-Table ShortDescription -Verbose

If there are a lot of updates included in the update package then the response can get pretty huge and it can cause request timeout or connection timeout.

So to avoid it timeout issues, I'm reading just one property ShortDescription in the response from List<ContingencyEntry> which is returned from Install-UpdatePackage. We can cut down on the response by using filter Where-Object in the script above if needed. Also, it is required to add -Verbose to this part of script to receive the response remotely.

Secondly to get the response in the local console OR to the CI/CD output I have added this section in the end right before closing the session,

Invoke-RemoteScript -Session $session -ScriptBlock {
    $ss = Get-ScriptSession -Id $using:JobId
    $ss | Receive-ScriptSession
}

I hope this helps.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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