1

When I execute this script in Sitecore Powershell ISE:

Write-Host 'This is a test'

$count = 0
$customObj = [PsCustomObject]@{
    Ask = 'Hello'
    Jeeves = 'World'
    Count = $count
}

$customObj

while ($count -lt 10) {
    Start-Sleep -seconds 1
    $count++
    $customObj.Count = $count
    $customObj
}

It produces the following output, and each line appears in turn as the script progresses.

This is a test

Ask   Jeeves Count
---   ------ -----
Hello World      0
Hello World      1
Hello World      2
Hello World      3
Hello World      4
Hello World      5
Hello World      6
Hello World      7
Hello World      8
Hello World      9
Hello World     10

If I try the same via a remote script call:

PS C:\Windows\System32> $session = New-ScriptSession ...
PS C:\Windows\System32> Invoke-RemoteScript -Session $session -ScriptBlock {
>>     Write-Host 'This is a test'
>>
>>     $count = 0
>>     $customObj = [PsCustomObject]@{
>>         Ask = 'Hello'
>>         Jeeves = 'World'
>>         Count = $count
>>     }
>>
>>     $customObj
>>
>>     while ($count -lt 10) {
>>         Start-Sleep -seconds 1
>>         $count++
>>         $customObj.Count = $count
>>         $customObj
>>     }
>> }

The output comes all at once at the very end, and the count is always 10. Additionally the Write-Host line is not captured. What's going on here?

Ask   Jeeves Count
---   ------ -----
Hello World     10
Hello World     10
Hello World     10
Hello World     10
Hello World     10
Hello World     10
Hello World     10
Hello World     10
Hello World     10
Hello World     10
Hello World     10

3 Answers 3

0

You can modify the script as below to get the desired output via running through the remote script as below.

Import-Module -Name SPE
$session = New-ScriptSession ...

Write-Host 'This is a test'
$count = 0
while ($count -lt 10) {
    $count++
    $output = Invoke-RemoteScript -Session $session -ScriptBlock {
        $customObj = [PsCustomObject]@{
            Ask = 'Hello'
            Jeeves = 'World'
            Count = $using:count
        }
        $customObj
    }
    $output
    Start-Sleep -seconds 1
}

This will produce the following output as desired.

enter image description here

You can refer the variable i.e. count from outside inside the script block through $using variable. Also note in order to print the values from script block you can add below code inside the block.

function Write-Verbose {
     param([string]$Message)
            Microsoft.PowerShell.Utility\Write-Host -Message $Message -Verbose 4>&1
}
Write-Verbose "Hello from the other side"

Hope this helps!!! Let me know in case you have any queries.

0

Have you seen the documentation on the Wait-RemoteScriptSession function?

There is a delay parameter which allows you to adjust the polling frequency.

Import-Module -Name SPE
$session = New-ScriptSession -Username admin -Password b -ConnectionUri https://remotesitecore
$jobId = Invoke-RemoteScript -Session $session -ScriptBlock {
        "master", "web" | Get-Database | 
            ForEach-Object { 
                [Sitecore.Globals]::LinkDatabase.Rebuild($_)
            }
} -AsJob
Wait-RemoteScriptSession -Session $session -Id $jobId -Delay 5 -Verbose
Stop-ScriptSession -Session $session
0

The problem you're seeing with Invoke-RemoteScript happens because of how remote sessions deal with results and handle tasks one after another.

You can change the script like this to get what you want when you run it remotely:

$session = New-ScriptSession ...

$results = Invoke-RemoteScript -Session $session -ScriptBlock {
    $count = 0
    $customObj = [PsCustomObject]@{
        Ask = 'Hello'
        Jeeves = 'World'
        Count = $count
    }

    # Store output in an array to capture all states
    $output = @()
    $output += $customObj

    while ($count -lt 10) {
        Start-Sleep -seconds 1
        $count++
        $customObj.Count = $count
        $output += $customObj
    }
    
    return $output
}

# Process results
$results | ForEach-Object {
    Write-Host $_.Ask $_.Jeeves $_.Count
}

Output

Ask   Jeeves Count
---   ------ -----
Hello World 0
Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5
Hello World 6
Hello World 7
Hello World 8
Hello World 9
Hello World 10

Note: If you want to see the output in real time while the script is executing, you'll need to implement a different method (e.g., logging to a file, or using other forms of notifications), as remote scripts generally buffer output until completion.

Hope this helps!!!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.