-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventcallbacks.ps1
71 lines (56 loc) · 2.16 KB
/
eventcallbacks.ps1
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
$asyncCallback = {
Param (
# Event source object
[System.Management.Automation.Powershell]
$sender,
# Inheritor of [System.EventArgs]
[System.Management.Automation.PSInvocationStateChangedEventArgs]
$e
)
# Ignore initial state change on startup
if ($e.InvocationStateInfo.State -eq [System.Management.Automation.PSInvocationState]::Running)
{
return
}
Write-Host $sender.Message
Write-Host "Event Fired!"
Write-Host ("Invocation State: {0}" -f $e.InvocationStateInfo.State)
# Use the NoteProperty references attached to the Powershell object by Add-Member
[void]$sender.EndInvoke($sender.AsyncResult)
$sender.Dispose()
#
# You can unregister the event from within the event handler, but you
# shouldn't do so if you plan on recycling/restarting the background
# powershell instance.
#
# Unregister the event subscription
Unregister-Event -SourceIdentifier $sender.EventSubscriber.Name
}
$ps = [PowerShell]::Create()
$rs = [RunspaceFactory]::CreateRunspace()
$rs.Open()
$ps.Runspace = $rs
$ps.AddScript( {
#Get-Service
Get-Process
Start-Sleep -Seconds 2
} )
#
# Subscribe to the Powershell state changed event. Attach the registration object
# to the Powershell object for future reference.
#
Add-Member -InputObject $ps -MemberType NoteProperty -Name EventSubscriber -Value (
Register-ObjectEvent -InputObject $ps -EventName InvocationStateChanged -Action $asyncCallback)
<#
# This call structure is unnecessary as you aren't using the InvocationSettings
#
# $psis = New-Object Management.Automation.PSInvocationSettings
# $aResult = $ps.BeginInvoke($psdcInputs, $psdcOutputs, $psis, $asyncCallback, $ps)
#>
Add-Member -InputObject $ps -MemberType NoteProperty -Name Message -Value (
"Hello World! It's Me {0}" -f $ps.EventSubscriber.Name)
$psdcInputs = New-Object Management.Automation.PSDataCollection[String]
$psdcInputs.Complete()
$psdcOutputs = New-Object Management.Automation.PSDataCollection[Object]
Add-Member -InputObject $ps -MemberType NoteProperty -Name AsyncResult -Value (
$ps.BeginInvoke($psdcInputs, $psdcOutputs))