├── README.md
├── Versions
└── ConfigMgr_Task_Sequence_Monitor.xml
├── LICENSE
└── 1.6_ConfigMgr TS Monitor.ps1
/README.md:
--------------------------------------------------------------------------------
1 | # ConfigMgr-Task-Sequence-Monitor
2 | A tool to monitor and report on task sequence executions in ConfigMgr
3 |
--------------------------------------------------------------------------------
/Versions/ConfigMgr_Task_Sequence_Monitor.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 1.0
6 |
7 | Initial Version
8 |
9 | 2015-01-01
10 |
11 |
12 | 1.1
13 |
14 | Added capability to link data from MDT with data from ConfigMgr
15 |
16 | 2015-01-01
17 |
18 |
19 | 1.2
20 |
21 | Added capability to generate an HTML deployment report
22 | Fixed memory leak
23 | Improved integration of MDT and ConfigMgr data
24 | Moved settings into a seperate window
25 |
26 | 2015-November-17
27 |
28 |
29 | 1.2.1
30 |
31 | Fixed code bug that caused the app to open and immediately close on some machines
32 |
33 | 2015-November-19
34 |
35 |
36 | 1.2.2
37 |
38 | Fixed the issue where if only one task sequence is active in the environment, it will display as an array of string characters instead of an array of items
39 |
40 | 2015-November-20
41 |
42 |
43 | 1.3.0
44 |
45 | Added the capability to change the displayed date/time from the MDT/ConfigMgr default (UTC) to your local system timezone
46 |
47 | 2015-November-23
48 |
49 |
50 | 1.3.1
51 |
52 | Fixed an issue where the HTML report returns no results when the dateformat of the default system language of SQL server is not using "mdy" format. Search dates are now passed to SQL server in ISO8601 standard format for compatibility with all SQL dateformats
53 |
54 | 2016-January-15
55 |
56 |
57 | 1.4
58 |
59 | Fixed the issue where duplicate records might be returned when the computer being deployed already exists in the ConfigMgr database (rebuild scenario)
60 |
61 | 2016-February-10
62 |
63 |
64 | 1.5
65 |
66 | Restyled the UI with a Modern Metro style
67 |
68 | 2016-October-13
69 |
70 |
71 | 2.0
72 |
73 | tbc...
74 |
75 | 2016-May-01
76 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/1.6_ConfigMgr TS Monitor.ps1:
--------------------------------------------------------------------------------
1 | #requires -Version 3
2 |
3 | #region Add Assemblies
4 | Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms, WindowsFormsIntegration
5 | $code = @"
6 | using System;
7 | using System.Drawing;
8 | using System.Runtime.InteropServices;
9 |
10 | namespace System
11 | {
12 | public class IconExtractor
13 | {
14 |
15 | public static Icon Extract(string file, int number, bool largeIcon)
16 | {
17 | IntPtr large;
18 | IntPtr small;
19 | ExtractIconEx(file, number, out large, out small, 1);
20 | try
21 | {
22 | return Icon.FromHandle(largeIcon ? large : small);
23 | }
24 | catch
25 | {
26 | return null;
27 | }
28 |
29 | }
30 | [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
31 | private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons);
32 |
33 | }
34 | }
35 | "@
36 | Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing
37 |
38 | # Mahapps Library
39 | if (Test-Path -Path "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\MahApps.Metro.dll")
40 | {
41 | [System.Reflection.Assembly]::LoadFrom("$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\MahApps.Metro.dll") | out-null
42 | [System.Reflection.Assembly]::LoadFrom("$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\System.Windows.Interactivity.dll") | out-null
43 | }
44 |
45 | if (Test-Path -Path "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\MahApps.Metro.dll")
46 | {
47 | [System.Reflection.Assembly]::LoadFrom("${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\MahApps.Metro.dll") | out-null
48 | [System.Reflection.Assembly]::LoadFrom("${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\System.Windows.Interactivity.dll") | out-null
49 | }
50 |
51 | #endregion
52 |
53 | #region GUI and Variables
54 | ### Main Window ###
55 | [xml]$xaml = @"
56 |
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 | $hash = [hashtable]::Synchronized(@{})
155 | $reader = (New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml)
156 | $hash.Window = [Windows.Markup.XamlReader]::Load( $reader )
157 | $global:PSInstances = @()
158 | $Global:Timezones = @()
159 |
160 | $hash.TaskSequence = $hash.Window.FindName('TaskSequence')
161 | $hash.TimePeriod = $hash.Window.FindName('TimePeriod')
162 | $hash.ErrorsOnly = $hash.Window.FindName('ErrorsOnly')
163 | $hash.ComputerName = $hash.Window.FindName('ComputerName')
164 | $hash.RefreshPeriod = $hash.Window.FindName('RefreshPeriod')
165 | $hash.RefreshNow = $hash.Window.FindName('RefreshNow')
166 | $hash.DataGrid = $hash.Window.FindName('DataGrid')
167 | $hash.ActionOutput = $hash.Window.FindName('ActionOutput')
168 | $hash.MDTIntegrated = $hash.Window.FindName('MDTIntegrated')
169 | $hash.DeploymentStatus = $hash.Window.FindName('DeploymentStatus')
170 | $hash.CurrentStep = $hash.Window.FindName('CurrentStep')
171 | $hash.StepName = $hash.Window.FindName('StepName')
172 | $hash.PercentComplete = $hash.Window.FindName('PercentComplete')
173 | $hash.MDTStartTime = $hash.Window.FindName('MDTStartTime')
174 | $hash.MDTEndTime = $hash.Window.FindName('MDTEndTime')
175 | $hash.MDTElapsedTime = $hash.Window.FindName('MDTElapsedTime')
176 | $hash.SettingsButton = $hash.Window.FindName('SettingsButton')
177 | $hash.ReportButton = $hash.Window.FindName('ReportButton')
178 | $hash.ErrorCount = $hash.Window.FindName('ErrorCount')
179 |
180 | $hash.DeploymentStatusLabel = $hash.Window.FindName('DeploymentStatusLabel')
181 | $hash.CurrentStepLabel = $hash.Window.FindName('CurrentStepLabel')
182 | $hash.StepNameLabel = $hash.Window.FindName('StepNameLabel')
183 | $hash.PercentCompleteLabel = $hash.Window.FindName('PercentCompleteLabel')
184 | $hash.StartLabel = $hash.Window.FindName('StartLabel')
185 | $hash.EndLabel = $hash.Window.FindName('EndLabel')
186 | $hash.ElapsedLabel = $hash.Window.FindName('ElapsedLabel')
187 | $hash.ProgressLabel = $hash.Window.FindName('ProgressLabel')
188 |
189 | $hash.ProgressBar = $hash.Window.FindName('ProgressBar')
190 | if (Test-Path -Path "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
191 | {
192 | #$hash.Window.Icon = "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
193 | }
194 | if (Test-Path -Path "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
195 | {
196 | #$hash.Window.Icon = "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
197 | }
198 |
199 |
200 | ### Settings Window ###
201 | [xml]$xaml2 = @"
202 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 | smsagent.wordpress.com
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 | "@
274 | $reader = (New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml2)
275 | $hash.Window2 = [Windows.Markup.XamlReader]::Load( $reader )
276 | $hash.SQLServer = $hash.Window2.FindName('SQLServer')
277 | $hash.Database = $hash.Window2.FindName('Database')
278 | $hash.MDTURL = $hash.Window2.FindName('MDTURL')
279 | $hash.ConnectSQL = $hash.Window2.FindName('ConnectSQL')
280 | $hash.TSList = $hash.Window2.FindName('TSList')
281 | $hash.StartDate = $hash.Window2.FindName('StartDate')
282 | $hash.EndDate = $hash.Window2.FindName('EndDate')
283 | $hash.GenerateReport = $hash.Window2.FindName('GenerateReport')
284 | $hash.SettingsTab = $hash.Window2.FindName('SettingsTab')
285 | $hash.ReportTab = $hash.Window2.FindName('ReportTab')
286 | $hash.Tabs = $hash.Window2.FindName('Tabs')
287 | $hash.Working = $hash.Window2.FindName('Working')
288 | $hash.Runasadmin = $hash.Window2.FindName('Runasadmin')
289 | $hash.ReportProgress = $hash.Window2.FindName('ReportProgress')
290 | $hash.Link1 = $hash.Window2.FindName('Link1')
291 | $hash.DTFormat = $hash.Window2.FindName('DTFormat')
292 |
293 | if (Test-Path -Path "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
294 | {
295 | #$hash.Window2.Icon = "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
296 | $Hash.Window2.ShowInTaskbar = $true
297 | }
298 | if (Test-Path -Path "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
299 | {
300 | #$hash.Window2.Icon = "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
301 | $Hash.Window2.ShowInTaskbar = $true
302 | }
303 |
304 | $script:SQLServer = $hash.SQLServer.Text
305 | $Script:Database = $hash.Database.Text
306 | #endregion
307 |
308 | #region Icons and Runspacepool
309 | # Output SystemIcons to bmps
310 | $icons = @()
311 | $global:greentickiconpath = "$env:temp\GreenTick.bmp"
312 | $icons += $greentickiconpath
313 | $global:redcrossiconpath = "$env:temp\RedCross.bmp"
314 | $icons += $redcrossiconpath
315 |
316 | if (!(Test-Path $greentickiconpath))
317 | {
318 | $global:greentickicon = [System.IconExtractor]::Extract('comres.dll',8,$true).ToBitmap()
319 | $greentickicon.save("$greentickiconpath")
320 | }
321 | if (!(Test-Path $redcrossiconpath))
322 | {
323 | $global:redcrossicon = [System.IconExtractor]::Extract('comres.dll',10,$true).ToBitmap()
324 | $redcrossicon.save("$redcrossiconpath")
325 | }
326 |
327 | $script:RunspacePool = [runspacefactory]::CreateRunspacePool()
328 | $RunspacePool.ApartmentState = 'STA'
329 | $RunspacePool.ThreadOptions = 'ReUseThread'
330 | $RunspacePool.Open()
331 | #endregion
332 |
333 | #region Functions
334 |
335 | Function Get-DateTimeFormat
336 | {
337 | if ([System.TimeZone]::CurrentTimeZone.IsDaylightSavingTime($(Get-Date)))
338 | {
339 | $TimeZone = [System.TimeZone]::CurrentTimeZone.DaylightName
340 | }
341 | Else
342 | {
343 | $TimeZone = [System.TimeZone]::CurrentTimeZone.StandardName
344 | }
345 |
346 | #$Global:Timezones = @()
347 | $obj = New-Object -TypeName psobject -Property @{
348 | TimeZone = 'UTC'
349 | }
350 | $Global:Timezones = [Array]$Timezones + $obj
351 | $obj = New-Object -TypeName psobject -Property @{
352 | TimeZone = $TimeZone
353 | }
354 | $Global:Timezones = [Array]$Timezones + $obj
355 | }
356 |
357 | Function Get-TaskSequenceList
358 | {
359 | # Set variables
360 | $script:SQLServer = $hash.SQLServer.Text
361 | $Script:Database = $hash.Database.Text
362 |
363 | # If SQLinstance not populated, ask for connection
364 | if ($SQLServer -eq '')
365 | {
366 | $hash.ActionOutput.Text = 'No SQL Server defined. Click Settings, and set the SQL Server, database and MDT URL if applicable.'
367 | return
368 | }
369 |
370 | # Connect to SQL server
371 | try
372 | {
373 | $connectionString = "Server=$SQLServer;Database=$Database;Integrated Security=SSPI;"
374 | $connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
375 | $connection.ConnectionString = $connectionString
376 | $connection.Open()
377 | $hash.ActionOutput.Text = 'Connected to SQL Server database. Select a Task Sequence.'
378 | }
379 | catch
380 | {
381 | $hash.ActionOutput.Text = '[ERROR} Could not connect to SQL Server database!'
382 | return
383 | }
384 | # Run SQL query
385 | $Query = "
386 | SELECT DISTINCT summ.SoftwareName AS 'Task Sequence'
387 | FROM vDeploymentSummary summ
388 | WHERE (summ.FeatureType=7)
389 | ORDER BY summ.SoftwareName
390 | "
391 | $command = $connection.CreateCommand()
392 | $command.CommandText = $Query
393 | $result = $command.ExecuteReader()
394 | $table = New-Object -TypeName 'System.Data.DataTable'
395 | $table.Load($result)
396 | $connection.Close()
397 |
398 | # Load data into psobject
399 | $global:Views = @()
400 | Foreach ($Row in $table.Rows)
401 | {
402 | $obj = New-Object -TypeName psobject
403 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'TS' -Value $Row.'Task Sequence'
404 | $global:Views = [Array]$Views + $obj
405 | }
406 |
407 | # Output to Task Sequence combobox
408 | $hash.Window.Dispatcher.Invoke(
409 | [action]{
410 | $hash.TaskSequence.ItemsSource = [Array]$Views.TS
411 | })
412 | }
413 |
414 | Function Get-TaskSequenceData
415 | {
416 | param ($hash,$RunspacePool)
417 |
418 | $code =
419 | {
420 | param($hash,$SQLServer,$Database,$TimePeriod,$ErrorsOnly,$ComputerName,$TS,$MDTIntegrated,$URL,$DTFormat)
421 |
422 | # Notify of data retrieval
423 | $hash.Window.Dispatcher.Invoke(
424 | [action]{
425 | $hash.ActionOutput.Text = 'Retrieving data...'
426 | $hash.DataGrid.ItemsSource = ''
427 | })
428 |
429 | # Set variable values
430 | if ($MyGUID)
431 | {
432 | Remove-Variable -Name MyGuid
433 | }
434 | if ($Unknowns) # put after display ####
435 | {
436 | Remove-Variable -Name Unknowns
437 | }
438 |
439 | if ($ErrorsOnly -eq 'True')
440 | {
441 | $ExitCode = 0
442 | }
443 | else
444 | {
445 | $ExitCode = 999999999999999999999999
446 | }
447 |
448 | if ($ComputerName -eq '-All-' -or $ComputerName -eq '' -or $ComputerName -eq $Null)
449 | {
450 | $SQLComputerName = '%'
451 | }
452 | Else
453 | {
454 | $SQLComputerName = $ComputerName
455 | }
456 |
457 | $greentickiconpath = "$env:temp\GreenTick.bmp"
458 | $redcrossiconpath = "$env:temp\RedCross.bmp"
459 |
460 | # Connect to SQL server
461 | try
462 | {
463 | $connectionString = "Server=$SQLServer;Database=$Database;Integrated Security=SSPI;"
464 | $connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
465 | $connection.ConnectionString = $connectionString
466 | $connection.Open()
467 | }
468 | catch
469 | {
470 | $MyError = $_.Exception.Message
471 | $hash.Window.Dispatcher.Invoke(
472 | [action]{
473 | $hash.ActionOutput.Text = "[ERROR} Could not connect to SQL Server database! $MyError"
474 | })
475 | return
476 | }
477 |
478 | if ($MDTIntegrated -eq 'True')
479 | {
480 | # Get Unknown Computers from ConfigMgr database if there are any
481 | $Query = "
482 | Select Distinct Name0,
483 | SMBIOS_GUID0 as 'GUID'
484 | from vSMS_TaskSequenceExecutionStatus tes
485 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
486 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
487 | where tsp.Name = '$TS'
488 | and DATEDIFF(hour,ExecutionTime,GETDATE()) < $TimePeriod
489 | --and Name0 like '$SQLComputerName'
490 | and ExitCode <> $ExitCode
491 | ORDER BY Name0 Desc
492 | "
493 | $command = $connection.CreateCommand()
494 | $command.CommandText = $Query
495 | $result = $command.ExecuteReader()
496 | $table = New-Object -TypeName 'System.Data.DataTable'
497 | $table.Load($result)
498 |
499 |
500 | # Gather unknowns into PS object
501 | $UnknownComputers = @()
502 | Foreach ($Row in $table.Rows | Where-Object -FilterScript {
503 | $_.Name0 -eq 'Unknown'
504 | })
505 | {
506 | $obj = New-Object -TypeName psobject
507 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ComputerName' -Value $Row.Name0
508 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'GUID' -Value $Row.GUID
509 | $UnknownComputers += $obj
510 | }
511 |
512 |
513 | # If there are unknowns, get computername from MDT
514 | if ($UnknownComputers.Count -ge 1)
515 | {
516 | $URL1 = $URL.Replace('Computers','ComputerIdentities')
517 |
518 | # Get ID numbers and Identifiers (GUIDs)
519 | function GetMDTIDs
520 | {
521 | param ($URL)
522 | $Data = Invoke-RestMethod -Uri $URL
523 | foreach($property in ($Data.content.properties) )
524 | {
525 | New-Object -TypeName PSObject -Property @{
526 | ID = $($property.ID.'#text')
527 | Identifier = $($property.Identifier)
528 | }
529 | }
530 | }
531 |
532 | # Filter out only the GUIDs
533 | $MDTIDs = GetMDTIDs -URL $URL1 |
534 | Select-Object -Property * |
535 | Where-Object -FilterScript {
536 | $_.Identifier -like '*-*'
537 | } |
538 | Sort-Object -Property ID
539 | $MDTComputerIDs = @()
540 | Foreach ($Computer in $UnknownComputers)
541 | {
542 | $MDTComputerID = $MDTIDs |
543 | Where-Object -FilterScript {
544 | $_.Identifier -eq $Computer.GUID
545 | } |
546 | Select-Object -Property ID, Identifier
547 | $MDTComputerIDs += $MDTComputerID
548 | }
549 |
550 | # Get ComputerNames from MDT
551 | function GetMDTComputerNames
552 | {
553 | param ($URL)
554 | $Data = Invoke-RestMethod -Uri $URL
555 | foreach($property in ($Data.content.properties) )
556 | {
557 | New-Object -TypeName PSObject -Property @{
558 | Name = $($property.Name)
559 | ID = $($property.ID.'#text')
560 | }
561 | }
562 | }
563 |
564 | # Filter out the computer names from the IDs
565 | $MDTComputers = GetMDTComputerNames -URL $URL |
566 | Select-Object -Property * |
567 | Sort-Object -Property ID
568 |
569 | $ResolvedComputerNames = @()
570 | Foreach ($MDTComputerID in $MDTComputerIDs)
571 | {
572 | $MDTComputerName = $MDTComputers |
573 | Where-Object -FilterScript {
574 | $_.ID -eq $MDTComputerID.ID
575 | } |
576 | Select-Object -ExpandProperty Name
577 | $GUID = $MDTIDs |
578 | Where-Object -FilterScript {
579 | $_.ID -eq $MDTComputerID.ID
580 | } |
581 | Select-Object -ExpandProperty Identifier
582 | $obj = New-Object -TypeName PSObject
583 | Add-Member -InputObject $obj -MemberType NoteProperty -Name ComputerName -Value $MDTComputerName
584 | Add-Member -InputObject $obj -MemberType NoteProperty -Name GUID -Value $GUID
585 | $ResolvedComputerNames += $obj
586 | }
587 | }
588 | foreach ($Computer in $ResolvedComputerNames)
589 | {
590 | if ($ComputerName -eq $Computer.ComputerName)
591 | {
592 | $MyGUID = $Computer.GUID
593 | }
594 | }
595 | }
596 |
597 |
598 | # Get TS execution data from ConfigMgr
599 | if ($MyGUID)
600 | {
601 | $Query = "
602 | Select Distinct Name0 as 'Computer Name',
603 | sys.SMBIOS_GUID0 as 'GUID',
604 | Name as 'Task Sequence',
605 | ExecutionTime,
606 | Step,
607 | ActionName,
608 | GroupName,
609 | tes.LastStatusMsgName,
610 | ExitCode,
611 | ActionOutput
612 | from vSMS_TaskSequenceExecutionStatus tes
613 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
614 | inner join v_RA_System_MACAddresses mac on tes.ResourceID = mac.ResourceID
615 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
616 | where tsp.Name = '$TS'
617 | and DATEDIFF(hour,ExecutionTime,GETDATE()) < $TimePeriod
618 | --and Name0 like '$SQLComputerName'
619 | and sys.SMBIOS_GUID0 = '$MyGUID'
620 | and ExitCode <> $ExitCode
621 | ORDER BY ExecutionTime Desc
622 | "
623 | $ErrQuery = "
624 | Select Count(Name0) as 'Count'
625 | from vSMS_TaskSequenceExecutionStatus tes
626 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
627 | inner join v_RA_System_MACAddresses mac on tes.ResourceID = mac.ResourceID
628 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
629 | where tsp.Name = '$TS'
630 | and DATEDIFF(hour,ExecutionTime,GETDATE()) < $TimePeriod
631 | --and Name0 like '$SQLComputerName'
632 | and sys.SMBIOS_GUID0 = '$MyGUID'
633 | and ExitCode <> 0
634 | "
635 | }
636 |
637 | if (!$MyGUID)
638 | {
639 | $Query = "
640 | Select Distinct Name0 as 'Computer Name',
641 | sys.SMBIOS_GUID0 as 'GUID',
642 | Name as 'Task Sequence',
643 | ExecutionTime,
644 | Step,
645 | ActionName,
646 | GroupName,
647 | tes.LastStatusMsgName,
648 | ExitCode,
649 | ActionOutput
650 | from vSMS_TaskSequenceExecutionStatus tes
651 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
652 | inner join v_RA_System_MACAddresses mac on tes.ResourceID = mac.ResourceID
653 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
654 | where tsp.Name = '$TS'
655 | and DATEDIFF(hour,ExecutionTime,GETDATE()) < $TimePeriod
656 | and Name0 like '$SQLComputerName'
657 | and ExitCode <> $ExitCode
658 | ORDER BY ExecutionTime Desc
659 | "
660 | $ErrQuery = "
661 | Select Count(Name0) as 'Count'
662 | from vSMS_TaskSequenceExecutionStatus tes
663 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
664 | inner join v_RA_System_MACAddresses mac on tes.ResourceID = mac.ResourceID
665 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
666 | where tsp.Name = '$TS'
667 | and DATEDIFF(hour,ExecutionTime,GETDATE()) < $TimePeriod
668 | and Name0 like '$SQLComputerName'
669 | and ExitCode <> 0
670 | "
671 | }
672 | $command = $connection.CreateCommand()
673 | $command.CommandText = $Query
674 | $result = $command.ExecuteReader()
675 | $table = New-Object -TypeName 'System.Data.DataTable'
676 | $table.Load($result)
677 | $command = $connection.CreateCommand()
678 | $command.CommandText = $ErrQuery
679 | $erresult = $command.ExecuteReader()
680 | $errtable = New-Object -TypeName 'System.Data.DataTable'
681 | $errtable.Load($erresult)
682 | $connection.Close()
683 |
684 | if ($table.rows.Count -lt 1)
685 | {
686 | $hash.Window.Dispatcher.Invoke(
687 | [action]{
688 | $hash.ActionOutput.Text = 'No results.'
689 | })
690 | return
691 | }
692 |
693 | # Gather results into psobject
694 | $global:Results = @()
695 | $i = 0
696 | Foreach ($Row in $table.Rows)
697 | {
698 | $obj = New-Object -TypeName psobject
699 | $i ++
700 | if ($Row.ExitCode -eq 0)
701 | {
702 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Icon' -Value $greentickiconpath
703 | }
704 | if ($Row.ExitCode -ne 0)
705 | {
706 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Icon' -Value $redcrossiconpath
707 | }
708 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ComputerName' -Value $Row.'Computer Name'
709 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'GUID' -Value $Row.'GUID'
710 | if ($DTFormat -eq 'UTC')
711 | {
712 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ExecutionTime' -Value $Row.'ExecutionTime'
713 | }
714 | Else
715 | {
716 | $extime = [System.TimeZone]::CurrentTimeZone.ToLocalTime($($Row.'ExecutionTime' | Get-Date))
717 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ExecutionTime' -Value $extime
718 | }
719 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Step' -Value $Row.'Step'
720 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ActionName' -Value $Row.'ActionName'
721 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'GroupName' -Value $Row.'GroupName'
722 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'LastStatusMsgName' -Value $Row.'LastStatusMsgName'
723 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ExitCode' -Value $Row.'ExitCode'
724 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ActionOutput' -Value $Row.'ActionOutput'
725 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Record' -Value $i
726 | $Results += $obj
727 | }
728 | if ($Results.Count -eq 1)
729 | {
730 | $obj = New-Object -TypeName psobject
731 | $i ++
732 | if ($Row.ExitCode -eq 0)
733 | {
734 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Icon' -Value $greentickiconpath
735 | }
736 | if ($Row.ExitCode -ne 0)
737 | {
738 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Icon' -Value $redcrossiconpath
739 | }
740 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ComputerName' -Value ' '
741 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'GUID' -Value ' '
742 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ExecutionTime' -Value ' '
743 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Step' -Value ' '
744 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ActionName' -Value ' '
745 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'GroupName' -Value ' '
746 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'LastStatusMsgName' -Value ' '
747 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ExitCode' -Value ' '
748 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ActionOutput' -Value ' '
749 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Record' -Value ' '
750 | $Results += $obj
751 | }
752 |
753 | $FilteredResults = $Results | Select-Object -Property Icon, ComputerName, GUID, ExecutionTime, Step, ActionName, GroupName, LastStatusMsgName, ExitCode, Record
754 |
755 | if (!$MyGUID -and $ComputerName -in ('-ALL-', '', $Null))
756 | {
757 | if ($FilteredResults.ComputerName -match 'unknown')
758 | {
759 | foreach ($Computer in $ResolvedComputerNames)
760 | {
761 | $Unknowns = $FilteredResults | Where-Object -FilterScript {
762 | $_.ComputerName -eq 'Unknown' -and $_.GUID -eq $Computer.GUID
763 | }
764 | $i = -1
765 | do
766 | {
767 | $i ++
768 | $Unknowns[$i].ComputerName = $Computer.ComputerName
769 | }
770 | until ($i -eq ($Unknowns.Count -1))
771 | }
772 | }
773 | }
774 |
775 | if ($MyGUID)
776 | {
777 | $Unknowns = $FilteredResults | Where-Object -FilterScript {
778 | $_.ComputerName -eq 'Unknown' -and $_.GUID -eq $MyGUID
779 | }
780 | if ($Unknowns)
781 | {
782 | $i = -1
783 | do
784 | {
785 | $i ++
786 | $Unknowns[$i].ComputerName = $ComputerName
787 | }
788 | until ($i -eq ($Unknowns.Count -1))
789 | }
790 | }
791 |
792 | # Display results in datagrid
793 | $hash.Window.Dispatcher.Invoke(
794 | [action]{
795 | $hash.DataGrid.ItemsSource = $FilteredResults
796 | $hash.ErrorCount.Text = $errtable.Count
797 | $hash.ActionOutput.Text = 'Click any step to see the action output.'
798 | })
799 | }
800 |
801 | # Set variables from Hash table
802 | $SQLServer = $hash.SQLServer.Text
803 | $Database = $hash.Database.Text
804 | $TimePeriod = $hash.TimePeriod.Text
805 | $ErrorsOnly = $hash.ErrorsOnly.IsChecked
806 | $ComputerName = $hash.ComputerName.SelectedItem
807 | $TS = $hash.TaskSequence.SelectedItem
808 | $MDTIntegrated = $hash.MDTIntegrated.IsChecked
809 | $URL = $hash.MDTURL.Text
810 | $DTFormat = $hash.DTFormat.SelectedItem
811 |
812 | # Create PS instance in runspace pool and execute
813 | $PSinstance = [powershell]::Create().AddScript($code).AddArgument($hash).AddArgument($SQLServer).AddArgument($Database).AddArgument($TimePeriod).AddArgument($ErrorsOnly).AddArgument($ComputerName).AddArgument($TS).AddArgument($MDTIntegrated).AddArgument($URL).AddArgument($DTFormat)
814 |
815 | $PSInstances += $PSinstance
816 | $PSinstance.RunspacePool = $RunspacePool
817 | $PSinstance.BeginInvoke()
818 | }
819 |
820 | Function Populate-ActionOutput
821 | {
822 | param ($hash,$RunspacePool)
823 |
824 | $code =
825 | {
826 | param($hash,$Record)
827 | $msg = $Results |
828 | Select-Object -Property * |
829 | Where-Object -FilterScript {
830 | $_.Record -eq $Record
831 | }
832 | $hash.Window.Dispatcher.Invoke(
833 | [action]{
834 | $hash.ActionOutput.Text = $msg.ActionOutput
835 | })
836 | }
837 |
838 | # Set variables from Hash table
839 | $Record = $hash.DataGrid.SelectedItem.Record
840 |
841 | # Create PS instance in runspace pool and execute
842 | $PSinstance = [powershell]::Create().AddScript($code).AddArgument($hash).AddArgument($Record)
843 | $PSInstances += $PSinstance
844 | $PSinstance.RunspacePool = $RunspacePool
845 | $PSinstance.BeginInvoke()
846 | }
847 |
848 | Function Populate-ComputerNames
849 | {
850 | param ($hash,$RunspacePool)
851 |
852 | $code =
853 | {
854 | param($hash,$SQLServer,$Database,$TimePeriod,$ErrorsOnly,$TS,$MDTIntegrated,$URL)
855 |
856 | # Set variable values
857 | if ($ErrorsOnly -eq 'True')
858 | {
859 | $ExitCode = 0
860 | }
861 | else
862 | {
863 | $ExitCode = 999999999999999999999999
864 | }
865 |
866 | # Connect to SQL Server
867 | $connectionString = "Server=$SQLServer;Database=$Database;Integrated Security=SSPI;"
868 | $connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
869 | $connection.ConnectionString = $connectionString
870 | $connection.Open()
871 |
872 | # Run SQL query
873 | $Query = "
874 | Select Distinct Name0,
875 | SMBIOS_GUID0 as 'GUID'
876 | from vSMS_TaskSequenceExecutionStatus tes
877 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
878 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
879 | where tsp.Name = '$TS'
880 | and DATEDIFF(hour,ExecutionTime,GETDATE()) < $TimePeriod
881 | --and Name0 like '$SQLComputerName'
882 | and ExitCode <> $ExitCode
883 | ORDER BY Name0 Desc
884 | "
885 | $command = $connection.CreateCommand()
886 | $command.CommandText = $Query
887 | $result = $command.ExecuteReader()
888 | $table = New-Object -TypeName 'System.Data.DataTable'
889 | $table.Load($result)
890 | $connection.Close()
891 |
892 | # Gather results into PS object
893 | $PCResults = @()
894 | Foreach ($Row in $table.Rows)
895 | {
896 | $obj = New-Object -TypeName psobject
897 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'ComputerName' -Value $Row.Name0
898 | Add-Member -InputObject $obj -MemberType NoteProperty -Name 'GUID' -Value $Row.GUID
899 | $PCResults += $obj
900 | }
901 |
902 | # For each 'Unknown' computer in the list, get the PC name from MDT
903 | if ($MDTIntegrated -eq 'true')
904 | {
905 | $URL1 = $URL.Replace('Computers','ComputerIdentities')
906 |
907 | # Get ID numbers and Identifiers (GUIDs)
908 | function GetMDTIDs
909 | {
910 | param ($URL)
911 | $Data = Invoke-RestMethod -Uri $URL
912 | foreach($property in ($Data.content.properties) )
913 | {
914 | New-Object -TypeName PSObject -Property @{
915 | ID = $($property.ID.'#text')
916 | Identifier = $($property.Identifier)
917 | }
918 | }
919 | }
920 |
921 | # Filter out only the GUIDs
922 | $MDTIDs = GetMDTIDs -URL $URL1 |
923 | Select-Object -Property * |
924 | Where-Object -FilterScript {
925 | $_.Identifier -like '*-*'
926 | } |
927 | Sort-Object -Property ID
928 | $UnknownComputers = $PCResults | Where-Object -FilterScript {
929 | $_.ComputerName -eq 'Unknown'
930 | }
931 | $MDTComputerIDs = @()
932 | Foreach ($Computer in $UnknownComputers)
933 | {
934 | $MDTComputerID = $MDTIDs |
935 | Where-Object -FilterScript {
936 | $_.Identifier -eq $Computer.GUID
937 | } |
938 | Select-Object -Property ID
939 | $MDTComputerIDs += $MDTComputerID
940 | }
941 |
942 | # Get ComputerNames from MDT
943 | function GetMDTComputerNames
944 | {
945 | param ($URL)
946 | $Data = Invoke-RestMethod -Uri $URL
947 | foreach($property in ($Data.content.properties) )
948 | {
949 | New-Object -TypeName PSObject -Property @{
950 | Name = $($property.Name)
951 | ID = $($property.ID.'#text')
952 | }
953 | }
954 | }
955 |
956 | # Filter out the computer names from the IDs
957 | $MDTComputers = GetMDTComputerNames -URL $URL |
958 | Select-Object -Property * |
959 | Sort-Object -Property ID
960 |
961 | $AdditionalComputerNames = @()
962 | Foreach ($MDTComputerID in $MDTComputerIDs)
963 | {
964 | $MDTComputerName = $MDTComputers |
965 | Where-Object -FilterScript {
966 | $_.ID -eq $MDTComputerID.ID
967 | } |
968 | Select-Object -ExpandProperty Name
969 | $AdditionalComputerNames += $MDTComputerName.ToUpper()
970 | }
971 |
972 | $ConfigMgrList = $PCResults |
973 | Select-Object -Property ComputerName |
974 | Where-Object -FilterScript {
975 | $_.ComputerName -ne 'Unknown'
976 | }
977 | $FinalComputerNameList = @()
978 | $FinalComputerNameList += $ConfigMgrList.ComputerName
979 | $FinalComputerNameList += $AdditionalComputerNames
980 | }
981 |
982 | # Add a wildcard option and add only ConfigMgr results if MDT not enabled
983 | if ($MDTIntegrated -eq $false)
984 | {
985 | $FinalComputerNameList = @()
986 | $PCResults = $PCResults | Select-Object -ExpandProperty ComputerName
987 | $FinalComputerNameList += $PCResults
988 | }
989 | $FinalComputerNameList += '-All-'
990 |
991 |
992 | # Display results in ComputerName comboxbox
993 | $hash.Window.Dispatcher.Invoke(
994 | [action]{
995 | $hash.ComputerName.ItemsSource = [Array]$FinalComputerNameList
996 | })
997 | }
998 |
999 | # Set variables from Hash table
1000 | $SQLServer = $hash.SQLServer.Text
1001 | $Database = $hash.Database.Text
1002 | $TimePeriod = $hash.TimePeriod.Text
1003 | $ErrorsOnly = $hash.ErrorsOnly.IsChecked
1004 | $TS = $hash.TaskSequence.SelectedItem
1005 | $MDTIntegrated = $hash.MDTIntegrated.IsChecked
1006 | $URL = $hash.MDTURL.Text
1007 |
1008 | # Create PS instance in runspace pool and execute
1009 | $PSinstance = [powershell]::Create().AddScript($code).AddArgument($hash).AddArgument($SQLServer).AddArgument($Database).AddArgument($TimePeriod).AddArgument($ErrorsOnly).AddArgument($TS).AddArgument($MDTIntegrated).AddArgument($URL)
1010 | $PSInstances += $PSinstance
1011 | $PSinstance.RunspacePool = $RunspacePool
1012 | $PSinstance.BeginInvoke()
1013 | }
1014 |
1015 | Function Enable-MDT
1016 | {
1017 | #Dispose-PSInstances
1018 | $hash.DeploymentStatus.IsEnabled = 'True'
1019 | $hash.CurrentStep.IsEnabled = 'True'
1020 | $hash.StepName.IsEnabled = 'True'
1021 | $hash.PercentComplete.IsEnabled = 'True'
1022 | $hash.MDTStartTime.IsEnabled = 'True'
1023 | $hash.MDTEndTime.IsEnabled = 'True'
1024 | $hash.MDTElapsedTime.IsEnabled = 'True'
1025 |
1026 | $hash.DeploymentStatusLabel.IsEnabled = 'True'
1027 | $hash.CurrentStepLabel.IsEnabled = 'True'
1028 | $hash.StepNameLabel.IsEnabled = 'True'
1029 | $hash.PercentCompleteLabel.IsEnabled = 'True'
1030 | $hash.StartLabel.IsEnabled = 'True'
1031 | $hash.EndLabel.IsEnabled = 'True'
1032 | $hash.ElapsedLabel.IsEnabled = 'True'
1033 | $hash.ProgressLabel.IsEnabled = 'True'
1034 | }
1035 |
1036 | Function Disable-MDT
1037 | {
1038 | #Dispose-PSInstances
1039 | $hash.DeploymentStatus.IsEnabled = $false
1040 | $hash.CurrentStep.IsEnabled = $false
1041 | $hash.StepName.IsEnabled = $false
1042 | $hash.PercentComplete.IsEnabled = $false
1043 | $hash.MDTStartTime.IsEnabled = $false
1044 | $hash.MDTEndTime.IsEnabled = $false
1045 | $hash.MDTElapsedTime.IsEnabled = $false
1046 |
1047 | $hash.DeploymentStatusLabel.IsEnabled = $false
1048 | $hash.CurrentStepLabel.IsEnabled = $false
1049 | $hash.StepNameLabel.IsEnabled = $false
1050 | $hash.PercentCompleteLabel.IsEnabled = $false
1051 | $hash.StartLabel.IsEnabled = $false
1052 | $hash.EndLabel.IsEnabled = $false
1053 | $hash.ElapsedLabel.IsEnabled = $false
1054 | $hash.ProgressLabel.IsEnabled = $false
1055 | }
1056 |
1057 | Function Get-MDTData
1058 | {
1059 | param ($hash,$RunspacePool)
1060 |
1061 | $code =
1062 | {
1063 | param($hash,$URL,$ComputerName,$IsMDTIntegrated,$DTFormat)
1064 |
1065 | $hash.Window.Dispatcher.Invoke(
1066 | [action]{
1067 | $hash.DeploymentStatus.Text = ''
1068 | $hash.CurrentStep.Text = ''
1069 | $hash.StepName.Text = ''
1070 | $hash.PercentComplete.Text = ''
1071 | $hash.MDTStartTime.Text = ''
1072 | $hash.MDTEndTime.Text = ''
1073 | $hash.MDTElapsedTime.Text = ''
1074 | $hash.ProgressBar.Value = 0
1075 | })
1076 |
1077 | if ($IsMDTIntegrated -eq $true -and $ComputerName -ne '-All-' -and $ComputerName -ne '' -and $ComputerName -ne $Null)
1078 | {
1079 | function GetMDTData2
1080 | {
1081 | param ($URL)
1082 | $Data = Invoke-RestMethod -Uri $URL
1083 |
1084 | foreach($property in ($Data.content.properties) )
1085 | {
1086 | New-Object -TypeName PSObject -Property @{
1087 | Name = $($property.Name)
1088 | PercentComplete = $($property.PercentComplete.'#text')
1089 | CurrentStep = $($property.CurrentStep.'#text')
1090 | StepName = $($property.StepName)
1091 | Warnings = $($property.Warnings.'#text')
1092 | Errors = $($property.Errors.'#text')
1093 | DeploymentStatus = $(
1094 | Switch ($property.DeploymentStatus.'#text') {
1095 | 1
1096 | {
1097 | 'Active/Running'
1098 | }
1099 | 2
1100 | {
1101 | 'Failed'
1102 | }
1103 | 3
1104 | {
1105 | 'Successfully completed'
1106 | }
1107 | Default
1108 | {
1109 | 'Unknown'
1110 | }
1111 | }
1112 | )
1113 | StartTime = $($property.StartTime.'#text') -replace 'T', ' '
1114 | EndTime = $($property.EndTime.'#text') -replace 'T', ' '
1115 | }
1116 | }
1117 | }
1118 | try
1119 | {
1120 | $MDT = GetMDTData2 -URL $URL |
1121 | Select-Object -Property Name, DeploymentStatus, PercentComplete, CurrentStep, StepName, Warnings, Errors, StartTime, EndTime |
1122 | Sort-Object -Property Name |
1123 | Where-Object -FilterScript {
1124 | $_.Name -eq $ComputerName
1125 | }
1126 |
1127 | if ($MDT)
1128 | {
1129 | # Calculate times
1130 | $MDTServer = $URL.Split('//')[2].Split(':')[0]
1131 | $Start = $MDT.StartTime | Get-Date
1132 | if ($DTFormat -ne 'UTC')
1133 | {
1134 | $Start = [System.TimeZone]::CurrentTimeZone.ToLocalTime($Start)
1135 | }
1136 | if (!$MDT.EndTime)
1137 | {
1138 | $MDTDate = Invoke-Command -ComputerName $MDTServer -ScriptBlock {
1139 | (Get-Date).ToUniversalTime()
1140 | }
1141 | if ($DTFormat -ne 'UTC')
1142 | {
1143 | $MDTDate = [System.TimeZone]::CurrentTimeZone.ToLocalTime($MDTDate)
1144 | }
1145 | $Elapsed = $MDTDate - $Start
1146 | $Elapsed = "$($Elapsed.Hours)h $($Elapsed.Minutes)m $($Elapsed.Seconds)s"
1147 | $Elapsed
1148 | }
1149 | if ($MDT.EndTime)
1150 | {
1151 | $End = $MDT.EndTime | Get-Date
1152 | if ($DTFormat -ne 'UTC')
1153 | {
1154 | $End = [System.TimeZone]::CurrentTimeZone.ToLocalTime($End)
1155 | }
1156 | $Elapsed = $End - $Start
1157 | $Elapsed = "$($Elapsed.Hours)h $($Elapsed.Minutes)m $($Elapsed.Seconds)s"
1158 | $Elapsed
1159 | }
1160 |
1161 | $hash.Window.Dispatcher.Invoke(
1162 | [action]{
1163 | $hash.DeploymentStatus.Text = $MDT.DeploymentStatus
1164 | $hash.CurrentStep.Text = $MDT.CurrentStep
1165 | $hash.StepName.Text = $MDT.StepName
1166 | $hash.PercentComplete.Text = $MDT.PercentComplete
1167 | $hash.ProgressBar.Value = $MDT.PercentComplete
1168 | $hash.MDTStartTime.Text = $Start
1169 | $hash.MDTEndTime.Text = $End
1170 | $hash.MDTElapsedTime.Text = $Elapsed
1171 | })
1172 | }
1173 | Else
1174 | {
1175 | $hash.Window.Dispatcher.Invoke(
1176 | [action]{
1177 | $hash.DeploymentStatus.Text = 'No data found'
1178 | })
1179 | }
1180 | }
1181 | catch
1182 | {
1183 | $hash.Window.Dispatcher.Invoke(
1184 | [action]{
1185 | $hash.ActionOutput.Text = '[ERROR] Could not connect to MDT Web Service'
1186 | })
1187 | }
1188 | }
1189 | }
1190 |
1191 | # Set variables from Hash table
1192 | $ComputerName = $hash.ComputerName.SelectedItem
1193 | $IsMDTIntegrated = $hash.MDTIntegrated.IsChecked
1194 | $URL = $hash.MDTURL.Text
1195 | $DTFormat = $hash.DTFormat.SelectedItem
1196 |
1197 | # Create PS instance in runspace pool and execute
1198 | $PSinstance = [powershell]::Create().AddScript($code).AddArgument($hash).AddArgument($URL).AddArgument($ComputerName).AddArgument($IsMDTIntegrated).AddArgument($DTFormat)
1199 | $PSInstances += $PSinstance
1200 | $PSinstance.RunspacePool = $RunspacePool
1201 | $PSinstance.BeginInvoke()
1202 | }
1203 |
1204 | function Dispose-PSInstances
1205 | {
1206 | foreach ($PSinstance in $PSInstances)
1207 | {
1208 | if ($PSinstance.InvocationStateInfo.State -eq 'Completed')
1209 | {
1210 | $PSinstance.Dispose()
1211 | }
1212 | }
1213 | }
1214 |
1215 | Function Create-Timer
1216 | {
1217 | $global:Timer = New-Object -TypeName System.Windows.Forms.Timer
1218 | $timer.Interval = [int]$hash.RefreshPeriod.Text * 60000
1219 | }
1220 |
1221 | Function Start-Timer
1222 | {
1223 | if ($timer)
1224 | {
1225 | $timer.Start()
1226 | }
1227 | }
1228 |
1229 | Function Stop-Timer
1230 | {
1231 | if ($timer)
1232 | {
1233 | $timer.Stop()
1234 | }
1235 | }
1236 |
1237 | Function Update-Registry
1238 | {
1239 | param($hash)
1240 | # Test whether running as admin first
1241 | If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator'))
1242 | {
1243 | if (Test-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor')
1244 | {
1245 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name SQLServer -Value $hash.SQLServer.Text
1246 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name Database -Value $hash.Database.Text
1247 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name MDTURL -Value $hash.MDTURL.Text
1248 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name DTFormat -Value $hash.DTFormat.SelectedItem
1249 | }
1250 |
1251 | if (Test-Path -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor')
1252 | {
1253 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name SQLServer -Value $hash.SQLServer.Text
1254 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name Database -Value $hash.Database.Text
1255 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name MDTURL -Value $hash.MDTURL.Text
1256 | Set-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name DTFormat -Value $hash.DTFormat.SelectedItem
1257 | }
1258 | }
1259 | }
1260 |
1261 | Function Read-Registry
1262 | {
1263 | if (Test-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor')
1264 | {
1265 | $regsql = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name SQLServer | Select-Object -ExpandProperty SQLServer
1266 | $regdb = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name Database | Select-Object -ExpandProperty Database
1267 | $regmdt = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name MDTURL | Select-Object -ExpandProperty MDTURL
1268 | $regdtformat = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\SMSAgent\ConfigMgr Task Sequence Monitor' -Name DTFormat | Select-Object -ExpandProperty DTFormat
1269 | }
1270 |
1271 | if (Test-Path -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor')
1272 | {
1273 | $regsql = Get-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name SQLServer | Select-Object -ExpandProperty SQLServer
1274 | $regdb = Get-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name Database | Select-Object -ExpandProperty Database
1275 | $regmdt = Get-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name MDTURL | Select-Object -ExpandProperty MDTURL
1276 | $regdtformat = Get-ItemProperty -Path 'HKLM:\SOFTWARE\SMSAgent\ConfigMgr Task Sequence Monitor' -Name DTFormat | Select-Object -ExpandProperty DTFormat
1277 | }
1278 | if ($regsql -ne $Null -and $regsql -ne '')
1279 | {
1280 | $hash.SQLServer.Text = $regsql
1281 | }
1282 | if ($regdb -ne $Null -and $regdb -ne '')
1283 | {
1284 | $hash.Database.Text = $regdb
1285 | }
1286 | if ($regmdt -ne $Null -and $regmdt -ne '')
1287 | {
1288 | $hash.MDTURL.Text = $regmdt
1289 | }
1290 |
1291 | if ($regdtformat -ne $Null -and $regdtformat -ne '')
1292 | {
1293 | if ($regdtformat -eq 'UTC')
1294 | {
1295 | if (!$CurrentDateTimeF)
1296 | {
1297 | $Global:CurrentDateTimeF = 'UTC'
1298 | }
1299 | }
1300 | }
1301 | }
1302 |
1303 | Function Generate-Report
1304 | {
1305 | param ($hash,$RunspacePool)
1306 |
1307 | $code =
1308 | {
1309 | param($hash,$SQLServer,$Database,$StartDate,$EndDate,$TS,$DTFormat)
1310 | $Results = @()
1311 |
1312 | if ($DTFormat -ne 'UTC')
1313 | {
1314 | [datetime]$StartDate = $StartDate.ToUniversalTime()
1315 | [datetime]$EndDate = $EndDate.ToUniversalTime()
1316 | }
1317 |
1318 | # Set dates to ISO standard format for SQL Server
1319 | $SQLStart = $StartDate | Get-Date -Format s
1320 | $SQLEnd = $EndDate | Get-Date -Format s
1321 |
1322 | $hash.Window.Dispatcher.Invoke(
1323 | [action]{
1324 | $hash.Working.Content = 'Working...'
1325 | $hash.ReportProgress.Visibility = 'Visible'
1326 | $hash.ReportProgress.Value = 10
1327 | })
1328 |
1329 | $connectionString = "Server=$SQLServer;Database=$Database;Integrated Security=SSPI;"
1330 | $connection = New-Object -TypeName System.Data.SqlClient.SqlConnection
1331 | $connection.ConnectionString = $connectionString
1332 | $connection.Open()
1333 |
1334 | # Find all resourceID for TS steps between the selected dates
1335 | $Query = "
1336 | select distinct tes.ResourceID
1337 | from vSMS_TaskSequenceExecutionStatus tes
1338 | --inner join v_R_System sys on tes.ResourceID = sys.ResourceID
1339 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
1340 | where tsp.Name = '$TS'
1341 | and tes.ExecutionTime >= '$SQLStart'
1342 | and tes.ExecutionTime <= '$SQLEnd'
1343 | "
1344 |
1345 | $command = $connection.CreateCommand()
1346 | $command.CommandText = $Query
1347 | $reader = $command.ExecuteReader()
1348 | $table = New-Object -TypeName 'System.Data.DataTable'
1349 | $table.Load($reader)
1350 |
1351 | $hash.Window.Dispatcher.Invoke(
1352 | [action]{
1353 | $hash.ReportProgress.Value = 20
1354 | })
1355 |
1356 | foreach ($ResourceID in $table.Rows.ResourceID)
1357 | {
1358 | $Query = "
1359 | Select (select top(1) convert(datetime,ExecutionTime,121)
1360 | from vSMS_TaskSequenceExecutionStatus tes
1361 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
1362 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
1363 | where tsp.Name = '$TS'
1364 | and tes.ExecutionTime >= '$SQLStart'
1365 | and tes.ExecutionTime <= '$SQLEnd'
1366 | and LastStatusMsgName = 'The task sequence execution engine started execution of a task sequence'
1367 | and Step = 0
1368 | and tes.ResourceID = $ResourceID
1369 | order by ExecutionTime desc) as 'Start',
1370 | (select top(1) convert(datetime,ExecutionTime,121)
1371 | from vSMS_TaskSequenceExecutionStatus tes
1372 | inner join v_R_System sys on tes.ResourceID = sys.ResourceID
1373 | inner join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
1374 | where tsp.Name = '$TS'
1375 | and tes.ExecutionTime >= '$SQLStart'
1376 | and tes.ExecutionTime <= '$SQLEnd'
1377 | and LastStatusMsgName = 'The task sequence execution engine successfully completed a task sequence'
1378 | and tes.ResourceID = $ResourceID
1379 | order by ExecutionTime desc) as 'Finish',
1380 | (Select name0 from v_R_System sys where sys.ResourceID = $ResourceID) as 'ComputerName',
1381 | (select Model0 from v_GS_Computer_System comp where comp.ResourceID = $ResourceID) as 'Model'
1382 | "
1383 | $command = $connection.CreateCommand()
1384 | $command.CommandText = $Query
1385 | $reader = $command.ExecuteReader()
1386 | $table = New-Object -TypeName 'System.Data.DataTable'
1387 | $table.Load($reader)
1388 |
1389 |
1390 | if ($table.rows[0].Start.GetType().Name -eq 'DBNull')
1391 | {
1392 | $Start = ''
1393 | }
1394 | Else
1395 | {
1396 | if ($DTFormat -eq 'UTC')
1397 | {
1398 | $Start = $table.rows[0].Start
1399 | }
1400 | Else
1401 | {
1402 | $Start = [System.TimeZone]::CurrentTimeZone.ToLocalTime($($table.rows[0].Start | Get-Date))
1403 | }
1404 | }
1405 |
1406 | if ($table.rows[0].Finish.GetType().Name -eq 'DBNull')
1407 | {
1408 | $Finish = ''
1409 | }
1410 | Else
1411 | {
1412 | if ($DTFormat -eq 'UTC')
1413 | {
1414 | $Finish = $table.rows[0].Finish
1415 | }
1416 | Else
1417 | {
1418 | $Finish = [System.TimeZone]::CurrentTimeZone.ToLocalTime($($table.rows[0].Finish | Get-Date))
1419 | }
1420 | }
1421 |
1422 |
1423 | #$table
1424 | if ($Start -eq '' -or $Finish -eq '')
1425 | {
1426 | $diff = $Null
1427 | }
1428 | else
1429 | {
1430 | $diff = $Finish-$Start
1431 | }
1432 |
1433 |
1434 | $PC = New-Object -TypeName psobject
1435 | Add-Member -InputObject $PC -MemberType NoteProperty -Name ComputerName -Value $table.rows[0].ComputerName
1436 | Add-Member -InputObject $PC -MemberType NoteProperty -Name StartTime -Value $Start
1437 | Add-Member -InputObject $PC -MemberType NoteProperty -Name FinishTime -Value $Finish
1438 | if ($Start -eq '' -or $Finish -eq '')
1439 | {
1440 | Add-Member -InputObject $PC -MemberType NoteProperty -Name DeploymentTime -Value ''
1441 | }
1442 | else
1443 | {
1444 | Add-Member -InputObject $PC -MemberType NoteProperty -Name DeploymentTime -Value $("$($diff.hours)" + ' hours ' + "$($diff.minutes)" + ' minutes')
1445 | }
1446 | Add-Member -InputObject $PC -MemberType NoteProperty -Name Model -Value $table.rows[0].Model
1447 | $Results += $PC
1448 | }
1449 |
1450 | $Results = $Results | Sort-Object -Property ComputerName
1451 |
1452 | $hash.Window.Dispatcher.Invoke(
1453 | [action]{
1454 | $hash.ReportProgress.Value = 50
1455 | })
1456 |
1457 | $Query = "
1458 | select sys.Name0 as 'ComputerName',
1459 | tsp.Name 'Task Sequence',
1460 | comp.Model0 as Model,
1461 | tes.ExecutionTime,
1462 | tes.Step,
1463 | tes.GroupName,
1464 | tes.ActionName,
1465 | tes.LastStatusMsgName,
1466 | tes.ExitCode,
1467 | tes.ActionOutput
1468 | from vSMS_TaskSequenceExecutionStatus tes
1469 | left join v_R_System sys on tes.ResourceID = sys.ResourceID
1470 | left join v_TaskSequencePackage tsp on tes.PackageID = tsp.PackageID
1471 | left join v_GS_COMPUTER_SYSTEM comp on tes.ResourceID = comp.ResourceID
1472 | where tsp.Name = '$TS'
1473 | and tes.ExecutionTime >= '$SQLStart'
1474 | and tes.ExecutionTime <= '$SQLEnd'
1475 | and tes.ExitCode not in (0,-2147467259)
1476 | Order by tes.ExecutionTime desc
1477 | "
1478 |
1479 | $command = $connection.CreateCommand()
1480 | $command.CommandText = $Query
1481 | $reader = $command.ExecuteReader()
1482 | $table = New-Object -TypeName 'System.Data.DataTable'
1483 | $table.Load($reader)
1484 |
1485 | if ($DTFormat -ne 'UTC')
1486 | {
1487 | $newdates = foreach ($item in $table.rows.ExecutionTime)
1488 | {
1489 | [System.TimeZone]::CurrentTimeZone.ToLocalTime($item)
1490 | }
1491 | $i = -1
1492 | $table.rows.ExecutionTime | ForEach-Object -Process {
1493 | $i ++
1494 | $table.Rows[$i].ExecutionTime = $newdates[$i]
1495 | }
1496 | }
1497 |
1498 | $hash.Window.Dispatcher.Invoke(
1499 | [action]{
1500 | $hash.ReportProgress.Value = 80
1501 | })
1502 |
1503 | #Convert dates if necessary
1504 | if ($DTFormat -ne 'UTC')
1505 | {
1506 | $StartDate = [System.TimeZone]::CurrentTimeZone.ToLocalTime($StartDate)
1507 | $EndDate = [System.TimeZone]::CurrentTimeZone.ToLocalTime($EndDate)
1508 | }
1509 |
1510 | # Create html email
1511 | $style = @"
1512 |
1534 | "@
1535 |
1536 |
1537 | $HEaders = @"
1538 | Task Sequence Execution Summary Report
1539 | Starting Date: $StartDate
1540 | End Date: $EndDate
1541 | Task Sequence: $TS
1542 | TimeZone for Date/Time: $DTFormat
1543 | "@
1544 |
1545 | $body1 = $Results |
1546 | Select-Object -Property ComputerName, StartTime, FinishTime , DeploymentTime, Model |
1547 | ConvertTo-Html -Head $style -Body "Task Sequence Executions ($($Results.Count))
" |
1548 | Out-String
1549 |
1550 | $body2 = $table |
1551 | Select-Object -Property ComputerName, 'Task Sequence', Model, ExecutionTime, Step, GroupName, ActionName, LastStatusMsgName, ExitCode |
1552 | ConvertTo-Html -Head $style -Body "Task Sequence Execution Errors ($($table.Rows.Count))
" |
1553 | Out-String
1554 |
1555 | $Body = $HEaders + $body1 + $body2
1556 |
1557 | $hash.Window.Dispatcher.Invoke(
1558 | [action]{
1559 | $hash.Working.Content = ''
1560 | $hash.ReportProgress.Value = 100
1561 | })
1562 |
1563 | $Body | Out-File -FilePath $env:temp\TSReport.htm -Force
1564 | Invoke-Item -Path $env:temp\TSReport.htm
1565 |
1566 |
1567 | # Close the connection
1568 | $connection.Close()
1569 | }
1570 |
1571 | # Set variables from Hash table
1572 | $SQLServer = $hash.SQLServer.Text
1573 | $Database = $hash.Database.Text
1574 | $DTFormat = $hash.DTFormat.SelectedItem
1575 | #if ($DTFormat -eq "UTC")
1576 | # {
1577 | [datetime]$StartDate = $hash.StartDate.Text | Get-Date -Format "MM'/'dd'/'yyyy HH':'mm':'ss"
1578 | [datetime]$EndDate = $hash.EndDate.Text | Get-Date -Format "MM'/'dd'/'yyyy HH':'mm':'ss"
1579 | # }
1580 | #Else {
1581 | # [datetime]$StartDate = [System.TimeZone]::CurrentTimeZone.ToLocalTime($($hash.StartDate.Text | Get-Date -Format "MM'/'dd'/'yyyy HH':'mm':'ss"))
1582 | # [datetime]$EndDate = [System.TimeZone]::CurrentTimeZone.ToLocalTime($($hash.EndDate.Text | Get-Date -Format "MM'/'dd'/'yyyy HH':'mm':'ss"))
1583 | # }
1584 |
1585 | $EndDate = $EndDate.AddDays(1).AddSeconds(-1)
1586 | $TS = $hash.TSList.SelectedItem
1587 |
1588 | # Create PS instance in runspace pool and execute
1589 | $PSinstance = [powershell]::Create().AddScript($code).AddArgument($hash).AddArgument($SQLServer).AddArgument($Database).AddArgument($StartDate).AddArgument($EndDate).AddArgument($TS).AddArgument($DTFormat)
1590 | $PSInstances += $PSinstance
1591 | $PSinstance.RunspacePool = $RunspacePool
1592 | $PSinstance.BeginInvoke()
1593 | }
1594 |
1595 | Function Clear-MDT
1596 | {
1597 | param ($hash)
1598 |
1599 | $hash.Window.Dispatcher.Invoke(
1600 | [action]{
1601 | $hash.DeploymentStatus.Text = ''
1602 | $hash.CurrentStep.Text = ''
1603 | $hash.StepName.Text = ''
1604 | $hash.PercentComplete.Text = ''
1605 | $hash.ProgressBar.Value = 0
1606 | $hash.MDTStartTime.Text = ''
1607 | $hash.MDTEndTime.Text = ''
1608 | $hash.MDTElapsedTime.Text = ''
1609 | })
1610 | }
1611 |
1612 | #endregion
1613 |
1614 | #region Event Handlers
1615 |
1616 | $hash.Window.Add_ContentRendered({
1617 | #Disable-MDT
1618 | Read-Registry
1619 | Get-DateTimeFormat
1620 | Get-TaskSequenceList
1621 | })
1622 |
1623 | $hash.TaskSequence.Add_SelectionChanged({
1624 | $Count = $hash.ComputerName.Items.Count
1625 | $hash.Window.Dispatcher.Invoke(
1626 | [action]{
1627 | $hash.ComputerName.SelectedIndex = ($Count -1)
1628 | })
1629 | Dispose-PSInstances
1630 | Clear-MDT -hash $hash
1631 | Populate-ComputerNames -hash $hash -RunspacePool $RunspacePool
1632 | Get-TaskSequenceData -hash $hash -RunspacePool $RunspacePool
1633 | Get-MDTData -hash $hash -RunspacePool $RunspacePool
1634 | Stop-Timer
1635 | Create-Timer
1636 | $timer.add_Tick({
1637 | Populate-ComputerNames -hash $hash -RunspacePool $RunspacePool
1638 | Get-TaskSequenceData -hash $hash -RunspacePool $RunspacePool
1639 | Get-MDTData -hash $hash -RunspacePool $RunspacePool
1640 | })
1641 | Start-Timer
1642 | $Global:CurrentTS = $hash.TaskSequence.SelectedItem
1643 | })
1644 |
1645 | $hash.ErrorsOnly.Add_Checked({
1646 | Dispose-PSInstances
1647 | Stop-Timer
1648 | Get-TaskSequenceData -hash $hash -RunspacePool $RunspacePool
1649 | Start-Timer
1650 | })
1651 |
1652 | $hash.MDTIntegrated.Add_Checked({
1653 | Populate-ComputerNames -hash $hash -RunspacePool $RunspacePool
1654 | Enable-MDT
1655 | })
1656 |
1657 | $hash.MDTIntegrated.Add_Unchecked({
1658 | Populate-ComputerNames -hash $hash -RunspacePool $RunspacePool
1659 | Disable-MDT
1660 | })
1661 |
1662 | $hash.ErrorsOnly.Add_Unchecked({
1663 | Dispose-PSInstances
1664 | Stop-Timer
1665 | Get-TaskSequenceData -hash $hash -RunspacePool $RunspacePool
1666 | Start-Timer
1667 | })
1668 |
1669 | $hash.DataGrid.Add_SelectionChanged({
1670 | Dispose-PSInstances
1671 | Populate-ActionOutput -hash $hash -RunspacePool $RunspacePool
1672 | })
1673 |
1674 | $hash.RefreshNow.Add_Click({
1675 | Dispose-PSInstances
1676 | Stop-Timer
1677 | Get-TaskSequenceData -hash $hash -RunspacePool $RunspacePool
1678 | Populate-ComputerNames -hash $hash -RunspacePool $RunspacePool
1679 | Get-MDTData -hash $hash -RunspacePool $RunspacePool
1680 | $timer.Interval = [int]$hash.RefreshPeriod.Text * 60000
1681 | Start-Timer
1682 | })
1683 |
1684 | $hash.ComputerName.Add_SelectionChanged({
1685 | if ($hash.TaskSequence.SelectedItem -eq $CurrentTS)
1686 | {
1687 | Dispose-PSInstances
1688 | Stop-Timer
1689 | Get-TaskSequenceData -hash $hash -RunspacePool $RunspacePool
1690 | Get-MDTData -hash $hash -RunspacePool $RunspacePool
1691 | Start-Timer
1692 | }
1693 | })
1694 |
1695 | $hash.TimePeriod.Add_KeyDown({
1696 | if ($_.Key -eq 'Return')
1697 | {
1698 | Dispose-PSInstances
1699 | Stop-Timer
1700 | Get-TaskSequenceData -hash $hash -RunspacePool $RunspacePool
1701 | Populate-ComputerNames -hash $hash -RunspacePool $RunspacePool
1702 | Get-MDTData -hash $hash -RunspacePool $RunspacePool
1703 | $timer.Interval = [int]$hash.RefreshPeriod.Text * 60000
1704 | Start-Timer
1705 | }
1706 | })
1707 |
1708 | $hash.RefreshPeriod.Add_TextChanged({
1709 | Stop-Timer
1710 | $timer.Interval = [int]$hash.RefreshPeriod.Text * 60000
1711 | Start-Timer
1712 | })
1713 |
1714 | $hash.SettingsButton.Add_Click({
1715 | $reader = (New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml2)
1716 | $hash.Window2 = [Windows.Markup.XamlReader]::Load( $reader )
1717 | $hash.SQLServer = $hash.Window2.FindName('SQLServer')
1718 | $hash.Database = $hash.Window2.FindName('Database')
1719 | $hash.MDTURL = $hash.Window2.FindName('MDTURL')
1720 | $hash.ConnectSQL = $hash.Window2.FindName('ConnectSQL')
1721 | $hash.TSList = $hash.Window2.FindName('TSList')
1722 | $hash.StartDate = $hash.Window2.FindName('StartDate')
1723 | $hash.EndDate = $hash.Window2.FindName('EndDate')
1724 | $hash.GenerateReport = $hash.Window2.FindName('GenerateReport')
1725 | $hash.SettingsTab = $hash.Window2.FindName('SettingsTab')
1726 | $hash.ReportTab = $hash.Window2.FindName('ReportTab')
1727 | $hash.Tabs = $hash.Window2.FindName('Tabs')
1728 | $hash.Runasadmin = $hash.Window2.FindName('Runasadmin')
1729 | $hash.Working = $hash.Window2.FindName('Working')
1730 | $hash.ReportProgress = $hash.Window2.FindName('ReportProgress')
1731 | $hash.Link1 = $hash.Window2.FindName('Link1')
1732 | $hash.DTFormat = $hash.Window2.FindName('DTFormat')
1733 | if (Test-Path -Path "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
1734 | {
1735 | #$hash.Window2.Icon = "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
1736 | }
1737 | if (Test-Path -Path "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
1738 | {
1739 | #$hash.Window2.Icon = "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
1740 | }
1741 |
1742 | Read-Registry
1743 | $hash.SettingsTab.Focus()
1744 | If (!(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')))
1745 | {
1746 | $hash.Runasadmin.Visibility = 'Visible'
1747 | }
1748 | $hash.TSList.ItemsSource = $Views.TS
1749 | $hash.DTFormat.ItemsSource = $Timezones.TimeZone
1750 | If ($CurrentDateTimeF -eq 'UTC')
1751 | {
1752 | $hash.DTFormat.SelectedIndex = 0
1753 | }
1754 | Else
1755 | {
1756 | $hash.DTFormat.SelectedIndex = 1
1757 | }
1758 |
1759 | $hash.SQLServer.Add_GotMouseCapture({
1760 | if ($hash.SQLServer.Text -eq '')
1761 | {
1762 | $hash.SQLServer.Text = ''
1763 | }
1764 | })
1765 |
1766 | $hash.SQLServer.Add_GotKeyboardFocus({
1767 | if ($hash.SQLServer.Text -eq '')
1768 | {
1769 | $hash.SQLServer.Text = ''
1770 | }
1771 | })
1772 |
1773 | $hash.Database.Add_GotMouseCapture({
1774 | if ($hash.Database.Text -eq '')
1775 | {
1776 | $hash.Database.Text = ''
1777 | }
1778 | })
1779 |
1780 | $hash.Database.Add_GotKeyboardFocus({
1781 | if ($hash.Database.Text -eq '')
1782 | {
1783 | $hash.Database.Text = ''
1784 | }
1785 | })
1786 | $hash.ConnectSQL.Add_Click({
1787 | Update-Registry -hash $hash
1788 | Get-TaskSequenceList
1789 | })
1790 |
1791 | $hash.GenerateReport.Add_Click({
1792 | Generate-Report -hash $hash -RunspacePool $RunspacePool
1793 | })
1794 |
1795 | $hash.Link1.Add_Click({
1796 | Start-Process -FilePath 'http://smsagent.wordpress.com/tools/configmgr-task-sequence-monitor/'
1797 | })
1798 |
1799 | $hash.DTFormat.Add_SelectionChanged({
1800 | $Global:CurrentDateTimeF = $hash.DTFormat.SelectedItem
1801 | })
1802 |
1803 | $hash.Window2.Add_Closed({
1804 | Update-Registry -hash $hash
1805 | })
1806 |
1807 | $Null = $hash.Window2.ShowDialog()
1808 | })
1809 |
1810 | $hash.ReportButton.Add_Click({
1811 | $reader = (New-Object -TypeName System.Xml.XmlNodeReader -ArgumentList $xaml2)
1812 | $hash.Window2 = [Windows.Markup.XamlReader]::Load( $reader )
1813 | $hash.SQLServer = $hash.Window2.FindName('SQLServer')
1814 | $hash.Database = $hash.Window2.FindName('Database')
1815 | $hash.MDTURL = $hash.Window2.FindName('MDTURL')
1816 | $hash.ConnectSQL = $hash.Window2.FindName('ConnectSQL')
1817 | $hash.TSList = $hash.Window2.FindName('TSList')
1818 | $hash.StartDate = $hash.Window2.FindName('StartDate')
1819 | $hash.EndDate = $hash.Window2.FindName('EndDate')
1820 | $hash.GenerateReport = $hash.Window2.FindName('GenerateReport')
1821 | $hash.SettingsTab = $hash.Window2.FindName('SettingsTab')
1822 | $hash.ReportTab = $hash.Window2.FindName('ReportTab')
1823 | $hash.Tabs = $hash.Window2.FindName('Tabs')
1824 | $hash.Runasadmin = $hash.Window2.FindName('Runasadmin')
1825 | $hash.Working = $hash.Window2.FindName('Working')
1826 | $hash.ReportProgress = $hash.Window2.FindName('ReportProgress')
1827 | $hash.Link1 = $hash.Window2.FindName('Link1')
1828 | $hash.DTFormat = $hash.Window2.FindName('DTFormat')
1829 | if (Test-Path -Path "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
1830 | {
1831 | #$hash.Window2.Icon = "$env:ProgramFiles\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
1832 | }
1833 | if (Test-Path -Path "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico")
1834 | {
1835 | #$hash.Window2.Icon = "${env:ProgramFiles(x86)}\SMSAgent\ConfigMgr Task Sequence Monitor\Grid.ico"
1836 | }
1837 |
1838 | Read-Registry
1839 | $hash.ReportTab.Focus()
1840 | If (!(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')))
1841 | {
1842 | $hash.Runasadmin.Visibility = 'Visible'
1843 | }
1844 | $hash.TSList.ItemsSource = $Views.TS
1845 | $hash.DTFormat.ItemsSource = $Timezones.TimeZone
1846 | If ($CurrentDateTimeF -eq 'UTC')
1847 | {
1848 | $hash.DTFormat.SelectedIndex = 0
1849 | }
1850 | Else
1851 | {
1852 | $hash.DTFormat.SelectedIndex = 1
1853 | }
1854 |
1855 | $hash.SQLServer.Add_GotMouseCapture({
1856 | if ($hash.SQLServer.Text -eq '')
1857 | {
1858 | $hash.SQLServer.Text = ''
1859 | }
1860 | })
1861 |
1862 | $hash.SQLServer.Add_GotKeyboardFocus({
1863 | if ($hash.SQLServer.Text -eq '')
1864 | {
1865 | $hash.SQLServer.Text = ''
1866 | }
1867 | })
1868 |
1869 | $hash.Database.Add_GotMouseCapture({
1870 | if ($hash.Database.Text -eq '')
1871 | {
1872 | $hash.Database.Text = ''
1873 | }
1874 | })
1875 |
1876 | $hash.Database.Add_GotKeyboardFocus({
1877 | if ($hash.Database.Text -eq '')
1878 | {
1879 | $hash.Database.Text = ''
1880 | }
1881 | })
1882 |
1883 | $hash.ConnectSQL.Add_Click({
1884 | Update-Registry -hash $hash
1885 | Get-TaskSequenceList
1886 | })
1887 |
1888 | $hash.GenerateReport.Add_Click({
1889 | Generate-Report -hash $hash -RunspacePool $RunspacePool
1890 | })
1891 |
1892 | $hash.Link1.Add_Click({
1893 | Start-Process -FilePath 'http://smsagent.wordpress.com/tools/configmgr-task-sequence-monitor/'
1894 | })
1895 |
1896 | $hash.DTFormat.Add_SelectionChanged({
1897 | $Global:CurrentDateTimeF = $hash.DTFormat.SelectedItem
1898 | })
1899 |
1900 | $hash.Window2.Add_Closed({
1901 | Update-Registry -hash $hash
1902 | })
1903 |
1904 | $Null = $hash.Window2.ShowDialog()
1905 | })
1906 |
1907 | $hash.Window.Add_Closed({
1908 | Stop-Timer
1909 | Dispose-PSInstances
1910 | $RunspacePool.close()
1911 | $RunspacePool.Dispose()
1912 | })
1913 |
1914 | # Stop process on closing, #comment our for development
1915 | $hash.window.Add_Closing({[System.Windows.Forms.Application]::Exit(); Stop-Process $pid})
1916 | #endregion
1917 |
1918 |
1919 | # Make PowerShell Disappear #comment our for development
1920 | $windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
1921 | $asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
1922 | $null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0)
1923 |
1924 | #$app = [Windows.Application]::new()
1925 | $app = New-Object Windows.Application
1926 | $app.Run($Hash.Window)
1927 |
1928 |
--------------------------------------------------------------------------------