-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWatch-Command.ps1
More file actions
170 lines (127 loc) · 5.26 KB
/
Watch-Command.ps1
File metadata and controls
170 lines (127 loc) · 5.26 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
Function Watch-Command {
<#
.SYNOPSIS
Runs a scriptblock or the preceeding pipeline repeatedly until there is change.
.DESCRIPTION
The Watch-Command cmdlet runs a specified scriptblock repeatedly at the specified interval (or
every 1 second by default) and returns the result of the scriptblock when the output has changed.
For the command to work the specified scriptblock must return a result to the pipeline.
.PARAMETER ScriptBlock
The scriptblock to execute, specified via curly braces. If you provide input via the pipleine that
isn't a scriptblock then the entire invocation line that preceeded the cmdlet will be used as the
scriptblock input.
.PARAMETER Seconds
Number of seconds to wait between checks. Default = 1
.PARAMETER Difference
Switch: Use to only output items in the collection that have changed
dditions or modifications).
.PARAMETER Continuous
Switch: Run continuously (even after a change has occurred) until exited with CTRL+C.
.PARAMETER AsString
Switch: Converts the result of the scriptblock into an array of strings for comparison.
.PARAMETER ClearScreen
Switch: Clears the screen between each result. You can also use 'cls' as an alias.
.PARAMETER PassThru
Switch: Passes through the initial result from the command (before any change has occurred).
.PARAMETER Property
Manually specify one or more property names to be used for comparison. If not specified,
the default display property set is used. If there is not a default display property set,
all properties are used. You can also use '*' to force all properties.
.EXAMPLE
Watch-Command -ScriptBlock { Get-Process }
Runs Get-Process and waits for any returns the result when the data has changed.
.EXAMPLE
Get-Service | Watch-Command -Diff -Cont
Runs Get-Service and returns any differences in the resultant data, continuously until interrupted
by CTRL+C.
.EXAMPLE
Watch-Command { Get-Content test.txt } -Difference -Verbose -ClearScreen
Uses Get-Content to monitor test.txt. Shows any changes and clears the screen between changes.
.EXAMPLE
Get-ChildItem | Watch-Command -Difference -AsString
Monitors the result of GEt-ChildItem for changes, returns any differences. Treats the input as
strings not objects.
.EXAMPLE
Get-Process | Watch-Command -Difference -Property processname,id -Continuous
Monitors Get-Process for differences in the specified properties only, continues until interrupted
by CTRL+C.
#>
[cmdletbinding()]
Param(
[parameter(ValueFromPipeline, Mandatory)]
[object]
$ScriptBlock,
[int]
$Seconds = 1,
[switch]
$Difference,
[switch]
$Continuous,
[switch]
$AsString,
[alias('cls')]
[switch]
$ClearScreen,
[switch]
$PassThru,
[string[]]
$Property
)
if ($ScriptBlock -isnot [scriptblock]) {
if ($MyInvocation.PipelinePosition -gt 1) {
$ScriptBlock = [Scriptblock]::Create( ($MyInvocation.Line -Split "\|\s*$($MyInvocation.InvocationName)")[0] )
}
else {
Throw 'The -ScriptBlock parameter must be provided an object of type ScriptBlock unless invoked via the Pipeline.'
}
}
Write-Verbose "Started executing $($ScriptBlock | Out-String)"
$FirstResult = Invoke-Command $ScriptBlock
if ($AsString) {
$FirstResult = $FirstResult | Out-String -Stream
}
elseif (($FirstResult | Select-Object -First 1) -isnot [string]){
if (-not $Property) {
$Property = ($FirstResult | Select-Object -First 1).PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames
}
if (-not $Property -or $Property -eq '*') {
$Property = ($FirstResult | Select-Object -First 1).PSObject.Properties.Name
}
Write-Verbose "Watched properties: $($Property -Join ',')"
}
if ($PassThru) {
$FirstResult
}
do {
do {
if ($Result) {
Start-Sleep $Seconds
}
if ($ClearScreen) {
Clear-Host
}
$Result = Invoke-Command $ScriptBlock
if ($AsString) {
$Result = $Result | Out-String -Stream
}
$CompareParams = @{
ReferenceObject = @($FirstResult | Select-Object)
DifferenceObject = @($Result | Select-Object)
}
if ($Property) {
$CompareParams.Add('Property', $Property)
}
$Diff = Compare-Object @CompareParams -PassThru
}
until ($Diff)
Write-Verbose "Change occurred at $(Get-Date)"
if ($Difference) {
$Diff | Where-Object {$_.SideIndicator -eq '=>'}
}
else {
$Result
}
$FirstResult = $Result
}
until (-not $Continuous)
}