-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathWait-RemoteScriptSession.ps1
More file actions
97 lines (80 loc) · 3.29 KB
/
Wait-RemoteScriptSession.ps1
File metadata and controls
97 lines (80 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function Wait-RemoteScriptSession {
<#
.SYNOPSIS
Polls for the specified job until it has completed.
.DESCRIPTON
The Wait-RemoteScriptSession command waits for a ScriptSession to complete processing.
.PARAMETER Session
The ScriptSession object to poll.
.PARAMETER Id
The id of the asynchronous job returned when calling Invoke-RemoteScript with the -AsJob switch.
.PARAMETER Delay
The polling interval in seconds.
.EXAMPLE
The following example remotely rebuilds link databases as a job and waits for it to complete.
The Invoke-RemoteScript command returns a ScriptSession object.
$session = New-ScriptSession -Username admin -Password b -ConnectionUri http://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
.LINK
New-ScriptSession
.LINK
Invoke-RemoteScript
.LINK
Stop-ScriptSession
.LINK
Wait-RemoteSitecoreJob
#>
[CmdletBinding()]
param(
[Parameter(ParameterSetName='Session')]
[ValidateNotNull()]
[pscustomobject]$Session,
[ValidateNotNullOrEmpty()]
[string]$Id,
[int]$Delay = 1,
[Parameter()]
[switch]$Raw
)
$doneScript = {
$backgroundScriptSession = Get-ScriptSession -Id $using:id -ErrorAction SilentlyContinue
$isDone = $backgroundScriptSession -eq $null -or $backgroundScriptSession.State -ne "Busy"
$status = "$($backgroundScriptSession.State)"
if([string]::IsNullOrEmpty($status)) { $status = "Unknown" }
[PSCustomObject]@{
"Name" = $using:id
"IsDone" = $isDone
"Status" = $status
}
}
$finishScript = {
$backgroundScriptSession = Get-ScriptSession -Id $using:id -ErrorAction SilentlyContinue
$backgroundScriptSession | Receive-ScriptSession
}
$keepRunning = $true
while($keepRunning) {
$response = Invoke-RemoteScript -Session $session -ScriptBlock $doneScript -Verbose
if($response -eq $null) {
Write-Verbose "Stopped polling job $($id). No results were returned from the service."
break
} elseif ($response -eq "login failed") {
Write-Verbose "Stopped polling job. Login with the specified account failed."
break
}
if($response -and $response.IsDone) {
$keepRunning = $false
Write-Verbose "Polling job $($response.Name). Status : $($response.Status)."
Write-Verbose "Finished polling job $($id)."
Invoke-RemoteScript -Session $session -ScriptBlock $finishScript -Raw:$Raw.IsPresent
} else {
Write-Verbose "Polling job $($response.Name). Status : $($response.Status)."
Start-Sleep -Seconds $Delay
}
}
}