· 10 years ago · Jun 20, 2016, 09:30 AM
1#requires -Version 3.0
2#requires -Module DHCPServer
3#This File is in Unicode format. Do not edit in an ASCII editor.
4
5<#
6.SYNOPSIS
7 Creates a complete inventory of a Microsoft 2012+ DHCP server using Microsoft Word 2010, 2013, 2016 or formatted text.
8.DESCRIPTION
9 Creates a complete inventory of a Microsoft 2012+ DHCP server using Microsoft Word and PowerShell or just formatted text.
10 Creates a Text file, Word document or PDF named after the DHCP server.
11
12 Requires PowerShell V3.0 or later.
13
14 Requires the DHCPServer module.
15 Can be run on a DHCP server or on a Windows 8.x or Windows 10 computer with RSAT installed.
16
17 Remote Server Administration Tools for Windows 8
18 http://www.microsoft.com/en-us/download/details.aspx?id=28972
19
20 Remote Server Administration Tools for Windows 8.1
21 http://www.microsoft.com/en-us/download/details.aspx?id=39296
22
23 Remote Server Administration Tools for Windows 10
24 http://www.microsoft.com/en-us/download/details.aspx?id=45520
25
26 For Windows Server 2003, 2008 and 2008 R2, use the following to export and import the DHCP data:
27 Export from the 2003, 2008 or 2008 R2 server:
28 netsh dhcp server export C:\DHCPExport.txt all
29
30 Copy the C:\DHCPExport.txt file to the 2012+ server.
31
32 Import on the 2012+ server:
33 netsh dhcp server import c:\DHCPExport.txt all
34
35 The script can now be run on the 2012+ DHCP server to document the older DHCP information.
36
37 For Windows Server 2008 and Server 2008 R2, the 2012+ DHCP Server PowerShell cmdlets can be used for the export and import.
38 From the 2012+ DHCP server:
39 Export-DhcpServer -ComputerName 2008R2Server.domain.tld -Leases -File C:\DHCPExport.xml
40
41 Import-DhcpServer -ComputerName 2012Server.domain.tld -Leases -File C:\DHCPExport.xml -BackupPath C:\dhcp\backup\
42
43 Note: The c:\dhcp\backup path must exist before the Import-DhcpServer cmdlet is run.
44
45 Using netsh is much faster than using the PowerShell export and import cmdlets.
46
47 Processing of IPv4 Multicast Scopes is only available with Server 2012 R2 DHCP.
48
49 Word and PDF Documents include a Cover Page, Table of Contents and Footer.
50
51 Includes support for the following language versions of Microsoft Word:
52 Catalan
53 Danish
54 Dutch
55 English
56 Finnish
57 French
58 German
59 Norwegian
60 Portuguese
61 Spanish
62 Swedish
63
64.PARAMETER CompanyName
65 Company Name to use for the Cover Page.
66 Default value is contained in HKCU:\Software\Microsoft\Office\Common\UserInfo\CompanyName or
67 HKCU:\Software\Microsoft\Office\Common\UserInfo\Company, whichever is populated on the
68 computer running the script.
69 This parameter has an alias of CN.
70.PARAMETER CoverPage
71 What Microsoft Word Cover Page to use.
72 Only Word 2010, 2013 and 2016 are supported.
73 (default cover pages in Word en-US)
74
75 Valid input is:
76 Alphabet (Word 2010. Works)
77 Annual (Word 2010. Doesn't work well for this report)
78 Austere (Word 2010. Works)
79 Austin (Word 2010/2013/2016. Doesn't work in 2013 or 2016, mostly works in 2010 but
80 Subtitle/Subject & Author fields need to be moved
81 after title box is moved up)
82 Banded (Word 2013/2016. Works)
83 Conservative (Word 2010. Works)
84 Contrast (Word 2010. Works)
85 Cubicles (Word 2010. Works)
86 Exposure (Word 2010. Works if you like looking sideways)
87 Facet (Word 2013/2016. Works)
88 Filigree (Word 2013/2016. Works)
89 Grid (Word 2010/2013/2016. Works in 2010)
90 Integral (Word 2013/2016. Works)
91 Ion (Dark) (Word 2013/2016. Top date doesn't fit; box needs to be manually resized or font
92 changed to 8 point)
93 Ion (Light) (Word 2013/2016. Top date doesn't fit; box needs to be manually resized or font
94 changed to 8 point)
95 Mod (Word 2010. Works)
96 Motion (Word 2010/2013/2016. Works if top date is manually changed to 36 point)
97 Newsprint (Word 2010. Works but date is not populated)
98 Perspective (Word 2010. Works)
99 Pinstripes (Word 2010. Works)
100 Puzzle (Word 2010. Top date doesn't fit; box needs to be manually resized or font
101 changed to 14 point)
102 Retrospect (Word 2013/2016. Works)
103 Semaphore (Word 2013/2016. Works)
104 Sideline (Word 2010/2013/2016. Doesn't work in 2013 or 2016, works in 2010)
105 Slice (Dark) (Word 2013/2016. Doesn't work)
106 Slice (Light) (Word 2013/2016. Doesn't work)
107 Stacks (Word 2010. Works)
108 Tiles (Word 2010. Date doesn't fit unless changed to 26 point)
109 Transcend (Word 2010. Works)
110 ViewMaster (Word 2013/2016. Works)
111 Whisp (Word 2013/2016. Works)
112
113 Default value is Sideline.
114 This parameter has an alias of CP.
115 This parameter is only valid with the MSWORD and PDF output parameters.
116.PARAMETER UserName
117 User name to use for the Cover Page and Footer.
118 Default value is contained in $env:username
119 This parameter has an alias of UN.
120.PARAMETER PDF
121 SaveAs PDF file instead of DOCX file.
122 This parameter is disabled by default.
123 The PDF file is roughly 5X to 10X larger than the DOCX file.
124.PARAMETER Text
125 Creates a formatted text file with a .txt extension.
126 This parameter is disabled by default.
127.PARAMETER MSWord
128 SaveAs DOCX file
129 This parameter is set True if no other output format is selected.
130#.PARAMETER HTML
131# Creates an HTML file with an .html extension.
132# This parameter is disabled by default.
133# This parameter is reserved for a future update and no output is created at this time.
134.PARAMETER ComputerName
135 DHCP server to run the script against.
136 The computername is used for the report title.
137 ComputerName can be entered as the NetBIOS name, FQDN, localhost or IP Address.
138 If entered as localhost, the actual computer name is determined and used.
139 If entered as an IP address, an attempt is made to determine and use the actual computer name.
140 This parameter is required.
141.PARAMETER IncludeLeases
142 Include DHCP lease information.
143 Default is to not included lease information.
144.PARAMETER AddDateTime
145 Adds a date time stamp to the end of the file name.
146 Time stamp is in the format of yyyy-MM-dd_HHmm.
147 June 1, 2016 at 6PM is 2016-06-01_1800.
148 Output filename will be ReportName_2016-06-01_1800.docx (or .pdf or .txt).
149 This parameter is disabled by default.
150.PARAMETER Folder
151 Specifies the optional output folder to save the output report.
152.PARAMETER SmtpServer
153 Specifies the optional email server to send the output report.
154.PARAMETER SmtpPort
155 Specifies the SMTP port.
156 Default is 25.
157.PARAMETER UseSSL
158 Specifies whether to use SSL for the SmtpServer.
159 Default is False.
160.PARAMETER From
161 Specifies the username for the From email address.
162 If SmtpServer is used, this is a required parameter.
163.PARAMETER To
164 Specifies the username for the To email address.
165 If SmtpServer is used, this is a required parameter.
166.PARAMETER Dev
167 Clears errors at the beginning of the script.
168 Outputs all errors to a text file at the end of the script.
169
170 This is used when the script developer requests more troubleshooting data.
171 Text file is placed in the same folder from where the script is run.
172
173 This parameter is disabled by default.
174.PARAMETER ScriptInfo
175 Outputs information about the script to a text file.
176 Text file is placed in the same folder from where the script is run.
177
178 This parameter is disabled by default.
179 This parameter has an alias of SI.
180.EXAMPLE
181 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -ComputerName DHCPServer01
182
183 Will use all Default values.
184 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
185 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
186 $env:username = Administrator
187
188 Carl Webster for the Company Name.
189 Sideline for the Cover Page format.
190 Administrator for the User Name.
191
192 Script will be run remotely against DHCP server DHCPServer01.
193.EXAMPLE
194 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -ComputerName localhost
195
196 Will use all Default values.
197 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
198 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
199 $env:username = Administrator
200
201 Carl Webster for the Company Name.
202 Sideline for the Cover Page format.
203 Administrator for the User Name.
204
205 Script will resolve localhost to $env:computername, for example DHCPServer01.
206 Script will be run remotely against DHCP server DHCPServer01 and not localhost.
207 Output file name will use the server name DHCPServer01 and not localhost.
208.EXAMPLE
209 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -ComputerName 192.168.1.222
210
211 Will use all Default values.
212 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
213 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
214 $env:username = Administrator
215
216 Carl Webster for the Company Name.
217 Sideline for the Cover Page format.
218 Administrator for the User Name.
219
220 Script will resolve 192.168.1.222 to the DNS hostname, for example DHCPServer01.
221 Script will be run remotely against DHCP server DHCPServer01 and not 192.18.1.222.
222 Output file name will use the server name DHCPServer01 and not 192.168.1.222.
223.EXAMPLE
224 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -PDF -ComputerName DHCPServer02
225
226 Will use all Default values and save the document as a PDF file.
227 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
228 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
229 $env:username = Administrator
230
231 Carl Webster for the Company Name.
232 Sideline for the Cover Page format.
233 Administrator for the User Name.
234
235 Script will be run remotely against DHCP server DHCPServer02.
236.EXAMPLE
237 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -Text -ComputerName DHCPServer02
238
239 Will use all Default values and save the document as a formatted text file.
240 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
241 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
242 $env:username = Administrator
243
244 Carl Webster for the Company Name.
245 Sideline for the Cover Page format.
246 Administrator for the User Name.
247
248 Script will be run remotely against DHCP server DHCPServer02.
249#.EXAMPLE
250# PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -HTML -ComputerName DHCPServer02
251#
252# This parameter is reserved for a future update and no output is created at this time.
253#
254# Will use all Default values and save the document as an HTML file.
255# HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
256# HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
257# $env:username = Administrator
258#
259# Carl Webster for the Company Name.
260# Sideline for the Cover Page format.
261# Administrator for the User Name.
262#
263# Script will be run remotely against DHCP server DHCPServer02.
264.EXAMPLE
265 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -MSWord -ComputerName DHCPServer02
266
267 Will use all Default values and save the document as a Word DOCX file.
268 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
269 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
270 $env:username = Administrator
271
272 Carl Webster for the Company Name.
273 Sideline for the Cover Page format.
274 Administrator for the User Name.
275
276 Script will be run remotely against DHCP server DHCPServer02.
277#.EXAMPLE
278# PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -HTML -ComputerName DHCPServer02
279#
280# Will use all Default values and save the output as an HTML file.
281# HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
282# HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
283# $env:username = Administrator
284#
285# Carl Webster for the Company Name.
286# Sideline for the Cover Page format.
287# Administrator for the User Name.
288#
289# Script will be run remotely against DHCP server DHCPServer02.
290.EXAMPLE
291 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -ComputerName DHCPServer03 -IncludeLeases
292
293 Will use all Default values and add additional information for each server about its hardware.
294 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
295 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
296 $env:username = Administrator
297
298 Carl Webster for the Company Name.
299 Sideline for the Cover Page format.
300 Administrator for the User Name.
301
302 Script will be run remotely against DHCP server DHCPServer03.
303 Output will contain DHCP lease information.
304.EXAMPLE
305 PS C:\PSScript .\DHCP_Inventory_V1_3.ps1 -CompanyName "Carl Webster Consulting" -CoverPage "Mod" -UserName "Carl Webster" -ComputerName DHCPServer01
306
307 Will use:
308 Carl Webster Consulting for the Company Name.
309 Mod for the Cover Page format.
310 Carl Webster for the User Name.
311
312 Script will be run remotely against DHCP server DHCPServer01.
313.EXAMPLE
314 PS C:\PSScript .\DHCP_Inventory_V1_3.ps1 -CN "Carl Webster Consulting" -CP "Mod" -UN "Carl Webster" -ComputerName DHCPServer02 -IncludeLeases
315
316 Will use:
317 Carl Webster Consulting for the Company Name (alias CN).
318 Mod for the Cover Page format (alias CP).
319 Carl Webster for the User Name (alias UN).
320
321 Script will be run remotely against DHCP server DHCPServer02.
322 Output will contain DHCP lease information.
323.EXAMPLE
324 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -Folder \\FileServer\ShareName
325
326 Will use all default values.
327 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
328 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
329 $env:username = Administrator
330
331 Carl Webster for the Company Name.
332 Sideline for the Cover Page format.
333 Administrator for the User Name.
334
335 Output file will be saved in the path \\FileServer\ShareName
336.EXAMPLE
337 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -ComputerName DHCPServer01 -SmtpServer mail.domain.tld -From XDAdmin@domain.tld -To ITGroup@domain.tld -ComputerName DHCPServer01
338
339 Will use all Default values.
340 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
341 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
342 $env:username = Administrator
343
344 Carl Webster for the Company Name.
345 Sideline for the Cover Page format.
346 Administrator for the User Name.
347
348 Script will be run remotely against DHCP server DHCPServer01.
349
350 Script will use the email server mail.domain.tld, sending from XDAdmin@domain.tld, sending to ITGroup@domain.tld.
351 If the current user's credentials are not valid to send email, the user will be prompted to enter valid credentials.
352.EXAMPLE
353 PS C:\PSScript > .\DHCP_Inventory_V1_3.ps1 -ComputerName DHCPServer01 -SmtpServer smtp.office365.com -SmtpPort 587 -UseSSL -From Webster@CarlWebster.com -To ITGroup@CarlWebster.com
354
355 Will use all Default values.
356 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\CompanyName="Carl Webster" or
357 HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\Company="Carl Webster"
358 $env:username = Administrator
359
360 Carl Webster for the Company Name.
361 Sideline for the Cover Page format.
362 Administrator for the User Name.
363
364 Script will be run remotely against DHCP server DHCPServer01.
365
366 Script will use the email server smtp.office365.com on port 587 using SSL, sending from webster@carlwebster.com, sending to ITGroup@carlwebster.com.
367 If the current user's credentials are not valid to send email, the user will be prompted to enter valid credentials.
368.INPUTS
369 None. You cannot pipe objects to this script.
370.OUTPUTS
371 No objects are output from this script. This script creates a Word, PDF or formatted text document.
372.NOTES
373 NAME: DHCP_Inventory_V1_3.ps1
374 VERSION: 1.30
375 AUTHOR: Carl Webster (with a lot of help from Michael B. Smith)
376 LASTEDIT: May 3, 2016
377#>
378
379
380#thanks to @jeffwouters and Michael B. Smith for helping me with these parameters
381[CmdletBinding(SupportsShouldProcess = $False, ConfirmImpact = "None", DefaultParameterSetName = "Word") ]
382
383Param(
384 [parameter(ParameterSetName="Word",Mandatory=$False)]
385 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
386 [Switch]$MSWord=$False,
387
388 [parameter(ParameterSetName="PDF",Mandatory=$False)]
389 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
390 [Switch]$PDF=$False,
391
392 [parameter(ParameterSetName="Text",Mandatory=$False)]
393 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
394 [Switch]$Text=$False,
395
396# [parameter(ParameterSetName="HTML",Mandatory=$False)]
397# [parameter(ParameterSetName="SMTP",Mandatory=$False)]
398# [Switch]$HTML=$False,
399
400 [parameter(Mandatory=$False)]
401 [string]$ComputerName="LocalHost",
402
403 [parameter(Mandatory=$False)]
404 [Switch]$IncludeLeases=$False,
405
406 [parameter(Mandatory=$False)]
407 [Switch]$AddDateTime=$False,
408
409 [parameter(Mandatory=$False)]
410 [string]$Folder="",
411
412 [parameter(ParameterSetName="Word",Mandatory=$False)]
413 [parameter(ParameterSetName="PDF",Mandatory=$False)]
414 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
415 [Alias("CN")]
416 [ValidateNotNullOrEmpty()]
417 [string]$CompanyName="",
418
419 [parameter(ParameterSetName="Word",Mandatory=$False)]
420 [parameter(ParameterSetName="PDF",Mandatory=$False)]
421 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
422 [Alias("CP")]
423 [ValidateNotNullOrEmpty()]
424 [string]$CoverPage="Sideline",
425
426 [parameter(ParameterSetName="Word",Mandatory=$False)]
427 [parameter(ParameterSetName="PDF",Mandatory=$False)]
428 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
429 [Alias("UN")]
430 [ValidateNotNullOrEmpty()]
431 [string]$UserName=$env:username,
432
433 [parameter(ParameterSetName="SMTP",Mandatory=$True)]
434 [string]$SmtpServer="",
435
436 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
437 [int]$SmtpPort=25,
438
439 [parameter(ParameterSetName="SMTP",Mandatory=$False)]
440 [switch]$UseSSL=$False,
441
442 [parameter(ParameterSetName="SMTP",Mandatory=$True)]
443 [string]$From="",
444
445 [parameter(ParameterSetName="SMTP",Mandatory=$True)]
446 [string]$To="",
447
448 [parameter(Mandatory=$False)]
449 [Switch]$Dev=$False,
450
451 [parameter(Mandatory=$False)]
452 [Alias("SI")]
453 [Switch]$ScriptInfo=$False
454
455 )
456
457
458#webster@carlwebster.com
459#@carlwebster on Twitter
460#http://www.CarlWebster.com
461#Created on April 10, 2014
462
463#Version 1.0 released to the community on May 31, 2014
464#
465#Version 1.01
466# Added an AddDateTime parameter
467#Version 1.1
468# Cleanup the script's parameters section
469# Code cleanup and standardization with the master template script
470# Requires PowerShell V3 or later
471# Removed support for Word 2007
472# Word 2007 references in help text removed
473# Cover page parameter now states only Word 2010 and 2013 are supported
474# Most Word 2007 references in script removed:
475# Function ValidateCoverPage
476# Function SetupWord
477# Function SaveandCloseDocumentandShutdownWord
478# Function CheckWord2007SaveAsPDFInstalled removed
479# If Word 2007 is detected, an error message is now given and the script is aborted
480# Fix when -ComputerName was entered as LocalHost, output filename said LocalHost and not actual server name
481# Cleanup Word table code for the first row and background color
482# Add Iain Brighton's Word table functions
483#Version 1.2
484# Cleanup some of the console output
485# Added error checking:
486# If script is run without -ComputerName, resolve LocalHost to computer and very it is a DHCP server
487# If script is run with -ComputerName, very it is a DHCP server
488#
489#Version 1.21 5-Oct-2015
490# Added support for Word 2016
491#
492#Version 1.22 25-Nov-2015
493# Updated help text and ReadMe for RSAT for Windows 10
494# Updated ReadMe with an example of running the script remotely
495# Tested script on Windows 10 x64 and Word 2016 x64
496#
497#Version 1.23 1-Feb-2016
498# Added DNS Dynamic update credentials from protocol properties, advanced tab
499#
500#Version 1.24 8-Feb-2016
501# Added specifying an optional output folder
502# Added the option to email the output file
503# Fixed several spacing and typo errors
504#
505#Version 1.30 4-May-2016
506# Fixed numerous issues discovered with the latest update to PowerShell V5
507# Color variables needed to be [long] and not [int] except for $wdColorBlack which is 0
508# Changed from using arrays to populating data in tables to strings
509# Fixed several incorrect variable names that kept PDFs from saving in Windows 10 and Office 2013
510# Fixed the rest of the $Var -eq $Null to $Null -eq $Var
511# Removed blocks of old commented out code
512# Removed the 10 second pauses waiting for Word to save and close.
513# Added -Dev parameter to create a text file of script errors
514# Added -ScriptInfo (SI) parameter to create a text file of script information
515# Added more script information to the console output when script starts
516# Cleaned up some issues in the help text
517# Commented out HTML parameters as HTML output is not ready
518# Added HTML functions to prep for adding HTML output
519
520Set-StrictMode -Version 2
521
522#force on
523$PSDefaultParameterValues = @{"*:Verbose"=$True}
524$SaveEAPreference = $ErrorActionPreference
525$ErrorActionPreference = 'SilentlyContinue'
526
527If($Null -eq $PDF)
528{
529 $PDF = $False
530}
531If($Null -eq $Text)
532{
533 $Text = $False
534}
535If($Null -eq $MSWord)
536{
537 $MSWord = $False
538}
539If($Null -eq $HTML)
540{
541 $HTML = $False
542}
543If($IncludeLeases -eq $Null)
544{
545 $IncludeLeases = $False
546}
547If($Null -eq $AddDateTime)
548{
549 $AddDateTime = $False
550}
551If($Null -eq $ComputerName)
552{
553 $ComputerName = "LocalHost"
554}
555If($Null -eq $Folder)
556{
557 $Folder = ""
558}
559If($Null -eq $SmtpServer)
560{
561 $SmtpServer = ""
562}
563If($Null -eq $SmtpPort)
564{
565 $SmtpPort = 25
566}
567If($Null -eq $UseSSL)
568{
569 $UseSSL = $False
570}
571If($Null -eq $From)
572{
573 $From = ""
574}
575If($Null -eq $To)
576{
577 $To = ""
578}
579If($Null -eq $Dev)
580{
581 $Dev = $False
582}
583If($Null -eq $ScriptInfo)
584{
585 $ScriptInfo = $False
586}
587
588If(!(Test-Path Variable:PDF))
589{
590 $PDF = $False
591}
592If(!(Test-Path Variable:Text))
593{
594 $Text = $False
595}
596If(!(Test-Path Variable:MSWord))
597{
598 $MSWord = $False
599}
600If(!(Test-Path Variable:HTML))
601{
602 $HTML = $False
603}
604If(!(Test-Path Variable:IncludeLeases))
605{
606 $IncludeLeases = $False
607}
608If(!(Test-Path Variable:AddDateTime))
609{
610 $AddDateTime = $False
611}
612If(!(Test-Path Variable:ComputerName))
613{
614 $ComputerName = "LocalHost"
615}
616If(!(Test-Path Variable:Folder))
617{
618 $Folder = ""
619}
620If(!(Test-Path Variable:SmtpServer))
621{
622 $SmtpServer = ""
623}
624If(!(Test-Path Variable:SmtpPort))
625{
626 $SmtpPort = 25
627}
628If(!(Test-Path Variable:UseSSL))
629{
630 $UseSSL = $False
631}
632If(!(Test-Path Variable:From))
633{
634 $From = ""
635}
636If(!(Test-Path Variable:To))
637{
638 $To = ""
639}
640If(!(Test-Path Variable:Dev))
641{
642 $Dev = $False
643}
644If(!(Test-Path Variable:ScriptInfo))
645{
646 $ScriptInfo = $False
647}
648
649If($Dev)
650{
651 $Error.Clear()
652 $Script:DevErrorFile = "$($pwd.Path)\DHCPInventoryScriptErrors_$(Get-Date -f yyyy-MM-dd_HHmm).txt"
653}
654
655If($Null -eq $MSWord)
656{
657 If($Text -or $HTML -or $PDF)
658 {
659 $MSWord = $False
660 }
661 Else
662 {
663 $MSWord = $True
664 }
665}
666
667If($MSWord -eq $False -and $PDF -eq $False -and $Text -eq $False -and $HTML -eq $False)
668{
669 $MSWord = $True
670}
671
672Write-Verbose "$(Get-Date): Testing output parameters"
673
674If($MSWord)
675{
676 Write-Verbose "$(Get-Date): MSWord is set"
677}
678ElseIf($PDF)
679{
680 Write-Verbose "$(Get-Date): PDF is set"
681}
682ElseIf($Text)
683{
684 Write-Verbose "$(Get-Date): Text is set"
685}
686ElseIf($HTML)
687{
688 Write-Verbose "$(Get-Date): HTML is set"
689}
690Else
691{
692 $ErrorActionPreference = $SaveEAPreference
693 Write-Verbose "$(Get-Date): Unable to determine output parameter"
694 If($Null -eq $MSWord)
695 {
696 Write-Verbose "$(Get-Date): MSWord is Null"
697 }
698 ElseIf($Null -eq $PDF)
699 {
700 Write-Verbose "$(Get-Date): PDF is Null"
701 }
702 ElseIf($Null -eq $Text)
703 {
704 Write-Verbose "$(Get-Date): Text is Null"
705 }
706 ElseIf($Null -eq $HTML)
707 {
708 Write-Verbose "$(Get-Date): HTML is Null"
709 }
710 Else
711 {
712 Write-Verbose "$(Get-Date): MSWord is $($MSWord)"
713 Write-Verbose "$(Get-Date): PDF is $($PDF)"
714 Write-Verbose "$(Get-Date): Text is $($Text)"
715 Write-Verbose "$(Get-Date): HTML is $($HTML)"
716 }
717 Write-Error "Unable to determine output parameter. Script cannot continue"
718 Exit
719}
720
721If($Folder -ne "")
722{
723 Write-Verbose "$(Get-Date): Testing folder path"
724 #does it exist
725 If(Test-Path $Folder -EA 0)
726 {
727 #it exists, now check to see if it is a folder and not a file
728 If(Test-Path $Folder -pathType Container -EA 0)
729 {
730 #it exists and it is a folder
731 Write-Verbose "$(Get-Date): Folder path $Folder exists and is a folder"
732 }
733 Else
734 {
735 #it exists but it is a file not a folder
736 Write-Error "Folder $Folder is a file, not a folder. Script cannot continue"
737 Exit
738 }
739 }
740 Else
741 {
742 #does not exist
743 Write-Error "Folder $Folder does not exist. Script cannot continue"
744 Exit
745 }
746}
747
748#region initialize variables for word html and text
749[string]$Script:RunningOS = (Get-WmiObject -class Win32_OperatingSystem -EA 0).Caption
750
751If($MSWord -or $PDF)
752{
753 #try and fix the issue with the $CompanyName variable
754 $Script:CoName = $CompanyName
755 Write-Verbose "$(Get-Date): CoName is $($Script:CoName)"
756
757 #the following values were attained from
758 #http://groovy.codehaus.org/modules/scriptom/1.6.0/scriptom-office-2K3-tlb/apidocs/
759 #http://msdn.microsoft.com/en-us/library/office/aa211923(v=office.11).aspx
760 [int]$wdAlignPageNumberRight = 2
761 [long]$wdColorGray15 = 14277081
762 [long]$wdColorGray05 = 15987699
763 [int]$wdMove = 0
764 [int]$wdSeekMainDocument = 0
765 [int]$wdSeekPrimaryFooter = 4
766 [int]$wdStory = 6
767 [long]$wdColorRed = 255
768 [int]$wdColorBlack = 0
769 [int]$wdWord2007 = 12
770 [int]$wdWord2010 = 14
771 [int]$wdWord2013 = 15
772 [int]$wdWord2016 = 16
773 [int]$wdFormatDocumentDefault = 16
774 [int]$wdFormatPDF = 17
775 #http://blogs.technet.com/b/heyscriptingguy/archive/2006/03/01/how-can-i-right-align-a-single-column-in-a-word-table.aspx
776 #http://msdn.microsoft.com/en-us/library/office/ff835817%28v=office.15%29.aspx
777 [int]$wdAlignParagraphLeft = 0
778 [int]$wdAlignParagraphCenter = 1
779 [int]$wdAlignParagraphRight = 2
780 #http://msdn.microsoft.com/en-us/library/office/ff193345%28v=office.15%29.aspx
781 [int]$wdCellAlignVerticalTop = 0
782 [int]$wdCellAlignVerticalCenter = 1
783 [int]$wdCellAlignVerticalBottom = 2
784 #http://msdn.microsoft.com/en-us/library/office/ff844856%28v=office.15%29.aspx
785 [int]$wdAutoFitFixed = 0
786 [int]$wdAutoFitContent = 1
787 [int]$wdAutoFitWindow = 2
788 #http://msdn.microsoft.com/en-us/library/office/ff821928%28v=office.15%29.aspx
789 [int]$wdAdjustNone = 0
790 [int]$wdAdjustProportional = 1
791 [int]$wdAdjustFirstColumn = 2
792 [int]$wdAdjustSameWidth = 3
793
794 [int]$PointsPerTabStop = 36
795 [int]$Indent0TabStops = 0 * $PointsPerTabStop
796 [int]$Indent1TabStops = 1 * $PointsPerTabStop
797 [int]$Indent2TabStops = 2 * $PointsPerTabStop
798 [int]$Indent3TabStops = 3 * $PointsPerTabStop
799 [int]$Indent4TabStops = 4 * $PointsPerTabStop
800
801 # http://www.thedoctools.com/index.php?show=wt_style_names_english_danish_german_french
802 [int]$wdStyleHeading1 = -2
803 [int]$wdStyleHeading2 = -3
804 [int]$wdStyleHeading3 = -4
805 [int]$wdStyleHeading4 = -5
806 [int]$wdStyleNoSpacing = -158
807 [int]$wdTableGrid = -155
808
809 #http://groovy.codehaus.org/modules/scriptom/1.6.0/scriptom-office-2K3-tlb/apidocs/org/codehaus/groovy/scriptom/tlb/office/word/WdLineStyle.html
810 [int]$wdLineStyleNone = 0
811 [int]$wdLineStyleSingle = 1
812
813 [int]$wdHeadingFormatTrue = -1
814 [int]$wdHeadingFormatFalse = 0
815}
816
817If($HTML)
818{
819 Set htmlredmask -Option AllScope -Value "#FF0000" 4>$Null
820 Set htmlcyanmask -Option AllScope -Value "#00FFFF" 4>$Null
821 Set htmlbluemask -Option AllScope -Value "#0000FF" 4>$Null
822 Set htmldarkbluemask -Option AllScope -Value "#0000A0" 4>$Null
823 Set htmllightbluemask -Option AllScope -Value "#ADD8E6" 4>$Null
824 Set htmlpurplemask -Option AllScope -Value "#800080" 4>$Null
825 Set htmlyellowmask -Option AllScope -Value "#FFFF00" 4>$Null
826 Set htmllimemask -Option AllScope -Value "#00FF00" 4>$Null
827 Set htmlmagentamask -Option AllScope -Value "#FF00FF" 4>$Null
828 Set htmlwhitemask -Option AllScope -Value "#FFFFFF" 4>$Null
829 Set htmlsilvermask -Option AllScope -Value "#C0C0C0" 4>$Null
830 Set htmlgraymask -Option AllScope -Value "#808080" 4>$Null
831 Set htmlblackmask -Option AllScope -Value "#000000" 4>$Null
832 Set htmlorangemask -Option AllScope -Value "#FFA500" 4>$Null
833 Set htmlmaroonmask -Option AllScope -Value "#800000" 4>$Null
834 Set htmlgreenmask -Option AllScope -Value "#008000" 4>$Null
835 Set htmlolivemask -Option AllScope -Value "#808000" 4>$Null
836
837 Set htmlbold -Option AllScope -Value 1 4>$Null
838 Set htmlitalics -Option AllScope -Value 2 4>$Null
839 Set htmlred -Option AllScope -Value 4 4>$Null
840 Set htmlcyan -Option AllScope -Value 8 4>$Null
841 Set htmlblue -Option AllScope -Value 16 4>$Null
842 Set htmldarkblue -Option AllScope -Value 32 4>$Null
843 Set htmllightblue -Option AllScope -Value 64 4>$Null
844 Set htmlpurple -Option AllScope -Value 128 4>$Null
845 Set htmlyellow -Option AllScope -Value 256 4>$Null
846 Set htmllime -Option AllScope -Value 512 4>$Null
847 Set htmlmagenta -Option AllScope -Value 1024 4>$Null
848 Set htmlwhite -Option AllScope -Value 2048 4>$Null
849 Set htmlsilver -Option AllScope -Value 4096 4>$Null
850 Set htmlgray -Option AllScope -Value 8192 4>$Null
851 Set htmlolive -Option AllScope -Value 16384 4>$Null
852 Set htmlorange -Option AllScope -Value 32768 4>$Null
853 Set htmlmaroon -Option AllScope -Value 65536 4>$Null
854 Set htmlgreen -Option AllScope -Value 131072 4>$Null
855 Set htmlblack -Option AllScope -Value 262144 4>$Null
856}
857
858If($TEXT)
859{
860 $global:output = ""
861}
862#endregion
863
864Function SetWordHashTable
865{
866 Param([string]$CultureCode)
867
868 #optimized by Michael B. SMith
869
870 # DE and FR translations for Word 2010 by Vladimir Radojevic
871 # Vladimir.Radojevic@Commerzreal.com
872
873 # DA translations for Word 2010 by Thomas Daugaard
874 # Citrix Infrastructure Specialist at edgemo A/S
875
876 # CA translations by Javier Sanchez
877 # CEO & Founder 101 Consulting
878
879 #ca - Catalan
880 #da - Danish
881 #de - German
882 #en - English
883 #es - Spanish
884 #fi - Finnish
885 #fr - French
886 #nb - Norwegian
887 #nl - Dutch
888 #pt - Portuguese
889 #sv - Swedish
890
891 [string]$toc = $(
892 Switch ($CultureCode)
893 {
894 'ca-' { 'Taula automática 2' }
895
896 'da-' { 'Automatisk tabel 2' }
897
898 'de-' { 'Automatische Tabelle 2' }
899
900 'en-' { 'Automatic Table 2' }
901
902 'es-' { 'Tabla automática 2' }
903
904 'fi-' { 'Automaattinen taulukko 2' }
905
906 'fr-' { 'Sommaire Automatique 2' }
907
908 'nb-' { 'Automatisk tabell 2' }
909
910 'nl-' { 'Automatische inhoudsopgave 2' }
911
912 'pt-' { 'Sumário Automático 2' }
913
914 'sv-' { 'Automatisk innehållsförteckning2' }
915 }
916 )
917
918 $Script:myHash = @{}
919 $Script:myHash.Word_TableOfContents = $toc
920 $Script:myHash.Word_NoSpacing = $wdStyleNoSpacing
921 $Script:myHash.Word_Heading1 = $wdStyleheading1
922 $Script:myHash.Word_Heading2 = $wdStyleheading2
923 $Script:myHash.Word_Heading3 = $wdStyleheading3
924 $Script:myHash.Word_Heading4 = $wdStyleheading4
925 $Script:myHash.Word_TableGrid = $wdTableGrid
926}
927
928Function GetCulture
929{
930 Param([int]$WordValue)
931
932 #codes obtained from http://support.microsoft.com/kb/221435
933 #http://msdn.microsoft.com/en-us/library/bb213877(v=office.12).aspx
934 $CatalanArray = 1027
935 $DanishArray = 1030
936 $DutchArray = 2067, 1043
937 $EnglishArray = 3081, 10249, 4105, 9225, 6153, 8201, 5129, 13321, 7177, 11273, 2057, 1033, 12297
938 $FinnishArray = 1035
939 $FrenchArray = 2060, 1036, 11276, 3084, 12300, 5132, 13324, 6156, 8204, 10252, 7180, 9228, 4108
940 $GermanArray = 1031, 3079, 5127, 4103, 2055
941 $NorwegianArray = 1044, 2068
942 $PortugueseArray = 1046, 2070
943 $SpanishArray = 1034, 11274, 16394, 13322, 9226, 5130, 7178, 12298, 17418, 4106, 18442, 19466, 6154, 15370, 10250, 20490, 3082, 14346, 8202
944 $SwedishArray = 1053, 2077
945
946 #ca - Catalan
947 #da - Danish
948 #de - German
949 #en - English
950 #es - Spanish
951 #fi - Finnish
952 #fr - French
953 #nb - Norwegian
954 #nl - Dutch
955 #pt - Portuguese
956 #sv - Swedish
957
958 Switch ($WordValue)
959 {
960 {$CatalanArray -contains $_} {$CultureCode = "ca-"}
961 {$DanishArray -contains $_} {$CultureCode = "da-"}
962 {$DutchArray -contains $_} {$CultureCode = "nl-"}
963 {$EnglishArray -contains $_} {$CultureCode = "en-"}
964 {$FinnishArray -contains $_} {$CultureCode = "fi-"}
965 {$FrenchArray -contains $_} {$CultureCode = "fr-"}
966 {$GermanArray -contains $_} {$CultureCode = "de-"}
967 {$NorwegianArray -contains $_} {$CultureCode = "nb-"}
968 {$PortugueseArray -contains $_} {$CultureCode = "pt-"}
969 {$SpanishArray -contains $_} {$CultureCode = "es-"}
970 {$SwedishArray -contains $_} {$CultureCode = "sv-"}
971 Default {$CultureCode = "en-"}
972 }
973
974 Return $CultureCode
975}
976
977Function ValidateCoverPage
978{
979 Param([int]$xWordVersion, [string]$xCP, [string]$CultureCode)
980
981 $xArray = ""
982
983 Switch ($CultureCode)
984 {
985 'ca-' {
986 If($xWordVersion -eq $wdWord2016)
987 {
988 $xArray = ("Austin", "En bandes", "Faceta", "Filigrana",
989 "Integral", "Ió (clar)", "Ió (fosc)", "LÃnia lateral",
990 "Moviment", "QuadrÃcula", "Retrospectiu", "Sector (clar)",
991 "Sector (fosc)", "Semà for", "Visualització principal", "Whisp")
992 }
993 ElseIf($xWordVersion -eq $wdWord2013)
994 {
995 $xArray = ("Austin", "En bandes", "Faceta", "Filigrana",
996 "Integral", "Ió (clar)", "Ió (fosc)", "LÃnia lateral",
997 "Moviment", "QuadrÃcula", "Retrospectiu", "Sector (clar)",
998 "Sector (fosc)", "Semà for", "Visualització", "Whisp")
999 }
1000 ElseIf($xWordVersion -eq $wdWord2010)
1001 {
1002 $xArray = ("Alfabet", "Anual", "Austin", "Conservador",
1003 "Contrast", "Cubicles", "Diplomà tic", "Exposició",
1004 "LÃnia lateral", "Mod", "Mosiac", "Moviment", "Paper de diari",
1005 "Perspectiva", "Piles", "QuadrÃcula", "Sobri",
1006 "Transcendir", "Trencaclosques")
1007 }
1008 }
1009
1010 'da-' {
1011 If($xWordVersion -eq $wdWord2016)
1012 {
1013 $xArray = ("Austin", "BevægElse", "Brusen", "Facet", "Filigran",
1014 "Gitter", "Integral", "Ion (lys)", "Ion (mørk)",
1015 "Retro", "Semafor", "Sidelinje", "Stribet",
1016 "Udsnit (lys)", "Udsnit (mørk)", "Visningsmaster")
1017 }
1018 ElseIf($xWordVersion -eq $wdWord2013)
1019 {
1020 $xArray = ("BevægElse", "Brusen", "Ion (lys)", "Filigran",
1021 "Retro", "Semafor", "Visningsmaster", "Integral",
1022 "Facet", "Gitter", "Stribet", "Sidelinje", "Udsnit (lys)",
1023 "Udsnit (mørk)", "Ion (mørk)", "Austin")
1024 }
1025 ElseIf($xWordVersion -eq $wdWord2010)
1026 {
1027 $xArray = ("BevægElse", "Moderat", "Perspektiv", "Firkanter",
1028 "Overskrid", "Alfabet", "Kontrast", "Stakke", "Fliser", "GÃ¥de",
1029 "Gitter", "Austin", "Eksponering", "Sidelinje", "Enkel",
1030 "NÃ¥lestribet", "Ã…rlig", "Avispapir", "Tradionel")
1031 }
1032 }
1033
1034 'de-' {
1035 If($xWordVersion -eq $wdWord2016)
1036 {
1037 $xArray = ("Austin", "Bewegung", "Facette", "Filigran",
1038 "Gebändert", "Integral", "Ion (dunkel)", "Ion (hell)",
1039 "Pfiff", "Randlinie", "Raster", "Rückblick",
1040 "Segment (dunkel)", "Segment (hell)", "Semaphor",
1041 "ViewMaster")
1042 }
1043 ElseIf($xWordVersion -eq $wdWord2013)
1044 {
1045 $xArray = ("Semaphor", "Segment (hell)", "Ion (hell)",
1046 "Raster", "Ion (dunkel)", "Filigran", "Rückblick", "Pfiff",
1047 "ViewMaster", "Segment (dunkel)", "Verbunden", "Bewegung",
1048 "Randlinie", "Austin", "Integral", "Facette")
1049 }
1050 ElseIf($xWordVersion -eq $wdWord2010)
1051 {
1052 $xArray = ("Alphabet", "Austin", "Bewegung", "Durchscheinend",
1053 "Herausgestellt", "Jährlich", "Kacheln", "Kontrast", "Kubistisch",
1054 "Modern", "Nadelstreifen", "Perspektive", "Puzzle", "Randlinie",
1055 "Raster", "Schlicht", "Stapel", "Traditionell", "Zeitungspapier")
1056 }
1057 }
1058
1059 'en-' {
1060 If($xWordVersion -eq $wdWord2013 -or $xWordVersion -eq $wdWord2016)
1061 {
1062 $xArray = ("Austin", "Banded", "Facet", "Filigree", "Grid",
1063 "Integral", "Ion (Dark)", "Ion (Light)", "Motion", "Retrospect",
1064 "Semaphore", "Sideline", "Slice (Dark)", "Slice (Light)", "ViewMaster",
1065 "Whisp")
1066 }
1067 ElseIf($xWordVersion -eq $wdWord2010)
1068 {
1069 $xArray = ("Alphabet", "Annual", "Austere", "Austin", "Conservative",
1070 "Contrast", "Cubicles", "Exposure", "Grid", "Mod", "Motion", "Newsprint",
1071 "Perspective", "Pinstripes", "Puzzle", "Sideline", "Stacks", "Tiles", "Transcend")
1072 }
1073 }
1074
1075 'es-' {
1076 If($xWordVersion -eq $wdWord2016)
1077 {
1078 $xArray = ("Austin", "Con bandas", "Cortar (oscuro)", "CuadrÃcula",
1079 "Whisp", "Faceta", "Filigrana", "Integral", "Ion (claro)",
1080 "Ion (oscuro)", "LÃnea lateral", "Movimiento", "Retrospectiva",
1081 "Semáforo", "Slice (luz)", "Vista principal", "Whisp")
1082 }
1083 ElseIf($xWordVersion -eq $wdWord2013)
1084 {
1085 $xArray = ("Whisp", "Vista principal", "Filigrana", "Austin",
1086 "Slice (luz)", "Faceta", "Semáforo", "Retrospectiva", "CuadrÃcula",
1087 "Movimiento", "Cortar (oscuro)", "LÃnea lateral", "Ion (oscuro)",
1088 "Ion (claro)", "Integral", "Con bandas")
1089 }
1090 ElseIf($xWordVersion -eq $wdWord2010)
1091 {
1092 $xArray = ("Alfabeto", "Anual", "Austero", "Austin", "Conservador",
1093 "Contraste", "CuadrÃcula", "CubÃculos", "Exposición", "LÃnea lateral",
1094 "Moderno", "Mosaicos", "Movimiento", "Papel periódico",
1095 "Perspectiva", "Pilas", "Puzzle", "Rayas", "Sobrepasar")
1096 }
1097 }
1098
1099 'fi-' {
1100 If($xWordVersion -eq $wdWord2016)
1101 {
1102 $xArray = ("Filigraani", "Integraali", "Ioni (tumma)",
1103 "Ioni (vaalea)", "Opastin", "Pinta", "Retro", "Sektori (tumma)",
1104 "Sektori (vaalea)", "Vaihtuvavärinen", "ViewMaster", "Austin",
1105 "Kuiskaus", "Liike", "Ruudukko", "Sivussa")
1106 }
1107 ElseIf($xWordVersion -eq $wdWord2013)
1108 {
1109 $xArray = ("Filigraani", "Integraali", "Ioni (tumma)",
1110 "Ioni (vaalea)", "Opastin", "Pinta", "Retro", "Sektori (tumma)",
1111 "Sektori (vaalea)", "Vaihtuvavärinen", "ViewMaster", "Austin",
1112 "Kiehkura", "Liike", "Ruudukko", "Sivussa")
1113 }
1114 ElseIf($xWordVersion -eq $wdWord2010)
1115 {
1116 $xArray = ("Aakkoset", "Askeettinen", "Austin", "Kontrasti",
1117 "Laatikot", "Liike", "Liituraita", "Mod", "Osittain peitossa",
1118 "Palapeli", "Perinteinen", "Perspektiivi", "Pinot", "Ruudukko",
1119 "Ruudut", "Sanomalehtipaperi", "Sivussa", "Vuotuinen", "Ylitys")
1120 }
1121 }
1122
1123 'fr-' {
1124 If($xWordVersion -eq $wdWord2013 -or $xWordVersion -eq $wdWord2016)
1125 {
1126 $xArray = ("À bandes", "Austin", "Facette", "Filigrane",
1127 "Guide", "Intégrale", "Ion (clair)", "Ion (foncé)",
1128 "Lignes latérales", "Quadrillage", "Rétrospective", "Secteur (clair)",
1129 "Secteur (foncé)", "Sémaphore", "ViewMaster", "Whisp")
1130 }
1131 ElseIf($xWordVersion -eq $wdWord2010)
1132 {
1133 $xArray = ("Alphabet", "Annuel", "Austère", "Austin",
1134 "Blocs empilés", "Classique", "Contraste", "Emplacements de bureau",
1135 "Exposition", "Guide", "Ligne latérale", "Moderne",
1136 "Mosaïques", "Mots croisés", "Papier journal", "Perspective",
1137 "Quadrillage", "Rayures fines", "Transcendant")
1138 }
1139 }
1140
1141 'nb-' {
1142 If($xWordVersion -eq $wdWord2013 -or $xWordVersion -eq $wdWord2016)
1143 {
1144 $xArray = ("Austin", "BevegElse", "Dempet", "Fasett", "Filigran",
1145 "Integral", "Ion (lys)", "Ion (mørk)", "Retrospekt", "Rutenett",
1146 "Sektor (lys)", "Sektor (mørk)", "Semafor", "Sidelinje", "Stripet",
1147 "ViewMaster")
1148 }
1149 ElseIf($xWordVersion -eq $wdWord2010)
1150 {
1151 $xArray = ("Alfabet", "Ã…rlig", "Avistrykk", "Austin", "Avlukker",
1152 "BevegElse", "Engasjement", "Enkel", "Fliser", "Konservativ",
1153 "Kontrast", "Mod", "Perspektiv", "Puslespill", "Rutenett", "Sidelinje",
1154 "Smale striper", "Stabler", "Transcenderende")
1155 }
1156 }
1157
1158 'nl-' {
1159 If($xWordVersion -eq $wdWord2013 -or $xWordVersion -eq $wdWord2016)
1160 {
1161 $xArray = ("Austin", "Beweging", "Facet", "Filigraan", "Gestreept",
1162 "Integraal", "Ion (donker)", "Ion (licht)", "Raster",
1163 "Segment (Light)", "Semafoor", "Slice (donker)", "Spriet",
1164 "Terugblik", "Terzijde", "ViewMaster")
1165 }
1166 ElseIf($xWordVersion -eq $wdWord2010)
1167 {
1168 $xArray = ("Aantrekkelijk", "Alfabet", "Austin", "Bescheiden",
1169 "Beweging", "Blikvanger", "Contrast", "Eenvoudig", "Jaarlijks",
1170 "Krantenpapier", "Krijtstreep", "Kubussen", "Mod", "Perspectief",
1171 "Puzzel", "Raster", "Stapels",
1172 "Tegels", "Terzijde")
1173 }
1174 }
1175
1176 'pt-' {
1177 If($xWordVersion -eq $wdWord2013 -or $xWordVersion -eq $wdWord2016)
1178 {
1179 $xArray = ("Animação", "Austin", "Em Tiras", "Exibição Mestra",
1180 "Faceta", "Fatia (Clara)", "Fatia (Escura)", "Filete", "Filigrana",
1181 "Grade", "Integral", "Ãon (Claro)", "Ãon (Escuro)", "Linha Lateral",
1182 "Retrospectiva", "Semáforo")
1183 }
1184 ElseIf($xWordVersion -eq $wdWord2010)
1185 {
1186 $xArray = ("Alfabeto", "Animação", "Anual", "Austero", "Austin", "Baias",
1187 "Conservador", "Contraste", "Exposição", "Grade", "Ladrilhos",
1188 "Linha Lateral", "Listras", "Mod", "Papel Jornal", "Perspectiva", "Pilhas",
1189 "Quebra-cabeça", "Transcend")
1190 }
1191 }
1192
1193 'sv-' {
1194 If($xWordVersion -eq $wdWord2013 -or $xWordVersion -eq $wdWord2016)
1195 {
1196 $xArray = ("Austin", "Band", "Fasett", "Filigran", "Integrerad", "Jon (ljust)",
1197 "Jon (mörkt)", "Knippe", "Rutnät", "RörElse", "Sektor (ljus)", "Sektor (mörk)",
1198 "Semafor", "Sidlinje", "VisaHuvudsida", "Ã…terblick")
1199 }
1200 ElseIf($xWordVersion -eq $wdWord2010)
1201 {
1202 $xArray = ("Alfabetmönster", "Austin", "Enkelt", "Exponering", "Konservativt",
1203 "Kontrast", "Kritstreck", "Kuber", "Perspektiv", "Plattor", "Pussel", "Rutnät",
1204 "RörElse", "Sidlinje", "Sobert", "Staplat", "Tidningspapper", "Årligt",
1205 "Övergående")
1206 }
1207 }
1208
1209 Default {
1210 If($xWordVersion -eq $wdWord2013 -or $xWordVersion -eq $wdWord2016)
1211 {
1212 $xArray = ("Austin", "Banded", "Facet", "Filigree", "Grid",
1213 "Integral", "Ion (Dark)", "Ion (Light)", "Motion", "Retrospect",
1214 "Semaphore", "Sideline", "Slice (Dark)", "Slice (Light)", "ViewMaster",
1215 "Whisp")
1216 }
1217 ElseIf($xWordVersion -eq $wdWord2010)
1218 {
1219 $xArray = ("Alphabet", "Annual", "Austere", "Austin", "Conservative",
1220 "Contrast", "Cubicles", "Exposure", "Grid", "Mod", "Motion", "Newsprint",
1221 "Perspective", "Pinstripes", "Puzzle", "Sideline", "Stacks", "Tiles", "Transcend")
1222 }
1223 }
1224 }
1225
1226 If($xArray -contains $xCP)
1227 {
1228 $xArray = $Null
1229 Return $True
1230 }
1231 Else
1232 {
1233 $xArray = $Null
1234 Return $False
1235 }
1236}
1237
1238Function CheckWordPrereq
1239{
1240 If((Test-Path REGISTRY::HKEY_CLASSES_ROOT\Word.Application) -eq $False)
1241 {
1242 $ErrorActionPreference = $SaveEAPreference
1243 Write-Host "`n`n`t`tThis script directly outputs to Microsoft Word, please install Microsoft Word`n`n"
1244 Exit
1245 }
1246
1247 #find out our session (usually "1" except on TS/RDC or Citrix)
1248 $SessionID = (Get-Process -PID $PID).SessionId
1249
1250 #Find out if winword is running in our session
1251 [bool]$wordrunning = ((Get-Process 'WinWord' -ea 0)|?{$_.SessionId -eq $SessionID}) -ne $Null
1252 If($wordrunning)
1253 {
1254 $ErrorActionPreference = $SaveEAPreference
1255 Write-Host "`n`n`tPlease close all instances of Microsoft Word before running this report.`n`n"
1256 Exit
1257 }
1258}
1259
1260Function ValidateCompanyName
1261{
1262 [bool]$xResult = Test-RegistryValue "HKCU:\Software\Microsoft\Office\Common\UserInfo" "CompanyName"
1263 If($xResult)
1264 {
1265 Return Get-RegistryValue "HKCU:\Software\Microsoft\Office\Common\UserInfo" "CompanyName"
1266 }
1267 Else
1268 {
1269 $xResult = Test-RegistryValue "HKCU:\Software\Microsoft\Office\Common\UserInfo" "Company"
1270 If($xResult)
1271 {
1272 Return Get-RegistryValue "HKCU:\Software\Microsoft\Office\Common\UserInfo" "Company"
1273 }
1274 Else
1275 {
1276 Return ""
1277 }
1278 }
1279}
1280
1281#http://stackoverflow.com/questions/5648931/test-if-registry-value-exists
1282# This Function just gets $True or $False
1283Function Test-RegistryValue($path, $name)
1284{
1285 $key = Get-Item -LiteralPath $path -EA 0
1286 $key -and $Null -ne $key.GetValue($name, $Null)
1287}
1288
1289# Gets the specified registry value or $Null if it is missing
1290Function Get-RegistryValue($path, $name)
1291{
1292 $key = Get-Item -LiteralPath $path -EA 0
1293 If($key)
1294 {
1295 $key.GetValue($name, $Null)
1296 }
1297 Else
1298 {
1299 $Null
1300 }
1301}
1302
1303#region word, text and html line output functions
1304Function line
1305#function created by Michael B. Smith, Exchange MVP
1306#@essentialexchange on Twitter
1307#http://TheEssentialExchange.com
1308#for creating the formatted text report
1309#created March 2011
1310#updated March 2014
1311{
1312 Param( [int]$tabs = 0, [string]$name = '', [string]$value = '', [string]$newline = "`r`n", [switch]$nonewline )
1313 While( $tabs -gt 0 ) { $Global:Output += "`t"; $tabs--; }
1314 If( $nonewline )
1315 {
1316 $Global:Output += $name + $value
1317 }
1318 Else
1319 {
1320 $Global:Output += $name + $value + $newline
1321 }
1322}
1323
1324Function WriteWordLine
1325#Function created by Ryan Revord
1326#@rsrevord on Twitter
1327#Function created to make output to Word easy in this script
1328#updated 27-Mar-2014 to include font name, font size, italics and bold options
1329{
1330 Param([int]$style=0,
1331 [int]$tabs = 0,
1332 [string]$name = '',
1333 [string]$value = '',
1334 [string]$fontName=$Null,
1335 [int]$fontSize=0,
1336 [bool]$italics=$False,
1337 [bool]$boldface=$False,
1338 [Switch]$nonewline)
1339
1340 #Build output style
1341 [string]$output = ""
1342 Switch ($style)
1343 {
1344 0 {$Script:Selection.Style = $Script:MyHash.Word_NoSpacing}
1345 1 {$Script:Selection.Style = $Script:MyHash.Word_Heading1}
1346 2 {$Script:Selection.Style = $Script:MyHash.Word_Heading2}
1347 3 {$Script:Selection.Style = $Script:MyHash.Word_Heading3}
1348 4 {$Script:Selection.Style = $Script:MyHash.Word_Heading4}
1349 Default {$Script:Selection.Style = $Script:MyHash.Word_NoSpacing}
1350 }
1351
1352 #build # of tabs
1353 While($tabs -gt 0)
1354 {
1355 $output += "`t"; $tabs--;
1356 }
1357
1358 If(![String]::IsNullOrEmpty($fontName))
1359 {
1360 $Script:Selection.Font.name = $fontName
1361 }
1362
1363 If($fontSize -ne 0)
1364 {
1365 $Script:Selection.Font.size = $fontSize
1366 }
1367
1368 If($italics -eq $True)
1369 {
1370 $Script:Selection.Font.Italic = $True
1371 }
1372
1373 If($boldface -eq $True)
1374 {
1375 $Script:Selection.Font.Bold = $True
1376 }
1377
1378 #output the rest of the parameters.
1379 $output += $name + $value
1380 $Script:Selection.TypeText($output)
1381
1382 #test for new WriteWordLine 0.
1383 If($nonewline)
1384 {
1385 # Do nothing.
1386 }
1387 Else
1388 {
1389 $Script:Selection.TypeParagraph()
1390 }
1391}
1392
1393#***********************************************************************************************************
1394# WriteHTMLLine
1395#***********************************************************************************************************
1396
1397<#
1398.Synopsis
1399 Writes a line of output for HTML output
1400.DESCRIPTION
1401 This function formats an HTML line
1402.USAGE
1403 WriteHTMLLine <Style> <Tabs> <Name> <Value> <Font Name> <Font Size> <Options>
1404
1405 0 for Font Size denotes using the default font size of 2 or 10 point
1406
1407.EXAMPLE
1408 WriteHTMLLine 0 0 ""
1409
1410 Writes a blank line with no style or tab stops, obviously none needed.
1411
1412.EXAMPLE
1413 WriteHTMLLine 0 1 "This is a regular line of text indented 1 tab stops"
1414
1415 Writes a line with 1 tab stop.
1416
1417.EXAMPLE
1418 WriteHTMLLine 0 0 "This is a regular line of text in the default font in italics" "" $Null 0 $htmlitalics
1419
1420 Writes a line omitting font and font size and setting the italics attribute
1421
1422.EXAMPLE
1423 WriteHTMLLine 0 0 "This is a regular line of text in the default font in bold" "" $Null 0 $htmlbold
1424
1425 Writes a line omitting font and font size and setting the bold attribute
1426
1427.EXAMPLE
1428 WriteHTMLLine 0 0 "This is a regular line of text in the default font in bold italics" "" $Null 0 ($htmlbold -bor $htmlitalics)
1429
1430 Writes a line omitting font and font size and setting both italics and bold options
1431
1432.EXAMPLE
1433 WriteHTMLLine 0 0 "This is a regular line of text in the default font in 10 point" "" $Null 2 # 10 point font
1434
1435 Writes a line using 10 point font
1436
1437.EXAMPLE
1438 WriteHTMLLine 0 0 "This is a regular line of text in Courier New font" "" "Courier New" 0
1439
1440 Writes a line using Courier New Font and 0 font point size (default = 2 if set to 0)
1441
1442.EXAMPLE
1443 WriteHTMLLine 0 0 "This is a regular line of RED text indented 0 tab stops with the computer name as data in 10 point Courier New bold italics: " $env:computername "Courier New" 2 ($htmlbold -bor $htmlred -bor $htmlitalics)
1444
1445 Writes a line using Courier New Font with first and second string values to be used, also uses 10 point font with bold, italics and red color options set.
1446
1447.NOTES
1448
1449 Font Size - Unlike word, there is a limited set of font sizes that can be used in HTML. They are:
1450 0 - default which actually gives it a 2 or 10 point.
1451 1 - 7.5 point font size
1452 2 - 10 point
1453 3 - 13.5 point
1454 4 - 15 point
1455 5 - 18 point
1456 6 - 24 point
1457 7 - 36 point
1458 Any number larger than 7 defaults to 7
1459
1460 Style - Refers to the headers that are used with output and resemble the headers in word,
1461 HTML supports headers h1-h6 and h1-h4 are more commonly used. Unlike word, H1 will not
1462 give you a blue colored font, you will have to set that yourself.
1463
1464 Colors and Bold/Italics Flags are:
1465
1466 htmlbold
1467 htmlitalics
1468 htmlred
1469 htmlcyan
1470 htmlblue
1471 htmldarkblue
1472 htmllightblue
1473 htmlpurple
1474 htmlyellow
1475 htmllime
1476 htmlmagenta
1477 htmlwhite
1478 htmlsilver
1479 htmlgray
1480 htmlolive
1481 htmlorange
1482 htmlmaroon
1483 htmlgreen
1484 htmlblack
1485#>
1486
1487Function WriteHTMLLine
1488#Function created by Ken Avram
1489#Function created to make output to HTML easy in this script
1490{
1491 Param([int]$style=0,
1492 [int]$tabs = 0,
1493 [string]$name = '',
1494 [string]$value = '',
1495 [string]$fontName="Calibri",
1496 [int]$fontSize=1,
1497 [int]$options=$htmlblack)
1498
1499
1500 #Build output style
1501 [string]$output = ""
1502
1503 If([String]::IsNullOrEmpty($Name))
1504 {
1505 $HTMLBody = "<p></p>"
1506 }
1507 Else
1508 {
1509 $color = CheckHTMLColor $options
1510
1511 #build # of tabs
1512
1513 While($tabs -gt 0)
1514 {
1515 $output += " "; $tabs--;
1516 }
1517
1518 $HTMLFontName = $fontName
1519
1520 $HTMLBody = ""
1521
1522 If($options -band $htmlitalics)
1523 {
1524 $HTMLBody += "<i>"
1525 }
1526
1527 If($options -band $htmlbold)
1528 {
1529 $HTMLBody += "<b>"
1530 }
1531
1532 #output the rest of the parameters.
1533 $output += $name + $value
1534
1535 $HTMLBody += "<br><font face='" + $HTMLFontName + "' " + "color='" + $color + "' size='" + $fontsize + "'>"
1536
1537 Switch ($style)
1538 {
1539 1 {$HTMLStyle = "<h1>"}
1540 2 {$HTMLStyle = "<h2>"}
1541 3 {$HTMLStyle = "<h3>"}
1542 4 {$HTMLStyle = "<h4>"}
1543 Default {$HTMLStyle = ""}
1544 }
1545
1546 $HTMLBody += $HTMLStyle + $output
1547
1548 Switch ($style)
1549 {
1550 1 {$HTMLStyle = "</h1>"}
1551 2 {$HTMLStyle = "</h2>"}
1552 3 {$HTMLStyle = "</h3>"}
1553 4 {$HTMLStyle = "</h4>"}
1554 Default {$HTMLStyle = ""}
1555 }
1556
1557 $HTMLBody += $HTMLStyle + "</font>"
1558
1559 If($options -band $htmlitalics)
1560 {
1561 $HTMLBody += "</i>"
1562 }
1563
1564 If($options -band $htmlbold)
1565 {
1566 $HTMLBody += "</b>"
1567 }
1568 }
1569 $HTMLBody += "<br />"
1570
1571 out-file -FilePath $Script:FileName1 -Append -InputObject $HTMLBody 4>$Null
1572}
1573#endregion
1574
1575#region HTML table functions
1576#***********************************************************************************************************
1577# AddHTMLTable - Called from FormatHTMLTable function
1578# Created by Ken Avram
1579# modified by Jake Rutski
1580#***********************************************************************************************************
1581Function AddHTMLTable
1582{
1583 Param([string]$fontName="Calibri",
1584 [int]$fontSize=2,
1585 [int]$colCount=0,
1586 [int]$rowCount=0,
1587 [object[]]$rowInfo=@(),
1588 [object[]]$fixedInfo=@())
1589
1590 For($rowidx = $RowIndex;$rowidx -le $rowCount;$rowidx++)
1591 {
1592 $rd = @($rowInfo[$rowidx - 2])
1593 $htmlbody = $htmlbody + "<tr>"
1594 For($columnIndex = 0; $columnIndex -lt $colCount; $columnindex+=2)
1595 {
1596 $fontitalics = $False
1597 $fontbold = $false
1598 $tmp = CheckHTMLColor $rd[$columnIndex+1]
1599
1600 If($fixedInfo.Length -eq 0)
1601 {
1602 $htmlbody += "<td style=""background-color:$($tmp)""><font face='$($fontName)' size='$($fontSize)'>"
1603 }
1604 Else
1605 {
1606 $htmlbody += "<td style=""width:$($fixedInfo[$columnIndex/2]); background-color:$($tmp)""><font face='$($fontName)' size='$($fontSize)'>"
1607 }
1608
1609 If($rd[$columnIndex+1] -band $htmlbold)
1610 {
1611 $htmlbody += "<b>"
1612 }
1613 If($rd[$columnIndex+1] -band $htmlitalics)
1614 {
1615 $htmlbody += "<i>"
1616 }
1617 If($Null -ne $rd[$columnIndex])
1618 {
1619 $cell = $rd[$columnIndex].tostring()
1620 If($cell -eq " " -or $cell.length -eq 0)
1621 {
1622 $htmlbody += " "
1623 }
1624 Else
1625 {
1626 For($i=0;$i -lt $cell.length;$i++)
1627 {
1628 If($cell[$i] -eq " ")
1629 {
1630 $htmlbody += " "
1631 }
1632 If($cell[$i] -ne " ")
1633 {
1634 Break
1635 }
1636 }
1637 $htmlbody += $cell
1638 }
1639 }
1640 Else
1641 {
1642 $htmlbody += " "
1643 }
1644 If($rd[$columnIndex+1] -band $htmlbold)
1645 {
1646 $htmlbody += "</b>"
1647 }
1648 If($rd[$columnIndex+1] -band $htmlitalics)
1649 {
1650 $htmlbody += "</i>"
1651 }
1652 $htmlbody += "</font></td>"
1653 }
1654 $htmlbody += "</tr>"
1655 }
1656 out-file -FilePath $Script:FileName1 -Append -InputObject $HTMLBody 4>$Null
1657}
1658
1659#***********************************************************************************************************
1660# FormatHTMLTable
1661# Created by Ken Avram
1662# modified by Jake Rutski
1663#***********************************************************************************************************
1664
1665<#
1666.Synopsis
1667 Format table for HTML output document
1668.DESCRIPTION
1669 This function formats a table for HTML from an array of strings
1670.PARAMETER noBorder
1671 If set to $true, a table will be generated without a border (border='0')
1672.PARAMETER noHeadCols
1673 This parameter should be used when generating tables without column headers
1674 Set this parameter equal to the number of columns in the table
1675.PARAMETER rowArray
1676 This parameter contains the row data array for the table
1677.PARAMETER columnArray
1678 This parameter contains column header data for the table
1679.PARAMETER fixedWidth
1680 This parameter contains widths for columns in pixel format ("100px") to override auto column widths
1681 The variable should contain a width for each column you wish to override the auto-size setting
1682 For example: $columnWidths = @("100px","110px","120px","130px","140px")
1683
1684.USAGE
1685 FormatHTMLTable <Table Header> <Table Format> <Font Name> <Font Size>
1686
1687.EXAMPLE
1688 FormatHTMLTable "Table Heading" "auto" "Calibri" 3
1689
1690 This example formats a table and writes it out into an html file. All of the parameters are optional
1691 defaults are used if not supplied.
1692
1693 for <Table format>, the default is auto which will autofit the text into the columns and adjust to the longest text in that column. You can also use percentage i.e. 25%
1694 which will take only 25% of the line and will auto word wrap the text to the next line in the column. Also, instead of using a percentage, you can use pixels i.e. 400px.
1695
1696 FormatHTMLTable "Table Heading" "auto" -rowArray $rowData -columnArray $columnData
1697
1698 This example creates an HTML table with a heading of 'Table Heading', auto column spacing, column header data from $columnData and row data from $rowData
1699
1700 FormatHTMLTable "Table Heading" -rowArray $rowData -noHeadCols 3
1701
1702 This example creates an HTML table with a heading of 'Table Heading', auto column spacing, no header, and row data from $rowData
1703
1704 FormatHTMLTable "Table Heading" -rowArray $rowData -fixedWidth $fixedColumns
1705
1706 This example creates an HTML table with a heading of 'Table Heading, no header, row data from $rowData, and fixed columns defined by $fixedColumns
1707
1708.NOTES
1709 In order to use the formatted table it first has to be loaded with data. Examples below will show how to load the table:
1710
1711 First, initialize the table array
1712
1713 $rowdata = @()
1714
1715 Then Load the array. If you are using column headers then load those into the column headers array, otherwise the first line of the table goes into the column headers array
1716 and the second and subsequent lines go into the $rowdata table as shown below:
1717
1718 $columnHeaders = @('Display Name',($htmlsilver -bor $htmlbold),'Status',($htmlsilver -bor $htmlbold),'Startup Type',($htmlsilver -bor $htmlbold))
1719
1720 The first column is the actual name to display, the second are the attributes of the column i.e. color anded with bold or italics. For the anding, parens are required or it will
1721 not format correctly.
1722
1723 This is following by adding rowdata as shown below. As more columns are added the columns will auto adjust to fit the size of the page.
1724
1725 $rowdata = @()
1726 $columnHeaders = @("User Name",($htmlsilver -bor $htmlbold),$UserName,$htmlwhite)
1727 $rowdata += @(,('Save as PDF',($htmlsilver -bor $htmlbold),$PDF.ToString(),$htmlwhite))
1728 $rowdata += @(,('Save as TEXT',($htmlsilver -bor $htmlbold),$TEXT.ToString(),$htmlwhite))
1729 $rowdata += @(,('Save as WORD',($htmlsilver -bor $htmlbold),$MSWORD.ToString(),$htmlwhite))
1730 $rowdata += @(,('Save as HTML',($htmlsilver -bor $htmlbold),$HTML.ToString(),$htmlwhite))
1731 $rowdata += @(,('Add DateTime',($htmlsilver -bor $htmlbold),$AddDateTime.ToString(),$htmlwhite))
1732 $rowdata += @(,('Hardware Inventory',($htmlsilver -bor $htmlbold),$Hardware.ToString(),$htmlwhite))
1733 $rowdata += @(,('Computer Name',($htmlsilver -bor $htmlbold),$ComputerName,$htmlwhite))
1734 $rowdata += @(,('Filename1',($htmlsilver -bor $htmlbold),$Script:FileName1,$htmlwhite))
1735 $rowdata += @(,('OS Detected',($htmlsilver -bor $htmlbold),$Script:RunningOS,$htmlwhite))
1736 $rowdata += @(,('PSUICulture',($htmlsilver -bor $htmlbold),$PSCulture,$htmlwhite))
1737 $rowdata += @(,('PoSH version',($htmlsilver -bor $htmlbold),$Host.Version.ToString(),$htmlwhite))
1738 FormatHTMLTable "Example of Horizontal AutoFitContents HTML Table" -rowArray $rowdata
1739
1740 The 'rowArray' paramater is mandatory to build the table, but it is not set as such in the function - if nothing is passed, the table will be empty.
1741
1742 Colors and Bold/Italics Flags are shown below:
1743
1744 htmlbold
1745 htmlitalics
1746 htmlred
1747 htmlcyan
1748 htmlblue
1749 htmldarkblue
1750 htmllightblue
1751 htmlpurple
1752 htmlyellow
1753 htmllime
1754 htmlmagenta
1755 htmlwhite
1756 htmlsilver
1757 htmlgray
1758 htmlolive
1759 htmlorange
1760 htmlmaroon
1761 htmlgreen
1762 htmlblack
1763
1764#>
1765
1766Function FormatHTMLTable
1767{
1768 Param([string]$tableheader,
1769 [string]$tablewidth="auto",
1770 [string]$fontName="Calibri",
1771 [int]$fontSize=2,
1772 [switch]$noBorder=$false,
1773 [int]$noHeadCols=1,
1774 [object[]]$rowArray=@(),
1775 [object[]]$fixedWidth=@(),
1776 [object[]]$columnArray=@())
1777
1778 $HTMLBody = "<b><font face='" + $fontname + "' size='" + ($fontsize + 1) + "'>" + $tableheader + "</font></b>"
1779
1780 If($columnArray.Length -eq 0)
1781 {
1782 $NumCols = $noHeadCols + 1
1783 } # means we have no column headers, just a table
1784 Else
1785 {
1786 $NumCols = $columnArray.Length
1787 } # need to add one for the color attrib
1788
1789 If($Null -ne $rowArray)
1790 {
1791 $NumRows = $rowArray.length + 1
1792 }
1793 Else
1794 {
1795 $NumRows = 1
1796 }
1797
1798 If($noBorder)
1799 {
1800 $htmlbody += "<table border='0' width='" + $tablewidth + "'>"
1801 }
1802 Else
1803 {
1804 $htmlbody += "<table border='1' width='" + $tablewidth + "'>"
1805 }
1806
1807 If(!($columnArray.Length -eq 0))
1808 {
1809 $htmlbody += "<tr>"
1810
1811 For($columnIndex = 0; $columnIndex -lt $NumCols; $columnindex+=2)
1812 {
1813 $tmp = CheckHTMLColor $columnArray[$columnIndex+1]
1814 If($fixedWidth.Length -eq 0)
1815 {
1816 $htmlbody += "<td style=""background-color:$($tmp)""><font face='$($fontName)' size='$($fontSize)'>"
1817 }
1818 Else
1819 {
1820 $htmlbody += "<td style=""width:$($fixedWidth[$columnIndex/2]); background-color:$($tmp)""><font face='$($fontName)' size='$($fontSize)'>"
1821 }
1822
1823 If($columnArray[$columnIndex+1] -band $htmlbold)
1824 {
1825 $htmlbody += "<b>"
1826 }
1827 If($columnArray[$columnIndex+1] -band $htmlitalics)
1828 {
1829 $htmlbody += "<i>"
1830 }
1831 If($Null -ne $columnArray[$columnIndex])
1832 {
1833 If($columnArray[$columnIndex] -eq " " -or $columnArray[$columnIndex].length -eq 0)
1834 {
1835 $htmlbody += " "
1836 }
1837 Else
1838 {
1839 $found = $false
1840 For($i=0;$i -lt $columnArray[$columnIndex].length;$i+=2)
1841 {
1842 If($columnArray[$columnIndex][$i] -eq " ")
1843 {
1844 $htmlbody += " "
1845 }
1846 If($columnArray[$columnIndex][$i] -ne " ")
1847 {
1848 Break
1849 }
1850 }
1851 $htmlbody += $columnArray[$columnIndex]
1852 }
1853 }
1854 Else
1855 {
1856 $htmlbody += " "
1857 }
1858 If($columnArray[$columnIndex+1] -band $htmlbold)
1859 {
1860 $htmlbody += "</b>"
1861 }
1862 If($columnArray[$columnIndex+1] -band $htmlitalics)
1863 {
1864 $htmlbody += "</i>"
1865 }
1866 $htmlbody += "</font></td>"
1867 }
1868 $htmlbody += "</tr>"
1869 }
1870 $rowindex = 2
1871 If($Null -ne $rowArray)
1872 {
1873 AddHTMLTable $fontName $fontSize -colCount $numCols -rowCount $NumRows -rowInfo $rowArray -fixedInfo $fixedWidth
1874 $rowArray = @()
1875 $htmlbody = "</table>"
1876 }
1877 Else
1878 {
1879 $HTMLBody += "</table>"
1880 }
1881 out-file -FilePath $Script:FileName1 -Append -InputObject $HTMLBody 4>$Null
1882}
1883#endregion
1884
1885#region other HTML functions
1886#***********************************************************************************************************
1887# CheckHTMLColor - Called from AddHTMLTable WriteHTMLLine and FormatHTMLTable
1888#***********************************************************************************************************
1889Function CheckHTMLColor
1890{
1891 Param($hash)
1892
1893 If($hash -band $htmlwhite)
1894 {
1895 Return $htmlwhitemask
1896 }
1897 If($hash -band $htmlred)
1898 {
1899 Return $htmlredmask
1900 }
1901 If($hash -band $htmlcyan)
1902 {
1903 Return $htmlcyanmask
1904 }
1905 If($hash -band $htmlblue)
1906 {
1907 Return $htmlbluemask
1908 }
1909 If($hash -band $htmldarkblue)
1910 {
1911 Return $htmldarkbluemask
1912 }
1913 If($hash -band $htmllightblue)
1914 {
1915 Return $htmllightbluemask
1916 }
1917 If($hash -band $htmlpurple)
1918 {
1919 Return $htmlpurplemask
1920 }
1921 If($hash -band $htmlyellow)
1922 {
1923 Return $htmlyellowmask
1924 }
1925 If($hash -band $htmllime)
1926 {
1927 Return $htmllimemask
1928 }
1929 If($hash -band $htmlmagenta)
1930 {
1931 Return $htmlmagentamask
1932 }
1933 If($hash -band $htmlsilver)
1934 {
1935 Return $htmlsilvermask
1936 }
1937 If($hash -band $htmlgray)
1938 {
1939 Return $htmlgraymask
1940 }
1941 If($hash -band $htmlblack)
1942 {
1943 Return $htmlblackmask
1944 }
1945 If($hash -band $htmlorange)
1946 {
1947 Return $htmlorangemask
1948 }
1949 If($hash -band $htmlmaroon)
1950 {
1951 Return $htmlmaroonmask
1952 }
1953 If($hash -band $htmlgreen)
1954 {
1955 Return $htmlgreenmask
1956 }
1957 If($hash -band $htmlolive)
1958 {
1959 Return $htmlolivemask
1960 }
1961}
1962
1963Function SetupHTML
1964{
1965 Write-Verbose "$(Get-Date): Setting up HTML"
1966 If(!$AddDateTime)
1967 {
1968 [string]$Script:FileName1 = "$($pwdpath)\$($OutputFileName).html"
1969 }
1970 ElseIf($AddDateTime)
1971 {
1972 [string]$Script:FileName1 = "$($pwdpath)\$($OutputFileName)_$(Get-Date -f yyyy-MM-dd_HHmm).html"
1973 }
1974
1975 $htmlhead = "<html><head><meta http-equiv='Content-Language' content='da'><title>" + $Script:Title + "</title></head><body>"
1976 out-file -FilePath $Script:Filename1 -Force -InputObject $HTMLHead 4>$Null
1977}#endregion
1978
1979#region Iain's Word table functions
1980
1981<#
1982.Synopsis
1983 Add a table to a Microsoft Word document
1984.DESCRIPTION
1985 This function adds a table to a Microsoft Word document from either an array of
1986 Hashtables or an array of PSCustomObjects.
1987
1988 Using this function is quicker than setting each table cell individually but can
1989 only utilise the built-in MS Word table autoformats. Individual tables cells can
1990 be altered after the table has been appended to the document (a table reference
1991 is returned).
1992.EXAMPLE
1993 AddWordTable -Hashtable $HashtableArray
1994
1995 This example adds table to the MS Word document, utilising all key/value pairs in
1996 the array of hashtables. Column headers will display the key names as defined.
1997 Note: the columns might not be displayed in the order that they were defined. To
1998 ensure columns are displayed in the required order utilise the -Columns parameter.
1999.EXAMPLE
2000 AddWordTable -Hashtable $HashtableArray -List
2001
2002 This example adds table to the MS Word document, utilising all key/value pairs in
2003 the array of hashtables. No column headers will be added, in a ListView format.
2004 Note: the columns might not be displayed in the order that they were defined. To
2005 ensure columns are displayed in the required order utilise the -Columns parameter.
2006.EXAMPLE
2007 AddWordTable -CustomObject $PSCustomObjectArray
2008
2009 This example adds table to the MS Word document, utilising all note property names
2010 the array of PSCustomObjects. Column headers will display the note property names.
2011 Note: the columns might not be displayed in the order that they were defined. To
2012 ensure columns are displayed in the required order utilise the -Columns parameter.
2013.EXAMPLE
2014 AddWordTable -Hashtable $HashtableArray -Columns FirstName,LastName,EmailAddress
2015
2016 This example adds a table to the MS Word document, but only using the specified
2017 key names: FirstName, LastName and EmailAddress. If other keys are present in the
2018 array of Hashtables they will be ignored.
2019.EXAMPLE
2020 AddWordTable -CustomObject $PSCustomObjectArray -Columns FirstName,LastName,EmailAddress -Headers "First Name","Last Name","Email Address"
2021
2022 This example adds a table to the MS Word document, but only using the specified
2023 PSCustomObject note properties: FirstName, LastName and EmailAddress. If other note
2024 properties are present in the array of PSCustomObjects they will be ignored. The
2025 display names for each specified column header has been overridden to display a
2026 custom header. Note: the order of the header names must match the specified columns.
2027#>
2028
2029Function AddWordTable
2030{
2031 [CmdletBinding()]
2032 Param
2033 (
2034 # Array of Hashtable (including table headers)
2035 [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, ParameterSetName='Hashtable', Position=0)]
2036 [ValidateNotNullOrEmpty()] [System.Collections.Hashtable[]] $Hashtable,
2037 # Array of PSCustomObjects
2038 [Parameter(Mandatory=$True, ValueFromPipelineByPropertyName=$True, ParameterSetName='CustomObject', Position=0)]
2039 [ValidateNotNullOrEmpty()] [PSCustomObject[]] $CustomObject,
2040 # Array of Hashtable key names or PSCustomObject property names to include, in display order.
2041 # If not supplied then all Hashtable keys or all PSCustomObject properties will be displayed.
2042 [Parameter(ValueFromPipelineByPropertyName=$True)] [AllowNull()] [string[]] $Columns = $Null,
2043 # Array of custom table header strings in display order.
2044 [Parameter(ValueFromPipelineByPropertyName=$True)] [AllowNull()] [string[]] $Headers = $Null,
2045 # AutoFit table behavior.
2046 [Parameter(ValueFromPipelineByPropertyName=$True)] [AllowNull()] [int] $AutoFit = -1,
2047 # List view (no headers)
2048 [Switch] $List,
2049 # Grid lines
2050 [Switch] $NoGridLines,
2051 [Switch] $NoInternalGridLines,
2052 # Built-in Word table formatting style constant
2053 # Would recommend only $wdTableFormatContempory for normal usage (possibly $wdTableFormatList5 for List view)
2054 [Parameter(ValueFromPipelineByPropertyName=$True)] [int] $Format = 0
2055 )
2056
2057 Begin
2058 {
2059 Write-Debug ("Using parameter set '{0}'" -f $PSCmdlet.ParameterSetName);
2060 ## Check if -Columns wasn't specified but -Headers were (saves some additional parameter sets!)
2061 If(($Null -eq $Columns) -and ($Null -ne $Headers))
2062 {
2063 Write-Warning "No columns specified and therefore, specified headers will be ignored.";
2064 $Columns = $Null;
2065 }
2066 ElseIf(($Null -ne $Columns) -and ($Null -ne $Headers))
2067 {
2068 ## Check if number of specified -Columns matches number of specified -Headers
2069 If($Columns.Length -ne $Headers.Length)
2070 {
2071 Write-Error "The specified number of columns does not match the specified number of headers.";
2072 }
2073 } ## end elseif
2074 } ## end Begin
2075
2076 Process
2077 {
2078 ## Build the Word table data string to be converted to a range and then a table later.
2079 [System.Text.StringBuilder] $WordRangeString = New-Object System.Text.StringBuilder;
2080
2081 Switch ($PSCmdlet.ParameterSetName)
2082 {
2083 'CustomObject'
2084 {
2085 If($Null -eq $Columns)
2086 {
2087 ## Build the available columns from all availble PSCustomObject note properties
2088 [string[]] $Columns = @();
2089 ## Add each NoteProperty name to the array
2090 ForEach($Property in ($CustomObject | Get-Member -MemberType NoteProperty))
2091 {
2092 $Columns += $Property.Name;
2093 }
2094 }
2095
2096 ## Add the table headers from -Headers or -Columns (except when in -List(view)
2097 If(-not $List)
2098 {
2099 Write-Debug ("$(Get-Date): `t`tBuilding table headers");
2100 If($Null -ne $Headers)
2101 {
2102 [ref] $Null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Headers));
2103 }
2104 Else
2105 {
2106 [ref] $Null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Columns));
2107 }
2108 }
2109
2110 ## Iterate through each PSCustomObject
2111 Write-Debug ("$(Get-Date): `t`tBuilding table rows");
2112 ForEach($Object in $CustomObject)
2113 {
2114 $OrderedValues = @();
2115 ## Add each row item in the specified order
2116 ForEach($Column in $Columns)
2117 {
2118 $OrderedValues += $Object.$Column;
2119 }
2120 ## Use the ordered list to add each column in specified order
2121 [ref] $Null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $OrderedValues));
2122 } ## end foreach
2123 Write-Debug ("$(Get-Date): `t`t`tAdded '{0}' table rows" -f ($CustomObject.Count));
2124 } ## end CustomObject
2125
2126 Default
2127 { ## Hashtable
2128 If($Null -eq $Columns)
2129 {
2130 ## Build the available columns from all available hashtable keys. Hopefully
2131 ## all Hashtables have the same keys (they should for a table).
2132 $Columns = $Hashtable[0].Keys;
2133 }
2134
2135 ## Add the table headers from -Headers or -Columns (except when in -List(view)
2136 If(-not $List)
2137 {
2138 Write-Debug ("$(Get-Date): `t`tBuilding table headers");
2139 If($Null -ne $Headers)
2140 {
2141 [ref] $Null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Headers));
2142 }
2143 Else
2144 {
2145 [ref] $Null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Columns));
2146 }
2147 }
2148
2149 ## Iterate through each Hashtable
2150 Write-Debug ("$(Get-Date): `t`tBuilding table rows");
2151 ForEach($Hash in $Hashtable)
2152 {
2153 $OrderedValues = @();
2154 ## Add each row item in the specified order
2155 ForEach($Column in $Columns)
2156 {
2157 $OrderedValues += $Hash.$Column;
2158 }
2159 ## Use the ordered list to add each column in specified order
2160 [ref] $Null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $OrderedValues));
2161 } ## end foreach
2162
2163 Write-Debug ("$(Get-Date): `t`t`tAdded '{0}' table rows" -f $Hashtable.Count);
2164 } ## end default
2165 } ## end switch
2166
2167 ## Create a MS Word range and set its text to our tab-delimited, concatenated string
2168 Write-Debug ("$(Get-Date): `t`tBuilding table range");
2169 $WordRange = $Script:Doc.Application.Selection.Range;
2170 $WordRange.Text = $WordRangeString.ToString();
2171
2172 ## Create hash table of named arguments to pass to the ConvertToTable method
2173 $ConvertToTableArguments = @{ Separator = [Microsoft.Office.Interop.Word.WdTableFieldSeparator]::wdSeparateByTabs; }
2174
2175 ## Negative built-in styles are not supported by the ConvertToTable method
2176 If($Format -ge 0)
2177 {
2178 $ConvertToTableArguments.Add("Format", $Format);
2179 $ConvertToTableArguments.Add("ApplyBorders", $True);
2180 $ConvertToTableArguments.Add("ApplyShading", $True);
2181 $ConvertToTableArguments.Add("ApplyFont", $True);
2182 $ConvertToTableArguments.Add("ApplyColor", $True);
2183 If(!$List)
2184 {
2185 $ConvertToTableArguments.Add("ApplyHeadingRows", $True);
2186 }
2187 $ConvertToTableArguments.Add("ApplyLastRow", $True);
2188 $ConvertToTableArguments.Add("ApplyFirstColumn", $True);
2189 $ConvertToTableArguments.Add("ApplyLastColumn", $True);
2190 }
2191
2192 ## Invoke ConvertToTable method - with named arguments - to convert Word range to a table
2193 ## See http://msdn.microsoft.com/en-us/library/office/aa171893(v=office.11).aspx
2194 Write-Debug ("$(Get-Date): `t`tConverting range to table");
2195 ## Store the table reference just in case we need to set alternate row coloring
2196 $WordTable = $WordRange.GetType().InvokeMember(
2197 "ConvertToTable", # Method name
2198 [System.Reflection.BindingFlags]::InvokeMethod, # Flags
2199 $Null, # Binder
2200 $WordRange, # Target (self!)
2201 ([Object[]]($ConvertToTableArguments.Values)), ## Named argument values
2202 $Null, # Modifiers
2203 $Null, # Culture
2204 ([String[]]($ConvertToTableArguments.Keys)) ## Named argument names
2205 );
2206
2207 ## Implement grid lines (will wipe out any existing formatting
2208 If($Format -lt 0)
2209 {
2210 Write-Debug ("$(Get-Date): `t`tSetting table format");
2211 $WordTable.Style = $Format;
2212 }
2213
2214 ## Set the table autofit behavior
2215 If($AutoFit -ne -1)
2216 {
2217 $WordTable.AutoFitBehavior($AutoFit);
2218 }
2219
2220 If(!$List)
2221 {
2222 #the next line causes the heading row to flow across page breaks
2223 $WordTable.Rows.First.Headingformat = $wdHeadingFormatTrue;
2224 }
2225
2226 If(!$NoGridLines)
2227 {
2228 $WordTable.Borders.InsideLineStyle = $wdLineStyleSingle;
2229 $WordTable.Borders.OutsideLineStyle = $wdLineStyleSingle;
2230 }
2231 If($NoGridLines)
2232 {
2233 $WordTable.Borders.InsideLineStyle = $wdLineStyleNone;
2234 $WordTable.Borders.OutsideLineStyle = $wdLineStyleNone;
2235 }
2236 If($NoInternalGridLines)
2237 {
2238 $WordTable.Borders.InsideLineStyle = $wdLineStyleNone;
2239 $WordTable.Borders.OutsideLineStyle = $wdLineStyleSingle;
2240 }
2241
2242 Return $WordTable;
2243
2244 } ## end Process
2245}
2246
2247<#
2248.Synopsis
2249 Sets the format of one or more Word table cells
2250.DESCRIPTION
2251 This function sets the format of one or more table cells, either from a collection
2252 of Word COM object cell references, an individual Word COM object cell reference or
2253 a hashtable containing Row and Column information.
2254
2255 The font name, font size, bold, italic , underline and shading values can be used.
2256.EXAMPLE
2257 SetWordCellFormat -Hashtable $Coordinates -Table $TableReference -Bold
2258
2259 This example sets all text to bold that is contained within the $TableReference
2260 Word table, using an array of hashtables. Each hashtable contain a pair of co-
2261 ordinates that is used to select the required cells. Note: the hashtable must
2262 contain the .Row and .Column key names. For example:
2263 @ { Row = 7; Column = 3 } to set the cell at row 7 and column 3 to bold.
2264.EXAMPLE
2265 $RowCollection = $Table.Rows.First.Cells
2266 SetWordCellFormat -Collection $RowCollection -Bold -Size 10
2267
2268 This example sets all text to size 8 and bold for all cells that are contained
2269 within the first row of the table.
2270 Note: the $Table.Rows.First.Cells returns a collection of Word COM cells objects
2271 that are in the first table row.
2272.EXAMPLE
2273 $ColumnCollection = $Table.Columns.Item(2).Cells
2274 SetWordCellFormat -Collection $ColumnCollection -BackgroundColor 255
2275
2276 This example sets the background (shading) of all cells in the table's second
2277 column to red.
2278 Note: the $Table.Columns.Item(2).Cells returns a collection of Word COM cells objects
2279 that are in the table's second column.
2280.EXAMPLE
2281 SetWordCellFormat -Cell $Table.Cell(17,3) -Font "Tahoma" -Color 16711680
2282
2283 This example sets the font to Tahoma and the text color to blue for the cell located
2284 in the table's 17th row and 3rd column.
2285 Note: the $Table.Cell(17,3) returns a single Word COM cells object.
2286#>
2287
2288Function SetWordCellFormat
2289{
2290 [CmdletBinding(DefaultParameterSetName='Collection')]
2291 Param (
2292 # Word COM object cell collection reference
2293 [Parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName='Collection', Position=0)] [ValidateNotNullOrEmpty()] $Collection,
2294 # Word COM object individual cell reference
2295 [Parameter(Mandatory=$true, ParameterSetName='Cell', Position=0)] [ValidateNotNullOrEmpty()] $Cell,
2296 # Hashtable of cell co-ordinates
2297 [Parameter(Mandatory=$true, ParameterSetName='Hashtable', Position=0)] [ValidateNotNullOrEmpty()] [System.Collections.Hashtable[]] $Coordinates,
2298 # Word COM object table reference
2299 [Parameter(Mandatory=$true, ParameterSetName='Hashtable', Position=1)] [ValidateNotNullOrEmpty()] $Table,
2300 # Font name
2301 [Parameter()] [AllowNull()] [string] $Font = $Null,
2302 # Font color
2303 [Parameter()] [AllowNull()] $Color = $Null,
2304 # Font size
2305 [Parameter()] [ValidateNotNullOrEmpty()] [int] $Size = 0,
2306 # Cell background color
2307 [Parameter()] [AllowNull()] $BackgroundColor = $Null,
2308 # Force solid background color
2309 [Switch] $Solid,
2310 [Switch] $Bold,
2311 [Switch] $Italic,
2312 [Switch] $Underline
2313 )
2314
2315 Begin
2316 {
2317 Write-Debug ("Using parameter set '{0}'." -f $PSCmdlet.ParameterSetName);
2318 }
2319
2320 Process
2321 {
2322 Switch ($PSCmdlet.ParameterSetName)
2323 {
2324 'Collection' {
2325 ForEach($Cell in $Collection)
2326 {
2327 If($Null -ne $BackgroundColor) { $Cell.Shading.BackgroundPatternColor = $BackgroundColor; }
2328 If($Bold) { $Cell.Range.Font.Bold = $true; }
2329 If($Italic) { $Cell.Range.Font.Italic = $true; }
2330 If($Underline) { $Cell.Range.Font.Underline = 1; }
2331 If($Null -ne $Font) { $Cell.Range.Font.Name = $Font; }
2332 If($Null -ne $Color) { $Cell.Range.Font.Color = $Color; }
2333 If($Size -ne 0) { $Cell.Range.Font.Size = $Size; }
2334 If($Solid) { $Cell.Shading.Texture = 0; } ## wdTextureNone
2335 } # end foreach
2336 } # end Collection
2337 'Cell'
2338 {
2339 If($Bold) { $Cell.Range.Font.Bold = $true; }
2340 If($Italic) { $Cell.Range.Font.Italic = $true; }
2341 If($Underline) { $Cell.Range.Font.Underline = 1; }
2342 If($Null -ne $Font) { $Cell.Range.Font.Name = $Font; }
2343 If($Null -ne $Color) { $Cell.Range.Font.Color = $Color; }
2344 If($Size -ne 0) { $Cell.Range.Font.Size = $Size; }
2345 If($Null -ne $BackgroundColor) { $Cell.Shading.BackgroundPatternColor = $BackgroundColor; }
2346 If($Solid) { $Cell.Shading.Texture = 0; } ## wdTextureNone
2347 } # end Cell
2348 'Hashtable'
2349 {
2350 ForEach($Coordinate in $Coordinates)
2351 {
2352 $Cell = $Table.Cell($Coordinate.Row, $Coordinate.Column);
2353 If($Bold) { $Cell.Range.Font.Bold = $true; }
2354 If($Italic) { $Cell.Range.Font.Italic = $true; }
2355 If($Underline) { $Cell.Range.Font.Underline = 1; }
2356 If($Null -ne $Font) { $Cell.Range.Font.Name = $Font; }
2357 If($Null -ne $Color) { $Cell.Range.Font.Color = $Color; }
2358 If($Size -ne 0) { $Cell.Range.Font.Size = $Size; }
2359 If($Null -ne $BackgroundColor) { $Cell.Shading.BackgroundPatternColor = $BackgroundColor; }
2360 If($Solid) { $Cell.Shading.Texture = 0; } ## wdTextureNone
2361 }
2362 } # end Hashtable
2363 } # end switch
2364 } # end process
2365}
2366
2367<#
2368.Synopsis
2369 Sets alternate row colors in a Word table
2370.DESCRIPTION
2371 This function sets the format of alternate rows within a Word table using the
2372 specified $BackgroundColor. This function is expensive (in performance terms) as
2373 it recursively sets the format on alternate rows. It would be better to pick one
2374 of the predefined table formats (if one exists)? Obviously the more rows, the
2375 longer it takes :'(
2376
2377 Note: this function is called by the AddWordTable function if an alternate row
2378 format is specified.
2379.EXAMPLE
2380 SetWordTableAlternateRowColor -Table $TableReference -BackgroundColor 255
2381
2382 This example sets every-other table (starting with the first) row and sets the
2383 background color to red (wdColorRed).
2384.EXAMPLE
2385 SetWordTableAlternateRowColor -Table $TableReference -BackgroundColor 39423 -Seed Second
2386
2387 This example sets every other table (starting with the second) row and sets the
2388 background color to light orange (weColorLightOrange).
2389#>
2390
2391Function SetWordTableAlternateRowColor
2392{
2393 [CmdletBinding()]
2394 Param (
2395 # Word COM object table reference
2396 [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] [ValidateNotNullOrEmpty()] $Table,
2397 # Alternate row background color
2398 [Parameter(Mandatory=$true, Position=1)] [ValidateNotNull()] [int] $BackgroundColor,
2399 # Alternate row starting seed
2400 [Parameter(ValueFromPipelineByPropertyName=$true, Position=2)] [ValidateSet('First','Second')] [string] $Seed = 'First'
2401 )
2402
2403 Process
2404 {
2405 $StartDateTime = Get-Date;
2406 Write-Debug ("{0}: `t`tSetting alternate table row colors.." -f $StartDateTime);
2407
2408 ## Determine the row seed (only really need to check for 'Second' and default to 'First' otherwise
2409 If($Seed.ToLower() -eq 'second')
2410 {
2411 $StartRowIndex = 2;
2412 }
2413 Else
2414 {
2415 $StartRowIndex = 1;
2416 }
2417
2418 For($AlternateRowIndex = $StartRowIndex; $AlternateRowIndex -lt $Table.Rows.Count; $AlternateRowIndex += 2)
2419 {
2420 $Table.Rows.Item($AlternateRowIndex).Shading.BackgroundPatternColor = $BackgroundColor;
2421 }
2422
2423 ## I've put verbose calls in here we can see how expensive this functionality actually is.
2424 $EndDateTime = Get-Date;
2425 $ExecutionTime = New-TimeSpan -Start $StartDateTime -End $EndDateTime;
2426 Write-Debug ("{0}: `t`tDone setting alternate row style color in '{1}' seconds" -f $EndDateTime, $ExecutionTime.TotalSeconds);
2427 }
2428}
2429#endregion
2430
2431Function _SetDocumentProperty
2432{
2433 #jeff hicks
2434 Param([object]$Properties,[string]$Name,[string]$Value)
2435 #get the property object
2436 $prop = $properties | ForEach {
2437 $propname=$_.GetType().InvokeMember("Name","GetProperty",$Null,$_,$Null)
2438 If($propname -eq $Name)
2439 {
2440 Return $_
2441 }
2442 } #ForEach
2443
2444 #set the value
2445 $Prop.GetType().InvokeMember("Value","SetProperty",$Null,$prop,$Value)
2446}
2447
2448Function AbortScript
2449{
2450 $Script:Word.quit()
2451 Write-Verbose "$(Get-Date): System Cleanup"
2452 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Script:Word) | Out-Null
2453 If(Test-Path variable:global:word)
2454 {
2455 Remove-Variable -Name word -Scope Global 4>$Null
2456 }
2457 [gc]::collect()
2458 [gc]::WaitForPendingFinalizers()
2459 Write-Verbose "$(Get-Date): Script has been aborted"
2460 $ErrorActionPreference = $SaveEAPreference
2461 Exit
2462}
2463
2464Function FindWordDocumentEnd
2465{
2466 #return focus to main document
2467 $Script:Doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
2468 #move to the end of the current document
2469 $Script:Selection.EndKey($wdStory,$wdMove) | Out-Null
2470}
2471
2472<#
2473.Synopsis
2474 Add a table to a Microsoft Word document
2475.DESCRIPTION
2476 This function adds a table to a Microsoft Word document from either an array of
2477 Hashtables or an array of PSCustomObjects.
2478
2479 Using this function is quicker than setting each table cell individually but can
2480 only utilise the built-in MS Word table autoformats. Individual tables cells can
2481 be altered after the table has been appended to the document (a table reference
2482 is returned).
2483.EXAMPLE
2484 AddWordTable -Hashtable $HashtableArray
2485
2486 This example adds table to the MS Word document, utilising all key/value pairs in
2487 the array of hashtables. Column headers will display the key names as defined.
2488 Note: the columns might not be displayed in the order that they were defined. To
2489 ensure columns are displayed in the required order utilise the -Columns parameter.
2490.EXAMPLE
2491 AddWordTable -Hashtable $HashtableArray -List
2492
2493 This example adds table to the MS Word document, utilising all key/value pairs in
2494 the array of hashtables. No column headers will be added, in a ListView format.
2495 Note: the columns might not be displayed in the order that they were defined. To
2496 ensure columns are displayed in the required order utilise the -Columns parameter.
2497.EXAMPLE
2498 AddWordTable -CustomObject $PSCustomObjectArray
2499
2500 This example adds table to the MS Word document, utilising all note property names
2501 the array of PSCustomObjects. Column headers will display the note property names.
2502 Note: the columns might not be displayed in the order that they were defined. To
2503 ensure columns are displayed in the required order utilise the -Columns parameter.
2504.EXAMPLE
2505 AddWordTable -Hashtable $HashtableArray -Columns FirstName,LastName,EmailAddress
2506
2507 This example adds a table to the MS Word document, but only using the specified
2508 key names: FirstName, LastName and EmailAddress. If other keys are present in the
2509 array of Hashtables they will be ignored.
2510.EXAMPLE
2511 AddWordTable -CustomObject $PSCustomObjectArray -Columns FirstName,LastName,EmailAddress -Headers "First Name","Last Name","Email Address"
2512
2513 This example adds a table to the MS Word document, but only using the specified
2514 PSCustomObject note properties: FirstName, LastName and EmailAddress. If other note
2515 properties are present in the array of PSCustomObjects they will be ignored. The
2516 display names for each specified column header has been overridden to display a
2517 custom header. Note: the order of the header names must match the specified columns.
2518#>
2519Function AddWordTable
2520{
2521 [CmdletBinding()]
2522 Param
2523 (
2524 # Array of Hashtable (including table headers)
2525 [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='Hashtable', Position=0)]
2526 [ValidateNotNullOrEmpty()] [System.Collections.Hashtable[]] $Hashtable,
2527 # Array of PSCustomObjects
2528 [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='CustomObject', Position=0)]
2529 [ValidateNotNullOrEmpty()] [PSCustomObject[]] $CustomObject,
2530 # Array of Hashtable key names or PSCustomObject property names to include, in display order.
2531 # If not supplied then all Hashtable keys or all PSCustomObject properties will be displayed.
2532 [Parameter(ValueFromPipelineByPropertyName=$true)] [AllowNull()] [string[]] $Columns = $null,
2533 # Array of custom table header strings in display order.
2534 [Parameter(ValueFromPipelineByPropertyName=$true)] [AllowNull()] [string[]] $Headers = $null,
2535 # AutoFit table behavior.
2536 [Parameter(ValueFromPipelineByPropertyName=$true)] [AllowNull()] [int] $AutoFit = -1,
2537 # List view (no headers)
2538 [Switch] $List,
2539 # Grid lines
2540 [Switch] $NoGridLines,
2541 # Built-in Word table formatting style constant
2542 # Would recommend only $wdTableFormatContempory for normal usage (possibly $wdTableFormatList5 for List view)
2543 [Parameter(ValueFromPipelineByPropertyName=$true)] [int] $Format = 0
2544 )
2545
2546 Begin
2547 {
2548 Write-Debug ("Using parameter set '{0}'" -f $PSCmdlet.ParameterSetName);
2549 ## Check if -Columns wasn't specified but -Headers were (saves some additional parameter sets!)
2550 If(($Null -eq $Columns) -and ($Null -ne $Headers))
2551 {
2552 Write-Warning "No columns specified and therefore, specified headers will be ignored.";
2553 $Columns = $null;
2554 }
2555 ElseIf(($Null -ne $Columns) -and ($Null -ne $Headers))
2556 {
2557 ## Check if number of specified -Columns matches number of specified -Headers
2558 If($Columns.Length -ne $Headers.Length)
2559 {
2560 Write-Error "The specified number of columns does not match the specified number of headers.";
2561 }
2562 } ## end elseif
2563 } ## end Begin
2564
2565 Process
2566 {
2567 ## Build the Word table data string to be converted to a range and then a table later.
2568 [System.Text.StringBuilder] $WordRangeString = New-Object System.Text.StringBuilder;
2569
2570 Switch ($PSCmdlet.ParameterSetName)
2571 {
2572 'CustomObject'
2573 {
2574 If($Null -eq $Columns)
2575 {
2576 ## Build the available columns from all availble PSCustomObject note properties
2577 [string[]] $Columns = @();
2578 ## Add each NoteProperty name to the array
2579 ForEach($Property in ($CustomObject | Get-Member -MemberType NoteProperty))
2580 {
2581 $Columns += $Property.Name;
2582 }
2583 }
2584
2585 ## Add the table headers from -Headers or -Columns (except when in -List(view)
2586 If(-not $List)
2587 {
2588 Write-Debug ("$(Get-Date): `t`tBuilding table headers");
2589 If($Null -ne $Headers)
2590 {
2591 [ref] $null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Headers));
2592 }
2593 Else
2594 {
2595 [ref] $null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Columns));
2596 }
2597 }
2598
2599 ## Iterate through each PSCustomObject
2600 Write-Debug ("$(Get-Date): `t`tBuilding table rows");
2601 ForEach($Object in $CustomObject)
2602 {
2603 $OrderedValues = @();
2604 ## Add each row item in the specified order
2605 ForEach($Column in $Columns)
2606 {
2607 $OrderedValues += $Object.$Column;
2608 }
2609 ## Use the ordered list to add each column in specified order
2610 [ref] $null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $OrderedValues));
2611 } ## end foreach
2612 Write-Debug ("$(Get-Date): `t`t`tAdded '{0}' table rows" -f ($CustomObject.Count));
2613 } ## end CustomObject
2614
2615 Default
2616 { ## Hashtable
2617 If($Null -eq $Columns)
2618 {
2619 ## Build the available columns from all available hashtable keys. Hopefully
2620 ## all Hashtables have the same keys (they should for a table).
2621 $Columns = $Hashtable[0].Keys;
2622 }
2623
2624 ## Add the table headers from -Headers or -Columns (except when in -List(view)
2625 If(-not $List)
2626 {
2627 Write-Debug ("$(Get-Date): `t`tBuilding table headers");
2628 If($Null -ne $Headers)
2629 {
2630 [ref] $null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Headers));
2631 }
2632 Else
2633 {
2634 [ref] $null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $Columns));
2635 }
2636 }
2637
2638 ## Iterate through each Hashtable
2639 Write-Debug ("$(Get-Date): `t`tBuilding table rows");
2640 ForEach($Hash in $Hashtable)
2641 {
2642 $OrderedValues = @();
2643 ## Add each row item in the specified order
2644 ForEach($Column in $Columns)
2645 {
2646 $OrderedValues += $Hash.$Column;
2647 }
2648 ## Use the ordered list to add each column in specified order
2649 [ref] $null = $WordRangeString.AppendFormat("{0}`n", [string]::Join("`t", $OrderedValues));
2650 } ## end foreach
2651
2652 Write-Debug ("$(Get-Date): `t`t`tAdded '{0}' table rows" -f $Hashtable.Count);
2653 } ## end default
2654 } ## end switch
2655
2656 ## Create a MS Word range and set its text to our tab-delimited, concatenated string
2657 Write-Debug ("$(Get-Date): `t`tBuilding table range");
2658 $WordRange = $Script:Doc.Application.Selection.Range;
2659 $WordRange.Text = $WordRangeString.ToString();
2660
2661 ## Create hash table of named arguments to pass to the ConvertToTable method
2662 $ConvertToTableArguments = @{ Separator = [Microsoft.Office.Interop.Word.WdTableFieldSeparator]::wdSeparateByTabs; }
2663
2664 ## Negative built-in styles are not supported by the ConvertToTable method
2665 If($Format -ge 0)
2666 {
2667 $ConvertToTableArguments.Add("Format", $Format);
2668 $ConvertToTableArguments.Add("ApplyBorders", $true);
2669 $ConvertToTableArguments.Add("ApplyShading", $true);
2670 $ConvertToTableArguments.Add("ApplyFont", $true);
2671 $ConvertToTableArguments.Add("ApplyColor", $true);
2672 If(!$List)
2673 {
2674 $ConvertToTableArguments.Add("ApplyHeadingRows", $true);
2675 }
2676 $ConvertToTableArguments.Add("ApplyLastRow", $true);
2677 $ConvertToTableArguments.Add("ApplyFirstColumn", $true);
2678 $ConvertToTableArguments.Add("ApplyLastColumn", $true);
2679 }
2680
2681 ## Invoke ConvertToTable method - with named arguments - to convert Word range to a table
2682 ## See http://msdn.microsoft.com/en-us/library/office/aa171893(v=office.11).aspx
2683 Write-Debug ("$(Get-Date): `t`tConverting range to table");
2684 ## Store the table reference just in case we need to set alternate row coloring
2685 $WordTable = $WordRange.GetType().InvokeMember(
2686 "ConvertToTable", # Method name
2687 [System.Reflection.BindingFlags]::InvokeMethod, # Flags
2688 $null, # Binder
2689 $WordRange, # Target (self!)
2690 ([Object[]]($ConvertToTableArguments.Values)), ## Named argument values
2691 $null, # Modifiers
2692 $null, # Culture
2693 ([String[]]($ConvertToTableArguments.Keys)) ## Named argument names
2694 );
2695
2696 ## Implement grid lines (will wipe out any existing formatting
2697 If($Format -lt 0)
2698 {
2699 Write-Debug ("$(Get-Date): `t`tSetting table format");
2700 $WordTable.Style = $Format;
2701 }
2702
2703 ## Set the table autofit behavior
2704 If($AutoFit -ne -1)
2705 {
2706 $WordTable.AutoFitBehavior($AutoFit);
2707 }
2708
2709 #the next line causes the heading row to flow across page breaks
2710 $WordTable.Rows.First.Headingformat = $wdHeadingFormatTrue;
2711
2712 If(!$NoGridLines)
2713 {
2714 $WordTable.Borders.InsideLineStyle = $wdLineStyleSingle;
2715 $WordTable.Borders.OutsideLineStyle = $wdLineStyleSingle;
2716 }
2717
2718 Return $WordTable;
2719
2720 } ## end Process
2721}
2722
2723<#
2724.Synopsis
2725 Sets the format of one or more Word table cells
2726.DESCRIPTION
2727 This function sets the format of one or more table cells, either from a collection
2728 of Word COM object cell references, an individual Word COM object cell reference or
2729 a hashtable containing Row and Column information.
2730
2731 The font name, font size, bold, italic , underline and shading values can be used.
2732.EXAMPLE
2733 SetWordCellFormat -Hashtable $Coordinates -Table $TableReference -Bold
2734
2735 This example sets all text to bold that is contained within the $TableReference
2736 Word table, using an array of hashtables. Each hashtable contain a pair of co-
2737 ordinates that is used to select the required cells. Note: the hashtable must
2738 contain the .Row and .Column key names. For example:
2739 @ { Row = 7; Column = 3 } to set the cell at row 7 and column 3 to bold.
2740.EXAMPLE
2741 $RowCollection = $Table.Rows.First.Cells
2742 SetWordCellFormat -Collection $RowCollection -Bold -Size 10
2743
2744 This example sets all text to size 8 and bold for all cells that are contained
2745 within the first row of the table.
2746 Note: the $Table.Rows.First.Cells returns a collection of Word COM cells objects
2747 that are in the first table row.
2748.EXAMPLE
2749 $ColumnCollection = $Table.Columns.Item(2).Cells
2750 SetWordCellFormat -Collection $ColumnCollection -BackgroundColor 255
2751
2752 This example sets the background (shading) of all cells in the table's second
2753 column to red.
2754 Note: the $Table.Columns.Item(2).Cells returns a collection of Word COM cells objects
2755 that are in the table's second column.
2756.EXAMPLE
2757 SetWordCellFormat -Cell $Table.Cell(17,3) -Font "Tahoma" -Color 16711680
2758
2759 This example sets the font to Tahoma and the text color to blue for the cell located
2760 in the table's 17th row and 3rd column.
2761 Note: the $Table.Cell(17,3) returns a single Word COM cells object.
2762#>
2763Function SetWordCellFormat
2764{
2765 [CmdletBinding(DefaultParameterSetName='Collection')]
2766 Param (
2767 # Word COM object cell collection reference
2768 [Parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName='Collection', Position=0)] [ValidateNotNullOrEmpty()] $Collection,
2769 # Word COM object individual cell reference
2770 [Parameter(Mandatory=$true, ParameterSetName='Cell', Position=0)] [ValidateNotNullOrEmpty()] $Cell,
2771 # Hashtable of cell co-ordinates
2772 [Parameter(Mandatory=$true, ParameterSetName='Hashtable', Position=0)] [ValidateNotNullOrEmpty()] [System.Collections.Hashtable[]] $Coordinates,
2773 # Word COM object table reference
2774 [Parameter(Mandatory=$true, ParameterSetName='Hashtable', Position=1)] [ValidateNotNullOrEmpty()] $Table,
2775 # Font name
2776 [Parameter()] [AllowNull()] [string] $Font = $null,
2777 # Font color
2778 [Parameter()] [AllowNull()] $Color = $null,
2779 # Font size
2780 [Parameter()] [ValidateNotNullOrEmpty()] [int] $Size = 0,
2781 # Cell background color
2782 [Parameter()] [AllowNull()] $BackgroundColor = $null,
2783 # Force solid background color
2784 [Switch] $Solid,
2785 [Switch] $Bold,
2786 [Switch] $Italic,
2787 [Switch] $Underline
2788 )
2789
2790 Begin
2791 {
2792 Write-Debug ("Using parameter set '{0}'." -f $PSCmdlet.ParameterSetName);
2793 }
2794
2795 Process
2796 {
2797 Switch ($PSCmdlet.ParameterSetName)
2798 {
2799 'Collection' {
2800 ForEach($Cell in $Collection)
2801 {
2802 If($Null -ne $BackgroundColor) { $Cell.Shading.BackgroundPatternColor = $BackgroundColor; }
2803 If($Bold) { $Cell.Range.Font.Bold = $true; }
2804 If($Italic) { $Cell.Range.Font.Italic = $true; }
2805 If($Underline) { $Cell.Range.Font.Underline = 1; }
2806 If($Null -ne $Font) { $Cell.Range.Font.Name = $Font; }
2807 If($Null -ne $Color) { $Cell.Range.Font.Color = $Color; }
2808 If($Size -ne 0) { $Cell.Range.Font.Size = $Size; }
2809 If($Solid) { $Cell.Shading.Texture = 0; } ## wdTextureNone
2810 } # end foreach
2811 } # end Collection
2812 'Cell'
2813 {
2814 If($Bold) { $Cell.Range.Font.Bold = $true; }
2815 If($Italic) { $Cell.Range.Font.Italic = $true; }
2816 If($Underline) { $Cell.Range.Font.Underline = 1; }
2817 If($Null -ne $Font) { $Cell.Range.Font.Name = $Font; }
2818 If($Null -ne $Color) { $Cell.Range.Font.Color = $Color; }
2819 If($Size -ne 0) { $Cell.Range.Font.Size = $Size; }
2820 If($Null -ne $BackgroundColor) { $Cell.Shading.BackgroundPatternColor = $BackgroundColor; }
2821 If($Solid) { $Cell.Shading.Texture = 0; } ## wdTextureNone
2822 } # end Cell
2823 'Hashtable'
2824 {
2825 ForEach($Coordinate in $Coordinates)
2826 {
2827 $Cell = $Table.Cell($Coordinate.Row, $Coordinate.Column);
2828 If($Bold) { $Cell.Range.Font.Bold = $true; }
2829 If($Italic) { $Cell.Range.Font.Italic = $true; }
2830 If($Underline) { $Cell.Range.Font.Underline = 1; }
2831 If($Null -ne $Font) { $Cell.Range.Font.Name = $Font; }
2832 If($Null -ne $Color) { $Cell.Range.Font.Color = $Color; }
2833 If($Size -ne 0) { $Cell.Range.Font.Size = $Size; }
2834 If($Null -ne $BackgroundColor) { $Cell.Shading.BackgroundPatternColor = $BackgroundColor; }
2835 If($Solid) { $Cell.Shading.Texture = 0; } ## wdTextureNone
2836 }
2837 } # end Hashtable
2838 } # end switch
2839 } # end process
2840}
2841
2842<#
2843.Synopsis
2844 Sets alternate row colors in a Word table
2845.DESCRIPTION
2846 This function sets the format of alternate rows within a Word table using the
2847 specified $BackgroundColor. This function is expensive (in performance terms) as
2848 it recursively sets the format on alternate rows. It would be better to pick one
2849 of the predefined table formats (if one exists)? Obviously the more rows, the
2850 longer it takes :'(
2851
2852 Note: this function is called by the AddWordTable function if an alternate row
2853 format is specified.
2854.EXAMPLE
2855 SetWordTableAlternateRowColor -Table $TableReference -BackgroundColor 255
2856
2857 This example sets every-other table (starting with the first) row and sets the
2858 background color to red (wdColorRed).
2859.EXAMPLE
2860 SetWordTableAlternateRowColor -Table $TableReference -BackgroundColor 39423 -Seed Second
2861
2862 This example sets every other table (starting with the second) row and sets the
2863 background color to light orange (weColorLightOrange).
2864#>
2865Function SetWordTableAlternateRowColor
2866{
2867 [CmdletBinding()]
2868 Param (
2869 # Word COM object table reference
2870 [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] [ValidateNotNullOrEmpty()] $Table,
2871 # Alternate row background color
2872 [Parameter(Mandatory=$true, Position=1)] [ValidateNotNull()] [int] $BackgroundColor,
2873 # Alternate row starting seed
2874 [Parameter(ValueFromPipelineByPropertyName=$true, Position=2)] [ValidateSet('First','Second')] [string] $Seed = 'First'
2875 )
2876
2877 Process
2878 {
2879 $StartDateTime = Get-Date;
2880 Write-Debug ("{0}: `t`tSetting alternate table row colors.." -f $StartDateTime);
2881
2882 ## Determine the row seed (only really need to check for 'Second' and default to 'First' otherwise
2883 If($Seed.ToLower() -eq 'second')
2884 {
2885 $StartRowIndex = 2;
2886 }
2887 Else
2888 {
2889 $StartRowIndex = 1;
2890 }
2891
2892 For($AlternateRowIndex = $StartRowIndex; $AlternateRowIndex -lt $Table.Rows.Count; $AlternateRowIndex += 2)
2893 {
2894 $Table.Rows.Item($AlternateRowIndex).Shading.BackgroundPatternColor = $BackgroundColor;
2895 }
2896
2897 ## I've put verbose calls in here we can see how expensive this functionality actually is.
2898 $EndDateTime = Get-Date;
2899 $ExecutionTime = New-TimeSpan -Start $StartDateTime -End $EndDateTime;
2900 Write-Debug ("{0}: `t`tDone setting alternate row style color in '{1}' seconds" -f $EndDateTime, $ExecutionTime.TotalSeconds);
2901 }
2902}
2903
2904Function validStateProp( [object] $object, [string] $topLevel, [string] $secondLevel )
2905{
2906 #function created 8-jan-2014 by Michael B. Smith
2907 if( $object )
2908 {
2909 If( ( gm -Name $topLevel -InputObject $object ) )
2910 {
2911 If( ( gm -Name $secondLevel -InputObject $object.$topLevel ) )
2912 {
2913 Return $True
2914 }
2915 }
2916 }
2917 Return $False
2918}
2919
2920Function validObject( [object] $object, [string] $topLevel )
2921{
2922 #function created 8-jan-2014 by Michael B. Smith
2923 If( $object )
2924 {
2925 If((gm -Name $topLevel -InputObject $object))
2926 {
2927 Return $True
2928 }
2929 }
2930 Return $False
2931}
2932
2933Function SetupWord
2934{
2935 Write-Verbose "$(Get-Date): Setting up Word"
2936
2937 # Setup word for output
2938 Write-Verbose "$(Get-Date): Create Word comObject."
2939 $Script:Word = New-Object -comobject "Word.Application" -EA 0 4>$Null
2940
2941 If(!$? -or $Null -eq $Script:Word)
2942 {
2943 Write-Warning "The Word object could not be created. You may need to repair your Word installation."
2944 $ErrorActionPreference = $SaveEAPreference
2945 Write-Error "`n`n`t`tThe Word object could not be created. You may need to repair your Word installation.`n`n`t`tScript cannot continue.`n`n"
2946 Exit
2947 }
2948
2949 Write-Verbose "$(Get-Date): Determine Word language value"
2950 If( ( validStateProp $Script:Word Language Value__ ) )
2951 {
2952 [int]$Script:WordLanguageValue = [int]$Script:Word.Language.Value__
2953 }
2954 Else
2955 {
2956 [int]$Script:WordLanguageValue = [int]$Script:Word.Language
2957 }
2958
2959 If(!($Script:WordLanguageValue -gt -1))
2960 {
2961 $ErrorActionPreference = $SaveEAPreference
2962 Write-Error "`n`n`t`tUnable to determine the Word language value.`n`n`t`tScript cannot continue.`n`n"
2963 AbortScript
2964 }
2965 Write-Verbose "$(Get-Date): Word language value is $($Script:WordLanguageValue)"
2966
2967 $Script:WordCultureCode = GetCulture $Script:WordLanguageValue
2968
2969 SetWordHashTable $Script:WordCultureCode
2970
2971 [int]$Script:WordVersion = [int]$Script:Word.Version
2972 If($Script:WordVersion -eq $wdWord2016)
2973 {
2974 $Script:WordProduct = "Word 2016"
2975 }
2976 ElseIf($Script:WordVersion -eq $wdWord2013)
2977 {
2978 $Script:WordProduct = "Word 2013"
2979 }
2980 ElseIf($Script:WordVersion -eq $wdWord2010)
2981 {
2982 $Script:WordProduct = "Word 2010"
2983 }
2984 ElseIf($Script:WordVersion -eq $wdWord2007)
2985 {
2986 $ErrorActionPreference = $SaveEAPreference
2987 Write-Error "`n`n`t`tMicrosoft Word 2007 is no longer supported.`n`n`t`tScript will end.`n`n"
2988 AbortScript
2989 }
2990 Else
2991 {
2992 $ErrorActionPreference = $SaveEAPreference
2993 Write-Error "`n`n`t`tYou are running an untested or unsupported version of Microsoft Word.`n`n`t`tScript will end.`n`n`t`tPlease send info on your version of Word to webster@carlwebster.com`n`n"
2994 AbortScript
2995 }
2996
2997 #only validate CompanyName if the field is blank
2998 If([String]::IsNullOrEmpty($CoName))
2999 {
3000 Write-Verbose "$(Get-Date): Company name is blank. Retrieve company name from registry."
3001 $TmpName = ValidateCompanyName
3002
3003 If([String]::IsNullOrEmpty($TmpName))
3004 {
3005 Write-Warning "`n`n`t`tCompany Name is blank so Cover Page will not show a Company Name."
3006 Write-Warning "`n`t`tCheck HKCU:\Software\Microsoft\Office\Common\UserInfo for Company or CompanyName value."
3007 Write-Warning "`n`t`tYou may want to use the -CompanyName parameter if you need a Company Name on the cover page.`n`n"
3008 }
3009 Else
3010 {
3011 $Script:CoName = $TmpName
3012 Write-Verbose "$(Get-Date): Updated company name to $($Script:CoName)"
3013 }
3014 }
3015
3016 If($Script:WordCultureCode -ne "en-")
3017 {
3018 Write-Verbose "$(Get-Date): Check Default Cover Page for $($WordCultureCode)"
3019 [bool]$CPChanged = $False
3020 Switch ($Script:WordCultureCode)
3021 {
3022 'ca-' {
3023 If($CoverPage -eq "Sideline")
3024 {
3025 $CoverPage = "LÃnia lateral"
3026 $CPChanged = $True
3027 }
3028 }
3029
3030 'da-' {
3031 If($CoverPage -eq "Sideline")
3032 {
3033 $CoverPage = "Sidelinje"
3034 $CPChanged = $True
3035 }
3036 }
3037
3038 'de-' {
3039 If($CoverPage -eq "Sideline")
3040 {
3041 $CoverPage = "Randlinie"
3042 $CPChanged = $True
3043 }
3044 }
3045
3046 'es-' {
3047 If($CoverPage -eq "Sideline")
3048 {
3049 $CoverPage = "LÃnea lateral"
3050 $CPChanged = $True
3051 }
3052 }
3053
3054 'fi-' {
3055 If($CoverPage -eq "Sideline")
3056 {
3057 $CoverPage = "Sivussa"
3058 $CPChanged = $True
3059 }
3060 }
3061
3062 'fr-' {
3063 If($CoverPage -eq "Sideline")
3064 {
3065 If($Script:WordVersion -eq $wdWord2013 -or $Script:WordVersion -eq $wdWord2016)
3066 {
3067 $CoverPage = "Lignes latérales"
3068 $CPChanged = $True
3069 }
3070 Else
3071 {
3072 $CoverPage = "Ligne latérale"
3073 $CPChanged = $True
3074 }
3075 }
3076 }
3077
3078 'nb-' {
3079 If($CoverPage -eq "Sideline")
3080 {
3081 $CoverPage = "Sidelinje"
3082 $CPChanged = $True
3083 }
3084 }
3085
3086 'nl-' {
3087 If($CoverPage -eq "Sideline")
3088 {
3089 $CoverPage = "Terzijde"
3090 $CPChanged = $True
3091 }
3092 }
3093
3094 'pt-' {
3095 If($CoverPage -eq "Sideline")
3096 {
3097 $CoverPage = "Linha Lateral"
3098 $CPChanged = $True
3099 }
3100 }
3101
3102 'sv-' {
3103 If($CoverPage -eq "Sideline")
3104 {
3105 $CoverPage = "Sidlinje"
3106 $CPChanged = $True
3107 }
3108 }
3109 }
3110
3111 If($CPChanged)
3112 {
3113 Write-Verbose "$(Get-Date): Changed Default Cover Page from Sideline to $($CoverPage)"
3114 }
3115 }
3116
3117 Write-Verbose "$(Get-Date): Validate cover page $($CoverPage) for culture code $($Script:WordCultureCode)"
3118 [bool]$ValidCP = $False
3119
3120 $ValidCP = ValidateCoverPage $Script:WordVersion $CoverPage $Script:WordCultureCode
3121
3122 If(!$ValidCP)
3123 {
3124 $ErrorActionPreference = $SaveEAPreference
3125 Write-Verbose "$(Get-Date): Word language value $($Script:WordLanguageValue)"
3126 Write-Verbose "$(Get-Date): Culture code $($Script:WordCultureCode)"
3127 Write-Error "`n`n`t`tFor $($Script:WordProduct), $($CoverPage) is not a valid Cover Page option.`n`n`t`tScript cannot continue.`n`n"
3128 AbortScript
3129 }
3130
3131 ShowScriptOptions
3132
3133 $Script:Word.Visible = $False
3134
3135 #http://jdhitsolutions.com/blog/2012/05/san-diego-2012-powershell-deep-dive-slides-and-demos/
3136 #using Jeff's Demo-WordReport.ps1 file for examples
3137 Write-Verbose "$(Get-Date): Load Word Templates"
3138
3139 [bool]$Script:CoverPagesExist = $False
3140 [bool]$BuildingBlocksExist = $False
3141
3142 $Script:Word.Templates.LoadBuildingBlocks()
3143 #word 2010/2013
3144 $BuildingBlocksCollection = $Script:Word.Templates | Where {$_.name -eq "Built-In Building Blocks.dotx"}
3145
3146 Write-Verbose "$(Get-Date): Attempt to load cover page $($CoverPage)"
3147 $part = $Null
3148
3149 $BuildingBlocksCollection |
3150 ForEach{
3151 If ($_.BuildingBlockEntries.Item($CoverPage).Name -eq $CoverPage)
3152 {
3153 $BuildingBlocks = $_
3154 }
3155 }
3156
3157 If($Null -ne $BuildingBlocks)
3158 {
3159 $BuildingBlocksExist = $True
3160
3161 Try
3162 {
3163 $part = $BuildingBlocks.BuildingBlockEntries.Item($CoverPage)
3164 }
3165
3166 Catch
3167 {
3168 $part = $Null
3169 }
3170
3171 If($Null -ne $part)
3172 {
3173 $Script:CoverPagesExist = $True
3174 }
3175 }
3176
3177 If(!$Script:CoverPagesExist)
3178 {
3179 Write-Verbose "$(Get-Date): Cover Pages are not installed or the Cover Page $($CoverPage) does not exist."
3180 Write-Warning "Cover Pages are not installed or the Cover Page $($CoverPage) does not exist."
3181 Write-Warning "This report will not have a Cover Page."
3182 }
3183
3184 Write-Verbose "$(Get-Date): Create empty word doc"
3185 $Script:Doc = $Script:Word.Documents.Add()
3186 If($Null -eq $Script:Doc)
3187 {
3188 Write-Verbose "$(Get-Date): "
3189 $ErrorActionPreference = $SaveEAPreference
3190 Write-Error "`n`n`t`tAn empty Word document could not be created.`n`n`t`tScript cannot continue.`n`n"
3191 AbortScript
3192 }
3193
3194 $Script:Selection = $Script:Word.Selection
3195 If($Null -eq $Script:Selection)
3196 {
3197 Write-Verbose "$(Get-Date): "
3198 $ErrorActionPreference = $SaveEAPreference
3199 Write-Error "`n`n`t`tAn unknown error happened selecting the entire Word document for default formatting options.`n`n`t`tScript cannot continue.`n`n"
3200 AbortScript
3201 }
3202
3203 #set Default tab stops to 1/2 inch (this line is not from Jeff Hicks)
3204 #36 = .50"
3205 $Script:Word.ActiveDocument.DefaultTabStop = 36
3206
3207 #Disable Spell and Grammar Check to resolve issue and improve performance (from Pat Coughlin)
3208 Write-Verbose "$(Get-Date): Disable grammar and spell checking"
3209 #bug reported 1-Apr-2014 by Tim Mangan
3210 #save current options first before turning them off
3211 $Script:CurrentGrammarOption = $Script:Word.Options.CheckGrammarAsYouType
3212 $Script:CurrentSpellingOption = $Script:Word.Options.CheckSpellingAsYouType
3213 $Script:Word.Options.CheckGrammarAsYouType = $False
3214 $Script:Word.Options.CheckSpellingAsYouType = $False
3215
3216 If($BuildingBlocksExist)
3217 {
3218 #insert new page, getting ready for table of contents
3219 Write-Verbose "$(Get-Date): Insert new page, getting ready for table of contents"
3220 $part.Insert($Script:Selection.Range,$True) | Out-Null
3221 $Script:Selection.InsertNewPage()
3222
3223 #table of contents
3224 Write-Verbose "$(Get-Date): Table of Contents - $($myHash.Word_TableOfContents)"
3225 $toc = $BuildingBlocks.BuildingBlockEntries.Item($myHash.Word_TableOfContents)
3226 If($Null -eq $toc)
3227 {
3228 Write-Verbose "$(Get-Date): "
3229 Write-Verbose "$(Get-Date): Table of Content - $($myHash.Word_TableOfContents) could not be retrieved."
3230 Write-Warning "This report will not have a Table of Contents."
3231 }
3232 Else
3233 {
3234 $toc.insert($Script:Selection.Range,$True) | Out-Null
3235 }
3236 }
3237 Else
3238 {
3239 Write-Verbose "$(Get-Date): Table of Contents are not installed."
3240 Write-Warning "Table of Contents are not installed so this report will not have a Table of Contents."
3241 }
3242
3243 #set the footer
3244 Write-Verbose "$(Get-Date): Set the footer"
3245 [string]$footertext = "Report created by $username"
3246
3247 #get the footer
3248 Write-Verbose "$(Get-Date): Get the footer and format font"
3249 $Script:Doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekPrimaryFooter
3250 #get the footer and format font
3251 $footers = $Script:Doc.Sections.Last.Footers
3252 ForEach ($footer in $footers)
3253 {
3254 If($footer.exists)
3255 {
3256 $footer.range.Font.name = "Calibri"
3257 $footer.range.Font.size = 8
3258 $footer.range.Font.Italic = $True
3259 $footer.range.Font.Bold = $True
3260 }
3261 } #end ForEach
3262 Write-Verbose "$(Get-Date): Footer text"
3263 $Script:Selection.HeaderFooter.Range.Text = $footerText
3264
3265 #add page numbering
3266 Write-Verbose "$(Get-Date): Add page numbering"
3267 $Script:Selection.HeaderFooter.PageNumbers.Add($wdAlignPageNumberRight) | Out-Null
3268
3269 FindWordDocumentEnd
3270 Write-Verbose "$(Get-Date):"
3271 #end of Jeff Hicks
3272}
3273
3274Function UpdateDocumentProperties
3275{
3276 Param([string]$AbstractTitle, [string]$SubjectTitle)
3277 #Update document properties
3278 If($MSWORD -or $PDF)
3279 {
3280 If($Script:CoverPagesExist)
3281 {
3282 Write-Verbose "$(Get-Date): Set Cover Page Properties"
3283 _SetDocumentProperty $Script:Doc.BuiltInDocumentProperties "Company" $Script:CoName
3284 _SetDocumentProperty $Script:Doc.BuiltInDocumentProperties "Title" $Script:title
3285 _SetDocumentProperty $Script:Doc.BuiltInDocumentProperties "Author" $username
3286
3287 _SetDocumentProperty $Script:Doc.BuiltInDocumentProperties "Subject" $SubjectTitle
3288
3289 #Get the Coverpage XML part
3290 $cp = $Script:Doc.CustomXMLParts | Where {$_.NamespaceURI -match "coverPageProps$"}
3291
3292 #get the abstract XML part
3293 $ab = $cp.documentelement.ChildNodes | Where {$_.basename -eq "Abstract"}
3294
3295 #set the text
3296 If([String]::IsNullOrEmpty($Script:CoName))
3297 {
3298 [string]$abstract = $AbstractTitle
3299 }
3300 Else
3301 {
3302 [string]$abstract = "$($AbstractTitle) for $Script:CoName"
3303 }
3304
3305 $ab.Text = $abstract
3306
3307 $ab = $cp.documentelement.ChildNodes | Where {$_.basename -eq "PublishDate"}
3308 #set the text
3309 [string]$abstract = (Get-Date -Format d).ToString()
3310 $ab.Text = $abstract
3311
3312 Write-Verbose "$(Get-Date): Update the Table of Contents"
3313 #update the Table of Contents
3314 $Script:Doc.TablesOfContents.item(1).Update()
3315 $cp = $Null
3316 $ab = $Null
3317 $abstract = $Null
3318 }
3319 }
3320}
3321
3322Function SaveandCloseDocumentandShutdownWord
3323{
3324 #bug fix 1-Apr-2014
3325 #reset Grammar and Spelling options back to their original settings
3326 $Script:Word.Options.CheckGrammarAsYouType = $Script:CurrentGrammarOption
3327 $Script:Word.Options.CheckSpellingAsYouType = $Script:CurrentSpellingOption
3328
3329 Write-Verbose "$(Get-Date): Save and Close document and Shutdown Word"
3330 If($Script:WordVersion -eq $wdWord2010)
3331 {
3332 #the $saveFormat below passes StrictMode 2
3333 #I found this at the following two links
3334 #http://blogs.technet.com/b/bshukla/archive/2011/09/27/3347395.aspx
3335 #http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat(v=office.14).aspx
3336 If($PDF)
3337 {
3338 Write-Verbose "$(Get-Date): Saving as DOCX file first before saving to PDF"
3339 }
3340 Else
3341 {
3342 Write-Verbose "$(Get-Date): Saving DOCX file"
3343 }
3344 If($AddDateTime)
3345 {
3346 $Script:FileName1 += "_$(Get-Date -f yyyy-MM-dd_HHmm).docx"
3347 If($PDF)
3348 {
3349 $Script:FileName2 += "_$(Get-Date -f yyyy-MM-dd_HHmm).pdf"
3350 }
3351 }
3352 Write-Verbose "$(Get-Date): Running $($Script:WordProduct) and detected operating system $($Script:RunningOS)"
3353 $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatDocumentDefault")
3354 $Script:Doc.SaveAs([REF]$Script:FileName1, [ref]$SaveFormat)
3355 If($PDF)
3356 {
3357 Write-Verbose "$(Get-Date): Now saving as PDF"
3358 $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatPDF")
3359 $Script:Doc.SaveAs([REF]$Script:FileName2, [ref]$saveFormat)
3360 }
3361 }
3362 ElseIf($Script:WordVersion -eq $wdWord2013 -or $Script:WordVersion -eq $wdWord2016)
3363 {
3364 If($PDF)
3365 {
3366 Write-Verbose "$(Get-Date): Saving as DOCX file first before saving to PDF"
3367 }
3368 Else
3369 {
3370 Write-Verbose "$(Get-Date): Saving DOCX file"
3371 }
3372 If($AddDateTime)
3373 {
3374 $Script:FileName1 += "_$(Get-Date -f yyyy-MM-dd_HHmm).docx"
3375 If($PDF)
3376 {
3377 $Script:FileName2 += "_$(Get-Date -f yyyy-MM-dd_HHmm).pdf"
3378 }
3379 }
3380 Write-Verbose "$(Get-Date): Running $($Script:WordProduct) and detected operating system $($Script:RunningOS)"
3381 $Script:Doc.SaveAs2([REF]$Script:FileName1, [ref]$wdFormatDocumentDefault)
3382 If($PDF)
3383 {
3384 Write-Verbose "$(Get-Date): Now saving as PDF"
3385 $Script:Doc.SaveAs([REF]$Script:FileName2, [ref]$wdFormatPDF)
3386 }
3387 }
3388
3389 Write-Verbose "$(Get-Date): Closing Word"
3390 $Script:Doc.Close()
3391 $Script:Word.Quit()
3392 If($PDF)
3393 {
3394 [int]$cnt = 0
3395 While(Test-Path $Script:FileName1)
3396 {
3397 $cnt++
3398 If($cnt -gt 1)
3399 {
3400 Write-Verbose "$(Get-Date): Waiting another 10 seconds to allow Word to fully close (try # $($cnt))"
3401 Start-Sleep -Seconds 10
3402 $Script:Word.Quit()
3403 If($cnt -gt 2)
3404 {
3405 #kill the winword process
3406
3407 #find out our session (usually "1" except on TS/RDC or Citrix)
3408 $SessionID = (Get-Process -PID $PID).SessionId
3409
3410 #Find out if winword is running in our session
3411 $wordprocess = ((Get-Process 'WinWord' -ea 0)|?{$_.SessionId -eq $SessionID}).Id
3412 If($wordprocess -gt 0)
3413 {
3414 Write-Verbose "$(Get-Date): Attempting to stop WinWord process # $($wordprocess)"
3415 Stop-Process $wordprocess -EA 0
3416 }
3417 }
3418 }
3419 Write-Verbose "$(Get-Date): Attempting to delete $($Script:FileName1) since only $($Script:FileName2) is needed (try # $($cnt))"
3420 Remove-Item $Script:FileName1 -EA 0 4>$Null
3421 }
3422 }
3423 Write-Verbose "$(Get-Date): System Cleanup"
3424 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Script:Word) | Out-Null
3425 If(Test-Path variable:global:word)
3426 {
3427 Remove-Variable -Name word -Scope Global 4>$Null
3428 }
3429 $SaveFormat = $Null
3430 [gc]::collect()
3431 [gc]::WaitForPendingFinalizers()
3432
3433 #is the winword process still running? kill it
3434
3435 #find out our session (usually "1" except on TS/RDC or Citrix)
3436 $SessionID = (Get-Process -PID $PID).SessionId
3437
3438 #Find out if winword is running in our session
3439 $wordprocess = $Null
3440 $wordprocess = ((Get-Process 'WinWord' -ea 0)|?{$_.SessionId -eq $SessionID}).Id
3441 If($null -ne $wordprocess -and $wordprocess -gt 0)
3442 {
3443 Write-Verbose "$(Get-Date): WinWord process is still running. Attempting to stop WinWord process # $($wordprocess)"
3444 Stop-Process $wordprocess -EA 0
3445 }
3446}
3447
3448Function SaveandCloseTextDocument
3449{
3450 If($AddDateTime)
3451 {
3452 $Script:FileName1 += "_$(Get-Date -f yyyy-MM-dd_HHmm).txt"
3453 }
3454
3455 Write-Output $Global:Output | Out-File $Script:Filename1 4>$Null
3456}
3457
3458Function SaveandCloseHTMLDocument
3459{
3460 Out-File -FilePath $Script:FileName1 -Append -InputObject "<p></p></body></html>" 4>$Null
3461}
3462
3463Function SetFileName1andFileName2
3464{
3465 Param([string]$OutputFileName)
3466
3467 If($Folder -eq "")
3468 {
3469 $pwdpath = $pwd.Path
3470 }
3471 Else
3472 {
3473 $pwdpath = $Folder
3474 }
3475
3476 If($pwdpath.EndsWith("\"))
3477 {
3478 #remove the trailing \
3479 $pwdpath = $pwdpath.SubString(0, ($pwdpath.Length - 1))
3480 }
3481
3482 #set $filename1 and $filename2 with no file extension
3483 If($AddDateTime)
3484 {
3485 [string]$Script:FileName1 = "$($pwdpath)\$($OutputFileName)"
3486 If($PDF)
3487 {
3488 [string]$Script:FileName2 = "$($pwdpath)\$($OutputFileName)"
3489 }
3490 }
3491
3492 If($MSWord -or $PDF)
3493 {
3494 CheckWordPreReq
3495
3496 If(!$AddDateTime)
3497 {
3498 [string]$Script:FileName1 = "$($pwdpath)\$($OutputFileName).docx"
3499 If($PDF)
3500 {
3501 [string]$Script:FileName2 = "$($pwdpath)\$($OutputFileName).pdf"
3502 }
3503 }
3504
3505 SetupWord
3506 }
3507 ElseIf($Text)
3508 {
3509 If(!$AddDateTime)
3510 {
3511 [string]$Script:FileName1 = "$($pwdpath)\$($OutputFileName).txt"
3512 }
3513 ShowScriptOptions
3514 }
3515 ElseIf($HTML)
3516 {
3517 SetupHTML
3518 ShowScriptOptions
3519 }
3520}
3521
3522Function GetIPv4ScopeData_WordPDF
3523{
3524 #put each scope on a new page
3525 $selection.InsertNewPage()
3526 Write-Verbose "$(Get-Date): `tGetting IPv4 scope data for scope $($IPv4Scope.Name)"
3527 WriteWordLine $xStartLevel 0 "Scope [$($IPv4Scope.ScopeId)] $($IPv4Scope.Name)"
3528 WriteWordLine ($xStartLevel + 1) 0 "Address Pool"
3529 $TableRange = $doc.Application.Selection.Range
3530 [int]$Columns = 2
3531 [int]$Rows = 5
3532 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
3533 $table.Style = $myHash.Word_TableGrid
3534 $table.Borders.InsideLineStyle = $wdLineStyleNone
3535 $table.Borders.OutsideLineStyle = $wdLineStyleNone
3536 $Table.Cell(1,1).Range.Text = "Start IP Address"
3537 $Table.Cell(1,2).Range.Text = $IPv4Scope.StartRange.ToString()
3538 $Table.Cell(2,1).Range.Text = "End IP Address"
3539 $Table.Cell(2,2).Range.Text = $IPv4Scope.EndRange.ToString()
3540 $Table.Cell(3,1).Range.Text = "Subnet Mask"
3541 $Table.Cell(3,2).Range.Text = $IPv4Scope.SubnetMask.ToString()
3542 $Table.Cell(4,1).Range.Text = "Lease duration"
3543 If($IPv4Scope.LeaseDuration -eq "00:00:00")
3544 {
3545 $Table.Cell(4,2).Range.Text = "Unlimited"
3546 }
3547 Else
3548 {
3549 $Str = [string]::format("{0} days, {1} hours, {2} minutes", `
3550 $IPv4Scope.LeaseDuration.Days, `
3551 $IPv4Scope.LeaseDuration.Hours, `
3552 $IPv4Scope.LeaseDuration.Minutes)
3553
3554 $Table.Cell(4,2).Range.Text = $Str
3555 }
3556 $Table.Cell(5,1).Range.Text = "Description"
3557 $Table.Cell(5,2).Range.Text = $IPv4Scope.Description
3558
3559 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
3560 $table.AutoFitBehavior($wdAutoFitContent)
3561
3562 #return focus back to document
3563 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
3564
3565 #move to the end of the current document
3566 $selection.EndKey($wdStory,$wdMove) | Out-Null
3567 $TableRange = $Null
3568 $Table = $Null
3569
3570 If($IncludeLeases)
3571 {
3572 Write-Verbose "$(Get-Date): `t`tGetting leases"
3573
3574 WriteWordLine ($xStartLevel + 1) 0 "Address Leases"
3575 $Leases = Get-DHCPServerV4Lease -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object IPAddress
3576 If($? -and $Null -ne $Leases)
3577 {
3578 $TableRange = $doc.Application.Selection.Range
3579 [int]$Columns = 2
3580 If($Leases -is [array])
3581 {
3582 [int]$Rows = ($Leases.Count * 11) - 1
3583 #subtract the very last row used for spacing
3584 }
3585 Else
3586 {
3587 [int]$Rows = 10
3588 }
3589 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
3590 $table.Style = $myHash.Word_TableGrid
3591 $table.Borders.InsideLineStyle = $wdLineStyleNone
3592 $table.Borders.OutsideLineStyle = $wdLineStyleNone
3593 [int]$xRow = 0
3594 ForEach($Lease in $Leases)
3595 {
3596 Write-Verbose "$(Get-Date): `t`t`tProcessing lease $($Lease.IPAddress)"
3597 If($Null -ne $Lease.LeaseExpiryTime)
3598 {
3599 $LeaseStr = [string]::format("{0} days, {1} hours, {2} minutes", `
3600 $Lease.LeaseExpiryTime.Day, `
3601 $Lease.LeaseExpiryTime.Hour, `
3602 $Lease.LeaseExpiryTime.Minute)
3603 }
3604 Else
3605 {
3606 $LeaseStr = ""
3607 }
3608
3609 If($Null -ne $Lease.ProbationEnds)
3610 {
3611 $ProbationStr = [string]::format("{0} days, {1} hours, {2} minutes", `
3612 $Lease.ProbationEnds.Day, `
3613 $Lease.ProbationEnds.Hour, `
3614 $Lease.ProbationEnds.Minute)
3615 }
3616 Else
3617 {
3618 $ProbationStr = ""
3619 }
3620
3621 $xRow++
3622 $Table.Cell($xRow,1).Range.Text = "Client IP address"
3623 $Table.Cell($xRow,2).Range.Text = $Lease.IPAddress.ToString()
3624
3625 $xRow++
3626 $Table.Cell($xRow,1).Range.Text = "Name"
3627 $Table.Cell($xRow,2).Range.Text = $Lease.HostName
3628
3629 $xRow++
3630 $Table.Cell($xRow,1).Range.Text = "Lease Expiration"
3631 If([string]::IsNullOrEmpty($Lease.LeaseExpiryTime))
3632 {
3633 If($Lease.AddressState -eq "ActiveReservation")
3634 {
3635 $Table.Cell($xRow,2).Range.Text = "Reservation (active)"
3636 }
3637 Else
3638 {
3639 $Table.Cell($xRow,2).Range.Text = "Reservation (inactive)"
3640 }
3641 }
3642 Else
3643 {
3644 $Table.Cell($xRow,2).Range.Text = $LeaseStr
3645 }
3646
3647 $xRow++
3648 $Table.Cell($xRow,1).Range.Text = "Type"
3649 $Table.Cell($xRow,2).Range.Text = $Lease.ClientType
3650
3651 $xRow++
3652 $Table.Cell($xRow,1).Range.Text = "Unique ID"
3653 $Table.Cell($xRow,2).Range.Text = $Lease.ClientID
3654
3655 $xRow++
3656 $Table.Cell($xRow,1).Range.Text = "Description"
3657 $Table.Cell($xRow,2).Range.Text = $Lease.Description
3658
3659 $xRow++
3660 $Table.Cell($xRow,1).Range.Text = "Network Access Protection"
3661 $Table.Cell($xRow,2).Range.Text = $Lease.NapStatus
3662
3663 $xRow++
3664 $Table.Cell($xRow,1).Range.Text = "Probation Expiration"
3665
3666 If([string]::IsNullOrEmpty($Lease.ProbationEnds))
3667 {
3668 $Table.Cell($xRow,2).Range.Text = "N/A"
3669 }
3670 Else
3671 {
3672 $Table.Cell($xRow,2).Range.Text = $ProbationStr
3673 }
3674
3675 $xRow++
3676 $Table.Cell($xRow,1).Range.Text = "Filter"
3677
3678 $Filters | % { $index = $Null }{ if( $_.MacAddress -eq $Lease.ClientID ) { $index = $_ } }
3679 If($Null -ne $Index)
3680 {
3681 $Table.Cell($xRow,2).Range.Text = $Index.List
3682 }
3683 Else
3684 {
3685 $Table.Cell($xRow,2).Range.Text = "<None>"
3686 }
3687
3688 $xRow++
3689 $Table.Cell($xRow,1).Range.Text = "Policy"
3690
3691 If([string]::IsNullOrEmpty($Lease.PolicyName))
3692 {
3693 $Table.Cell($xRow,2).Range.Text = "<None>"
3694 }
3695 Else
3696 {
3697 $Table.Cell($xRow,2).Range.Text = $Lease.PolicyName
3698 }
3699
3700 #skip a row for spacing
3701 $xRow++
3702 }
3703 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
3704 $table.AutoFitBehavior($wdAutoFitContent)
3705
3706 #return focus back to document
3707 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
3708
3709 #move to the end of the current document
3710 $selection.EndKey($wdStory,$wdMove) | Out-Null
3711 $TableRange = $Null
3712 $Table = $Null
3713 }
3714 ElseIf(!$?)
3715 {
3716 WriteWordLine 0 0 "Error retrieving leases for scope $IPv4Scope.ScopeId"
3717 }
3718 Else
3719 {
3720 WriteWordLine 0 0 "<None>"
3721 }
3722 $Leases = $Null
3723 }
3724
3725 Write-Verbose "$(Get-Date): `t`tGetting exclusions"
3726 WriteWordLine ($xStartLevel + 1) 0 "Exclusions"
3727 $Exclusions = Get-DHCPServerV4ExclusionRange -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object StartRange
3728 If($? -and $Null -ne $Exclusions)
3729 {
3730 $TableRange = $doc.Application.Selection.Range
3731 [int]$Columns = 2
3732 If($Exclusions -is [array])
3733 {
3734 [int]$Rows = $Exclusions.Count + 1
3735 }
3736 Else
3737 {
3738 [int]$Rows = 2
3739 }
3740 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
3741 $table.Style = $myHash.Word_TableGrid
3742 $table.Borders.InsideLineStyle = $wdLineStyleNone
3743 $table.Borders.OutsideLineStyle = $wdLineStyleNone
3744 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
3745 $Table.Cell(1,1).Range.Font.Bold = $True
3746 $Table.Cell(1,1).Range.Text = "Start IP Address"
3747 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
3748 $Table.Cell(1,2).Range.Font.Bold = $True
3749 $Table.Cell(1,2).Range.Text = "End IP Address"
3750 [int]$xRow = 1
3751 ForEach($Exclusion in $Exclusions)
3752 {
3753 $xRow++
3754 $Table.Cell($xRow,1).Range.Text = $Exclusion.StartRange
3755 $Table.Cell($xRow,2).Range.Text = $Exclusion.EndRange
3756 }
3757 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
3758 $table.AutoFitBehavior($wdAutoFitContent)
3759
3760 #return focus back to document
3761 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
3762
3763 #move to the end of the current document
3764 $selection.EndKey($wdStory,$wdMove) | Out-Null
3765 $TableRange = $Null
3766 $Table = $Null
3767 }
3768 ElseIf(!$?)
3769 {
3770 WriteWordLine 0 0 "Error retrieving exclusions for scope $IPv4Scope.ScopeId"
3771 }
3772 Else
3773 {
3774 WriteWordLine 0 1 "<None>"
3775 }
3776 $Exclusions = $Null
3777
3778 Write-Verbose "$(Get-Date): `t`tGetting reservations"
3779 WriteWordLine ($xStartLevel + 1) 0 "Reservations"
3780 $Reservations = Get-DHCPServerV4Reservation -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object IPAddress
3781 If($? -and $Null -ne $Reservations)
3782 {
3783 ForEach($Reservation in $Reservations)
3784 {
3785 Write-Verbose "$(Get-Date): `t`t`tProcessing reservation $($Reservation.Name)"
3786 $TableRange = $doc.Application.Selection.Range
3787 [int]$Columns = 2
3788 If([string]::IsNullOrEmpty($Reservation.Description))
3789 {
3790 [int]$Rows = 4
3791 }
3792 Else
3793 {
3794 [int]$Rows = 5
3795 }
3796 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
3797 $table.Style = $myHash.Word_TableGrid
3798 $table.Borders.InsideLineStyle = $wdLineStyleNone
3799 $table.Borders.OutsideLineStyle = $wdLineStyleNone
3800 $Table.Cell(1,1).Range.Text = "Reservation name"
3801 $Table.Cell(1,2).Range.Text = $Reservation.Name
3802 $Table.Cell(2,1).Range.Text = "IP address"
3803 $Table.Cell(2,2).Range.Text = $Reservation.IPAddress.ToString()
3804 $Table.Cell(3,1).Range.Text = "MAC address"
3805 $Table.Cell(3,2).Range.Text = $Reservation.ClientId
3806 $Table.Cell(4,1).Range.Text = "Supported types"
3807 $Table.Cell(4,2).Range.Text = $Reservation.Type
3808 If(![string]::IsNullOrEmpty($Reservation.Description))
3809 {
3810 $Table.Cell(5,1).Range.Text = "Description"
3811 $Table.Cell(5,2).Range.Text = $Reservation.Description
3812 }
3813 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
3814 $table.AutoFitBehavior($wdAutoFitContent)
3815
3816 #return focus back to document
3817 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
3818
3819 #move to the end of the current document
3820 $selection.EndKey($wdStory,$wdMove) | Out-Null
3821 $TableRange = $Null
3822 $Table = $Null
3823
3824 Write-Verbose "$(Get-Date): `t`t`tGetting DNS settings"
3825 $DNSSettings = Get-DHCPServerV4DnsSetting -ComputerName $DHCPServerName -IPAddress $Reservation.IPAddress -EA 0
3826 If($? -and $Null -ne $DNSSettings)
3827 {
3828 GetDNSSettings $DNSSettings "A"
3829 }
3830 Else
3831 {
3832 WriteWordLine 0 0 "Error retrieving DNS Settings for reserved IP address $Reservation.IPAddress"
3833 }
3834 $DNSSettings = $Null
3835 WriteWordLine 0 0 ""
3836 }
3837 }
3838 ElseIf(!$?)
3839 {
3840 WriteWordLine 0 0 "Error retrieving reservations for scope $IPv4Scope.ScopeId"
3841 }
3842 Else
3843 {
3844 WriteWordLine 0 1 "<None>"
3845 }
3846 $Reservations = $Null
3847
3848 Write-Verbose "$(Get-Date): `t`tGetting scope options"
3849 WriteWordLine ($xStartLevel + 1) 0 "Scope Options"
3850 $ScopeOptions = Get-DHCPServerV4OptionValue -All -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object OptionId
3851
3852 If($? -and $Null -ne $ScopeOptions)
3853 {
3854 $TableRange = $doc.Application.Selection.Range
3855 [int]$Columns = 2
3856 If($ScopeOptions -is [array])
3857 {
3858 [int]$Rows = (($ScopeOptions.Count * 5) - 5) - 1
3859 #subtract option 51
3860 #subtract the very last row used for spacing
3861 }
3862 Else
3863 {
3864 [int]$Rows = 4
3865 }
3866 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
3867 $table.Style = $myHash.Word_TableGrid
3868 $table.Borders.InsideLineStyle = $wdLineStyleNone
3869 $table.Borders.OutsideLineStyle = $wdLineStyleNone
3870 [int]$xRow = 0
3871 ForEach($ScopeOption in $ScopeOptions)
3872 {
3873 If($ScopeOption.OptionId -ne 51)
3874 {
3875 Write-Verbose "$(Get-Date): `t`t`tProcessing option name $($ScopeOption.Name)"
3876 $xRow++
3877 $Table.Cell($xRow,1).Range.Text = "Option Name"
3878 $Table.Cell($xRow,2).Range.Text = "$($ScopeOption.OptionId.ToString("00000")) $($ScopeOption.Name)"
3879
3880 $xRow++
3881 $Table.Cell($xRow,1).Range.Text = "Vendor"
3882 If([string]::IsNullOrEmpty($ScopeOption.VendorClass))
3883 {
3884 $Table.Cell($xRow,2).Range.Text = "Standard"
3885 }
3886 Else
3887 {
3888 $Table.Cell($xRow,2).Range.Text = $ScopeOption.VendorClass
3889 }
3890
3891 $xRow++
3892 $Table.Cell($xRow,1).Range.Text = "Value"
3893 $Table.Cell($xRow,2).Range.Text = "$($ScopeOption.Value)"
3894
3895 $xRow++
3896 $Table.Cell($xRow,1).Range.Text = "Policy Name"
3897
3898 If([string]::IsNullOrEmpty($ScopeOption.PolicyName))
3899 {
3900 $Table.Cell($xRow,2).Range.Text = "<None>"
3901 }
3902 Else
3903 {
3904 $Table.Cell($xRow,2).Range.Text = $ScopeOption.PolicyName
3905 }
3906
3907 #for spacing
3908 $xRow++
3909 }
3910 }
3911 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
3912 $table.AutoFitBehavior($wdAutoFitContent)
3913
3914 #return focus back to document
3915 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
3916
3917 #move to the end of the current document
3918 $selection.EndKey($wdStory,$wdMove) | Out-Null
3919 $TableRange = $Null
3920 $Table = $Null
3921 }
3922 ElseIf(!$?)
3923 {
3924 WriteWordLine 0 0 "Error retrieving scope options for $IPv4Scope.ScopeId"
3925 }
3926 Else
3927 {
3928 WriteWordLine 0 1 "<None>"
3929 }
3930 $ScopeOptions = $Null
3931
3932 Write-Verbose "$(Get-Date): `t`tGetting policies"
3933 WriteWordLine ($xStartLevel + 1) 0 "Policies"
3934 $ScopePolicies = Get-DHCPServerV4Policy -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object ProcessingOrder
3935
3936 If($? -and $Null -ne $ScopePolicies)
3937 {
3938 $TableRange = $doc.Application.Selection.Range
3939 [int]$Columns = 2
3940 If($ScopePolicies -is [array])
3941 {
3942 [int]$Rows = $ScopePolicies.Count * 6
3943 }
3944 Else
3945 {
3946 [int]$Rows = 6
3947 }
3948 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
3949 $table.Style = $myHash.Word_TableGrid
3950 $table.Borders.InsideLineStyle = $wdLineStyleNone
3951 $table.Borders.OutsideLineStyle = $wdLineStyleNone
3952 [int]$xRow = 0
3953 ForEach($Policy in $ScopePolicies)
3954 {
3955 Write-Verbose "$(Get-Date): `t`t`tProcessing policy name $($Policy.Name)"
3956 $xRow++
3957 $Table.Cell($xRow,1).Range.Text = "Policy Name"
3958 $Table.Cell($xRow,2).Range.Text = $Policy.Name
3959
3960 $xRow++
3961 $Table.Cell($xRow,1).Range.Text = "Description"
3962 $Table.Cell($xRow,2).Range.Text = $Policy.Description
3963
3964 $xRow++
3965 $Table.Cell($xRow,1).Range.Text = "Processing Order"
3966 $Table.Cell($xRow,2).Range.Text = $Policy.ProcessingOrder.ToString()
3967
3968 $xRow++
3969 $Table.Cell($xRow,1).Range.Text = "Level"
3970 $Table.Cell($xRow,2).Range.Text = "Scope"
3971
3972 $xRow++
3973 $Table.Cell($xRow,1).Range.Text = "Address Range"
3974
3975 $IPRange = Get-DHCPServerV4PolicyIPRange -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -Name $Policy.Name -EA 0
3976
3977 If($? -and $Null -ne $IPRange)
3978 {
3979 $Table.Cell($xRow,2).Range.Text = "$($IPRange.StartRange) - $($IPRange.EndRange)"
3980 }
3981 Else
3982 {
3983 $Table.Cell($xRow,2).Range.Text = "<None>"
3984 }
3985
3986 $xRow++
3987 $Table.Cell($xRow,1).Range.Text = "State"
3988 If($Policy.Enabled)
3989 {
3990 $Table.Cell($xRow,2).Range.Text = "Enabled"
3991 }
3992 Else
3993 {
3994 $Table.Cell($xRow,2).Range.Text = "Disabled"
3995 }
3996 }
3997 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
3998 $table.AutoFitBehavior($wdAutoFitContent)
3999
4000 #return focus back to document
4001 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
4002
4003 #move to the end of the current document
4004 $selection.EndKey($wdStory,$wdMove) | Out-Null
4005 $TableRange = $Null
4006 $Table = $Null
4007 }
4008 ElseIf(!$?)
4009 {
4010 WriteWordLine 0 0 "Error retrieving scope policies"
4011 }
4012 Else
4013 {
4014 WriteWordLine 0 1 "<None>"
4015 }
4016 $ScopePolicies = $Null
4017
4018 Write-Verbose "$(Get-Date): `t`tGetting DNS"
4019 WriteWordLine ($xStartLevel + 1) 0 "DNS"
4020 $DNSSettings = Get-DHCPServerV4DnsSetting -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0
4021 If($? -and $Null -ne $DNSSettings)
4022 {
4023 GetDNSSettings $DNSSettings "A"
4024 }
4025 Else
4026 {
4027 WriteWordLine 0 0 "Error retrieving DNS Settings for scope $($IPv4Scope.ScopeId)"
4028 }
4029 $DNSSettings = $Null
4030
4031 #next tab is Network Access Protection but I can't find anything that gives me that info
4032
4033 #failover
4034 Write-Verbose "$(Get-Date): `t`tGetting failover"
4035 WriteWordLine ($xStartLevel + 1) 0 "Failover"
4036 $Failovers = Get-DHCPServerV4Failover -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0
4037
4038 If($? -and $Null -ne $Failovers)
4039 {
4040 $TableRange = $doc.Application.Selection.Range
4041 [int]$Columns = 2
4042 If($Failovers -is [array])
4043 {
4044 [int]$Rows = ($Failovers.Count * 10) - 1
4045 #subtract the very last row used for spacing
4046 }
4047 Else
4048 {
4049 [int]$Rows = 9
4050 }
4051 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
4052 $table.Style = $myHash.Word_TableGrid
4053 $table.Borders.InsideLineStyle = $wdLineStyleNone
4054 $table.Borders.OutsideLineStyle = $wdLineStyleNone
4055 [int]$xRow = 0
4056 ForEach($Failover in $Failovers)
4057 {
4058 Write-Verbose "$(Get-Date): `t`tProcessing failover $($Failover.Name)"
4059 $xRow++
4060 $Table.Cell($xRow,1).Range.Text = "Relationship name"
4061 $Table.Cell($xRow,2).Range.Text = $Failover.Name
4062
4063 $xRow++
4064 $Table.Cell($xRow,1).Range.Text = "Partner Server"
4065 $Table.Cell($xRow,2).Range.Text = $Failover.PartnerServer
4066
4067 $xRow++
4068 $Table.Cell($xRow,1).Range.Text = "Mode"
4069 $Table.Cell($xRow,2).Range.Text = $Failover.Mode
4070
4071 If($Null -ne $Failover.MaxClientLeadTime)
4072 {
4073 $MaxLeadStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4074 $Failover.MaxClientLeadTime.Day, `
4075 $Failover.MaxClientLeadTime.Hour, `
4076 $Failover.MaxClientLeadTime.Minute)
4077 }
4078 Else
4079 {
4080 $MaxLeadStr = ""
4081 }
4082
4083 $xRow++
4084 $Table.Cell($xRow,1).Range.Text = "Max Client Lead Time"
4085 $Table.Cell($xRow,2).Range.Text = $MaxLeadStr
4086
4087 If($Null -ne $Failover.StateSwitchInterval)
4088 {
4089 $SwitchStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4090 $Failover.StateSwitchInterval.Day, `
4091 $Failover.StateSwitchInterval.Hour, `
4092 $Failover.StateSwitchInterval.Minute)
4093 }
4094 Else
4095 {
4096 $SwitchStr = "Disabled"
4097 }
4098
4099 $xRow++
4100 $Table.Cell($xRow,1).Range.Text = "State Switchover Interval"
4101 $Table.Cell($xRow,2).Range.Text = $SwitchStr
4102
4103 Switch($Failover.State)
4104 {
4105 "NoState"
4106 {
4107 $xRow++
4108 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4109 $Table.Cell($xRow,2).Range.Text = "No State"
4110 $xRow++
4111 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4112 $Table.Cell($xRow,2).Range.Text = "No State"
4113 }
4114 "Normal"
4115 {
4116 $xRow++
4117 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4118 $Table.Cell($xRow,2).Range.Text = "Normal"
4119 $xRow++
4120 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4121 $Table.Cell($xRow,2).Range.Text = "Normal"
4122 }
4123 "Init"
4124 {
4125 $xRow++
4126 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4127 $Table.Cell($xRow,2).Range.Text = "Initializing"
4128 $xRow++
4129 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4130 $Table.Cell($xRow,2).Range.Text = "Initializing"
4131 }
4132 "CommunicationInterrupted"
4133 {
4134 $xRow++
4135 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4136 $Table.Cell($xRow,2).Range.Text = "Communication Interrupted"
4137 $xRow++
4138 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4139 $Table.Cell($xRow,2).Range.Text = "Communication Interrupted"
4140 }
4141 "PartnerDown"
4142 {
4143 $xRow++
4144 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4145 $Table.Cell($xRow,2).Range.Text = "Normal"
4146 $xRow++
4147 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4148 $Table.Cell($xRow,2).Range.Text = "Down"
4149 }
4150 "PotentialConflict"
4151 {
4152 $xRow++
4153 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4154 $Table.Cell($xRow,2).Range.Text = "Potential Conflict"
4155 $xRow++
4156 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4157 $Table.Cell($xRow,2).Range.Text = "Potential Conflict"
4158 }
4159 "Startup"
4160 {
4161 $xRow++
4162 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4163 $Table.Cell($xRow,2).Range.Text = "Starting Up"
4164 $xRow++
4165 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4166 $Table.Cell($xRow,2).Range.Text = "Starting Up"
4167 }
4168 "ResolutionInterrupted"
4169 {
4170 $xRow++
4171 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4172 $Table.Cell($xRow,2).Range.Text = "Resolution Interrupted"
4173 $xRow++
4174 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4175 $Table.Cell($xRow,2).Range.Text = "Resolution Interrupted"
4176 }
4177 "ConflictDone"
4178 {
4179 $xRow++
4180 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4181 $Table.Cell($xRow,2).Range.Text = "Conflict Done"
4182 $xRow++
4183 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4184 $Table.Cell($xRow,2).Range.Text = "Conflict Done"
4185 }
4186 "Recover"
4187 {
4188 $xRow++
4189 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4190 $Table.Cell($xRow,2).Range.Text = "Recover"
4191 $xRow++
4192 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4193 $Table.Cell($xRow,2).Range.Text = "Recover"
4194 }
4195 "RecoverWait"
4196 {
4197 $xRow++
4198 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4199 $Table.Cell($xRow,2).Range.Text = "Recover Wait"
4200 $xRow++
4201 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4202 $Table.Cell($xRow,2).Range.Text = "Recover Wait"
4203 }
4204 "RecoverDone"
4205 {
4206 $xRow++
4207 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4208 $Table.Cell($xRow,2).Range.Text = "Recover Done"
4209 $xRow++
4210 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4211 $Table.Cell($xRow,2).Range.Text = "Recover Done"
4212 }
4213 Default
4214 {
4215 $xRow++
4216 $Table.Cell($xRow,1).Range.Text = "State of this Server"
4217 $Table.Cell($xRow,2).Range.Text = "Unable to determine server state"
4218 $xRow++
4219 $Table.Cell($xRow,1).Range.Text = "State of Partner Server"
4220 $Table.Cell($xRow,2).Range.Text = "Unable to determine server state"
4221 }
4222 }
4223
4224 If($Failover.Mode -eq "LoadBalance")
4225 {
4226 $xRow++
4227 $Table.Cell($xRow,1).Range.Text = "Local server"
4228 $Table.Cell($xRow,2).Range.Text = "$($Failover.LoadBalancePercent)%"
4229
4230 $xRow++
4231 $Table.Cell($xRow,1).Range.Text = "Partner Server"
4232 $tmp = (100 - $($Failover.LoadBalancePercent))
4233 $Table.Cell($xRow,2).Range.Text = "$($tmp)%"
4234 $tmp = $Null
4235 }
4236 Else
4237 {
4238 $xRow++
4239 $Table.Cell($xRow,1).Range.Text = "Role of this server"
4240 $Table.Cell($xRow,2).Range.Text = $Failover.ServerRole
4241
4242 $xRow++
4243 $Table.Cell($xRow,1).Range.Text = "Addresses reserved for standby server"
4244 $Table.Cell($xRow,2).Range.Text = "$($Failover.ReservePercent)%"
4245 }
4246
4247 #skip a row for spacing
4248 $xRow++
4249 }
4250 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
4251 $table.AutoFitBehavior($wdAutoFitContent)
4252
4253 #return focus back to document
4254 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
4255
4256 #move to the end of the current document
4257 $selection.EndKey($wdStory,$wdMove) | Out-Null
4258 $TableRange = $Null
4259 $Table = $Null
4260 }
4261 Else
4262 {
4263 WriteWordLine 0 1 "<None>"
4264 }
4265 $Failovers = $Null
4266
4267 Write-Verbose "$(Get-Date): `t`tGetting Scope statistics"
4268 WriteWordLine ($xStartLevel + 1) 0 "Statistics"
4269
4270 $Statistics = Get-DHCPServerV4ScopeStatistics -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0
4271
4272 If($? -and $Null -ne $Statistics)
4273 {
4274 GetShortStatistics $Statistics
4275 }
4276 ElseIf(!$?)
4277 {
4278 WriteWordLine 0 0 "Error retrieving scope statistics"
4279 }
4280 Else
4281 {
4282 WriteWordLine 0 1 "<None>"
4283 }
4284 $Statistics = $Null
4285}
4286
4287Function GetIPv4ScopeData_HTML
4288{
4289
4290}
4291
4292Function GetIPv4ScopeData_Text
4293{
4294 Write-Verbose "$(Get-Date):`tGetting IPv4 scope data for scope $($IPv4Scope.Name)"
4295 Line 0 "Scope [$($IPv4Scope.ScopeId)] $($IPv4Scope.Name)"
4296 Line 1 "Address Pool:"
4297 Line 2 "Start IP Address`t: " $IPv4Scope.StartRange
4298 Line 2 "End IP Address`t`t: " $IPv4Scope.EndRange
4299 Line 2 "Subnet Mask`t`t: " $IPv4Scope.SubnetMask
4300 Line 2 "Lease duration`t`t: " -NoNewLine
4301 If($IPv4Scope.LeaseDuration -eq "00:00:00")
4302 {
4303 Line 0 "Unlimited"
4304 }
4305 Else
4306 {
4307 $Str = [string]::format("{0} days, {1} hours, {2} minutes", `
4308 $IPv4Scope.LeaseDuration.Days, `
4309 $IPv4Scope.LeaseDuration.Hours, `
4310 $IPv4Scope.LeaseDuration.Minutes)
4311
4312 Line 0 $Str
4313 }
4314 If(![string]::IsNullOrEmpty($IPv4Scope.Description))
4315 {
4316 Line 2 "Description`t`t: " $IPv4Scope.Description
4317 }
4318
4319 If($IncludeLeases)
4320 {
4321 Write-Verbose "$(Get-Date):`t`tGetting leases"
4322
4323 Line 1 "Address Leases:"
4324 $Leases = Get-DHCPServerV4Lease -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object IPAddress
4325 If($? -and $Null -ne $Leases)
4326 {
4327 ForEach($Lease in $Leases)
4328 {
4329 Write-Verbose "$(Get-Date):`t`t`tProcessing lease $($Lease.IPAddress)"
4330 If($Null -ne $Lease.LeaseExpiryTime)
4331 {
4332 $LeaseStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4333 $Lease.LeaseExpiryTime.Day, `
4334 $Lease.LeaseExpiryTime.Hour, `
4335 $Lease.LeaseExpiryTime.Minute)
4336 }
4337 Else
4338 {
4339 $LeaseStr = ""
4340 }
4341
4342 If($Null -ne $Lease.ProbationEnds)
4343 {
4344 $ProbationStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4345 $Lease.ProbationEnds.Day, `
4346 $Lease.ProbationEnds.Hour, `
4347 $Lease.ProbationEnds.Minute)
4348 }
4349 Else
4350 {
4351 $ProbationStr = ""
4352 }
4353
4354 Line 2 "Name: " $Lease.HostName
4355 Line 2 "Client IP address`t`t: " $Lease.IPAddress
4356 Line 2 "Lease Expiration`t`t: " -NoNewLine
4357 If([string]::IsNullOrEmpty($Lease.LeaseExpiryTime))
4358 {
4359 If($Lease.AddressState -eq "ActiveReservation")
4360 {
4361 Line 0 "Reservation (active)"
4362 }
4363 Else
4364 {
4365 Line 0 "Reservation (inactive)"
4366 }
4367 }
4368 Else
4369 {
4370 Line 0 $LeaseStr
4371 }
4372 Line 2 "Type`t`t`t`t: " $Lease.ClientType
4373 Line 2 "Unique ID`t`t`t: " $Lease.ClientID
4374 If(![string]::IsNullOrEmpty($Lease.Description))
4375 {
4376 Line 2 "Description`t`t`t: " $Lease.Description
4377 }
4378 Line 2 "Network Access Protection`t: " $Lease.NapStatus
4379 Line 2 "Probation Expiration`t`t: " -NoNewLine
4380 If([string]::IsNullOrEmpty($Lease.ProbationEnds))
4381 {
4382 Line 0 "N/A"
4383 }
4384 Else
4385 {
4386 Line 0 $ProbationStr
4387 }
4388 Line 2 "Filter`t`t`t`t: " -NoNewLine
4389
4390 $Filters | % { $index = $Null }{ if( $_.MacAddress -eq $Lease.ClientID ) { $index = $_ } }
4391 If($Null -ne $Index)
4392 {
4393 Line 0 $Index.List
4394 }
4395 Else
4396 {
4397 Line 0 "<None>"
4398 }
4399 Line 2 "Policy`t`t`t`t: " -NoNewLine
4400 If([string]::IsNullOrEmpty($Lease.PolicyName))
4401 {
4402 Line 0 "<None>"
4403 }
4404 Else
4405 {
4406 Line 0 $Lease.PolicyName
4407 }
4408
4409 #skip a row for spacing
4410 Line 0 ""
4411 }
4412 }
4413 ElseIf(!$?)
4414 {
4415 Line 0 "Error retrieving leases for scope $IPv4Scope.ScopeId"
4416 }
4417 Else
4418 {
4419 Line 2 "<None>"
4420 }
4421 $Leases = $Null
4422 }
4423
4424 Write-Verbose "$(Get-Date):`t`tGetting exclusions"
4425 Line 1 "Exclusions:"
4426 $Exclusions = Get-DHCPServerV4ExclusionRange -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object StartRange
4427 If($? -and $Null -ne $Exclusions)
4428 {
4429 ForEach($Exclusion in $Exclusions)
4430 {
4431 Line 2 "Start IP Address`t: " $Exclusion.StartRange
4432 Line 2 "End IP Address`t`t: " $Exclusion.EndRange
4433 Line 0 ""
4434 }
4435 }
4436 ElseIf(!$?)
4437 {
4438 Line 0 "Error retrieving exclusions for scope $IPv4Scope.ScopeId"
4439 }
4440 Else
4441 {
4442 Line 2 "<None>"
4443 }
4444 $Exclusions = $Null
4445
4446 Write-Verbose "$(Get-Date):`t`tGetting reservations"
4447 Line 1 "Reservations:"
4448 $Reservations = Get-DHCPServerV4Reservation -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object IPAddress
4449 If($? -and $Null -ne $Reservations)
4450 {
4451 ForEach($Reservation in $Reservations)
4452 {
4453 Write-Verbose "$(Get-Date):`t`t`tProcessing reservation $($Reservation.Name)"
4454 Line 2 "Reservation name`t: " $Reservation.Name
4455 Line 2 "IP address`t`t: " $Reservation.IPAddress
4456 Line 2 "MAC address`t`t: " $Reservation.ClientId
4457 Line 2 "Supported types`t`t: " $Reservation.Type
4458 If(![string]::IsNullOrEmpty($Reservation.Description))
4459 {
4460 Line 2 "Description`t`t: " $Reservation.Description
4461 }
4462
4463 Write-Verbose "$(Get-Date):`t`t`tGetting DNS settings"
4464 $DNSSettings = Get-DHCPServerV4DnsSetting -ComputerName $DHCPServerName -IPAddress $Reservation.IPAddress -EA 0
4465 If($? -and $Null -ne $DNSSettings)
4466 {
4467 GetDNSSettings $DNSSettings "A"
4468 }
4469 Else
4470 {
4471 Line 0 "Error retrieving DNS Settings for reserved IP address $Reservation.IPAddress"
4472 }
4473 $DNSSettings = $Null
4474 Line 0 ""
4475 }
4476 }
4477 ElseIf(!$?)
4478 {
4479 Line 0 "Error retrieving reservations for scope $IPv4Scope.ScopeId"
4480 }
4481 Else
4482 {
4483 Line 2 "<None>"
4484 }
4485 $Reservations = $Null
4486
4487 Write-Verbose "$(Get-Date):`t`tGetting scope options"
4488 Line 1 "Scope Options:"
4489 $ScopeOptions = Get-DHCPServerV4OptionValue -All -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object OptionId
4490
4491 If($? -and $Null -ne $ScopeOptions)
4492 {
4493 ForEach($ScopeOption in $ScopeOptions)
4494 {
4495 If($ScopeOption.OptionId -ne 51)
4496 {
4497 Write-Verbose "$(Get-Date):`t`t`tProcessing option name $($ScopeOption.Name)"
4498 Line 2 "Option Name`t: $($ScopeOption.OptionId.ToString("00000")) $($ScopeOption.Name)"
4499 Line 2 "Vendor`t`t: " -NoNewLine
4500 If([string]::IsNullOrEmpty($ScopeOption.VendorClass))
4501 {
4502 Line 0 "Standard"
4503 }
4504 Else
4505 {
4506 Line 0 $ScopeOption.VendorClass
4507 }
4508 Line 2 "Value`t`t: $($ScopeOption.Value)"
4509 Line 2 "Policy Name`t: " -NoNewLine
4510
4511 If([string]::IsNullOrEmpty($ScopeOption.PolicyName))
4512 {
4513 Line 0 "<None>"
4514 }
4515 Else
4516 {
4517 Line 0 $ScopeOption.PolicyName
4518 }
4519
4520 #for spacing
4521 Line 0 ""
4522 }
4523 }
4524 }
4525 ElseIf(!$?)
4526 {
4527 Line 0 "Error retrieving scope options for $IPv4Scope.ScopeId"
4528 }
4529 Else
4530 {
4531 Line 2 "<None>"
4532 }
4533 $ScopeOptions = $Null
4534
4535 Write-Verbose "$(Get-Date):`t`tGetting policies"
4536 Line 1 "Policies:"
4537 $ScopePolicies = Get-DHCPServerV4Policy -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0 | Sort-Object ProcessingOrder
4538
4539 If($? -and $Null -ne $ScopePolicies)
4540 {
4541 ForEach($Policy in $ScopePolicies)
4542 {
4543 Write-Verbose "$(Get-Date):`t`t`tProcessing policy name $($Policy.Name)"
4544 Line 2 "Policy Name`t`t: " $Policy.Name
4545 If(![string]::IsNullOrEmpty($Policy.Description))
4546 {
4547 Line 2 "Description`t`t: " $Policy.Description
4548 }
4549 Line 2 "Processing Order`t: " $Policy.ProcessingOrder
4550 Line 2 "Level`t`t`t: Scope"
4551 Line 2 "Address Range`t: " -NoNewLine
4552
4553 $IPRange = Get-DHCPServerV4PolicyIPRange -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -Name $Policy.Name -EA 0
4554
4555 If($? -and $Null -ne $IPRange)
4556 {
4557 Line 0 "$($IPRange.StartRange) - $($IPRange.EndRange)"
4558 }
4559 Else
4560 {
4561 Line 0 "<None>"
4562 }
4563 Line 2 "State`t`t`t: " -NoNewLine
4564 If($Policy.Enabled)
4565 {
4566 Line 0 "Enabled"
4567 }
4568 Else
4569 {
4570 Line 0 "Disabled"
4571 }
4572 }
4573 }
4574 ElseIf(!$?)
4575 {
4576 Line 0 "Error retrieving scope policies"
4577 }
4578 Else
4579 {
4580 Line 2 "<None>"
4581 }
4582 $ScopePolicies = $Null
4583
4584 Write-Verbose "$(Get-Date):`t`tGetting DNS"
4585 Line 1 "DNS:"
4586 $DNSSettings = Get-DHCPServerV4DnsSetting -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0
4587 If($? -and $Null -ne $DNSSettings)
4588 {
4589 GetDNSSettings $DNSSettings "A"
4590 }
4591 Else
4592 {
4593 Line 0 "Error retrieving DNS Settings for scope $($IPv4Scope.ScopeId)"
4594 }
4595 $DNSSettings = $Null
4596
4597 #next tab is Network Access Protection but I can't find anything that gives me that info
4598
4599 #failover
4600 Write-Verbose "$(Get-Date):`t`tGetting Failover"
4601 Line 1 "Failover:"
4602
4603 $Failovers = Get-DHCPServerV4Failover -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0
4604
4605 If($? -and $Null -ne $Failovers)
4606 {
4607 ForEach($Failover in $Failovers)
4608 {
4609 Write-Verbose "$(Get-Date):`t`tProcessing failover $($Failover.Name)"
4610 Line 2 "Relationship name: " $Failover.Name
4611 Line 2 "Partner Server`t`t`t: " $Failover.PartnerServer
4612 Line 2 "Mode`t`t`t`t: " $Failover.Mode
4613
4614 If($Null -ne $Failover.MaxClientLeadTime)
4615 {
4616 $MaxLeadStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4617 $Failover.MaxClientLeadTime.Day, `
4618 $Failover.MaxClientLeadTime.Hour, `
4619 $Failover.MaxClientLeadTime.Minute)
4620 }
4621 Else
4622 {
4623 $MaxLeadStr = ""
4624 }
4625
4626 Line 2 "Max Client Lead Time`t`t: " $MaxLeadStr
4627
4628 If($Null -ne $Failover.StateSwitchInterval)
4629 {
4630 $SwitchStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4631 $Failover.StateSwitchInterval.Day, `
4632 $Failover.StateSwitchInterval.Hour, `
4633 $Failover.StateSwitchInterval.Minute)
4634 }
4635 Else
4636 {
4637 $SwitchStr = "Disabled"
4638 }
4639
4640 Line 2 "State Switchover Interval`t: " $SwitchStr
4641
4642 Switch($Failover.State)
4643 {
4644 "NoState"
4645 {
4646 Line 2 "State of this Server`t`t: No State"
4647 Line 2 "State of Partner Server`t`t: No State"
4648 }
4649 "Normal"
4650 {
4651 Line 2 "State of this Server`t`t: Normal"
4652 Line 2 "State of Partner Server`t`t: Normal"
4653 }
4654 "Init"
4655 {
4656 Line 2 "State of this Server`t`t: Initializing"
4657 Line 2 "State of Partner Server`t`t: Initializing"
4658 }
4659 "CommunicationInterrupted"
4660 {
4661 Line 2 "State of this Server`t`t: Communication Interrupted"
4662 Line 2 "State of Partner Server`t`t: Communication Interrupted"
4663 }
4664 "PartnerDown"
4665 {
4666 Line 2 "State of this Server`t`t: Normal"
4667 Line 2 "State of Partner Server`t`t: Down"
4668 }
4669 "PotentialConflict"
4670 {
4671 Line 2 "State of this Server`t`t: Potential Conflict"
4672 Line 2 "State of Partner Server`t`t: Potential Conflict"
4673 }
4674 "Startup"
4675 {
4676 Line 2 "State of this Server`t`t: Starting Up"
4677 Line 2 "State of Partner Server`t`t: Starting Up"
4678 }
4679 "ResolutionInterrupted"
4680 {
4681 Line 2 "State of this Server`t`t: Resolution Interrupted"
4682 Line 2 "State of Partner Server`t`t: Resolution Interrupted"
4683 }
4684 "ConflictDone"
4685 {
4686 Line 2 "State of this Server`t`t: Conflict Done"
4687 Line 2 "State of Partner Server`t`t: Conflict Done"
4688 }
4689 "Recover"
4690 {
4691 Line 2 "State of this Server`t`t: Recover"
4692 Line 2 "State of Partner Server`t`t: Recover"
4693 }
4694 "RecoverWait"
4695 {
4696 Line 2 "State of this Server`t`t: Recover Wait"
4697 Line 2 "State of Partner Server`t`t: Recover Wait"
4698 }
4699 "RecoverDone"
4700 {
4701 Line 2 "State of this Server`t`t: Recover Done"
4702 Line 2 "State of Partner Server`t`t: Recover Done"
4703 }
4704 Default
4705 {
4706 Line 2 "State of this Server`t`t: Unable to determine Server: state"
4707 Line 2 "State of Partner Server`t`t: Unable to determine Server: state"
4708 }
4709 }
4710
4711 If($Failover.Mode -eq "LoadBalance")
4712 {
4713 Line 2 "Local server`t`t`t: $($Failover.LoadBalancePercent)%"
4714 Line 2 "Partner Server`t`t`t: " -NoNewLine
4715 $tmp = (100 - $($Failover.LoadBalancePercent))
4716 Line 0 "$($tmp)%"
4717 $tmp = $Null
4718 }
4719 Else
4720 {
4721 Line 2 "Role of this server`t`t: " $Failover.ServerRole
4722 Line 2 "Addresses reserved for standby server: $($Failover.ReservePercent)%"
4723 }
4724
4725 #skip a row for spacing
4726 Line 0 ""
4727 }
4728 }
4729 Else
4730 {
4731 Line 2 "<None>"
4732 }
4733 $Failovers = $Null
4734
4735 Write-Verbose "$(Get-Date):`t`tGetting Scope statistics"
4736 Line 1 "Statistics:"
4737
4738 $Statistics = Get-DHCPServerV4ScopeStatistics -ComputerName $DHCPServerName -ScopeId $IPv4Scope.ScopeId -EA 0
4739
4740 If($? -and $Null -ne $Statistics)
4741 {
4742 GetShortStatistics $Statistics
4743 }
4744 ElseIf(!$?)
4745 {
4746 Line "Error retrieving scope statistics"
4747 }
4748 Else
4749 {
4750 Line 2 "<None>"
4751 }
4752 $Statistics = $Null
4753}
4754
4755Function GetIPv4ScopeData
4756{
4757 Param([object]$IPv4Scope, [int]$xStartLevel)
4758
4759 If($MSWord -or $PDF)
4760 {
4761 GetIPv4ScopeData_WordPDF
4762 }
4763 ElseIf($Text)
4764 {
4765 GetIPv4ScopeData_Text
4766 }
4767 ElseIf($HTML)
4768 {
4769 GetIPv4ScopeData_HTML
4770 }
4771}
4772
4773Function GetIPv6ScopeData_WordPDF
4774{
4775 Write-Verbose "$(Get-Date):`tGetting IPv6 scope data for scope $($IPv6Scope.Name)"
4776 WriteWordLine 3 0 "Scope [$($IPv6Scope.Prefix)] $($IPv6Scope.Name)"
4777 WriteWordLine 4 0 "General"
4778 $TableRange = $doc.Application.Selection.Range
4779 [int]$Columns = 2
4780 If(![string]::IsNullOrEmpty($IPv6Scope.Description))
4781 {
4782 [int]$Rows = 6
4783 }
4784 Else
4785 {
4786 [int]$Rows = 5
4787 }
4788 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
4789 $table.Style = $myHash.Word_TableGrid
4790 $table.Borders.InsideLineStyle = $wdLineStyleNone
4791 $table.Borders.OutsideLineStyle = $wdLineStyleNone
4792 $Table.Cell(1,1).Range.Text = "Prefix"
4793 $Table.Cell(1,2).Range.Text = $IPv6Scope.Prefix
4794 $Table.Cell(2,1).Range.Text = "Preference"
4795 $Table.Cell(2,2).Range.Text = $IPv6Scope.Preference
4796 $Table.Cell(3,1).Range.Text = "Available Range"
4797 $Table.Cell(3,2).Range.Text = ""
4798 $Table.Cell(4,1).Range.Text = "`tStart"
4799 $Table.Cell(4,2).Range.Text = "$($IPv6Scope.Prefix)0:0:0:1"
4800 $Table.Cell(5,1).Range.Text = "`tEnd"
4801 $Table.Cell(5,2).Range.Text = "$($IPv6Scope.Prefix)ffff:ffff:ffff:ffff"
4802 If(![string]::IsNullOrEmpty($IPv6Scope.Description))
4803 {
4804 $Table.Cell(6,1).Range.Text = "Description"
4805 $Table.Cell(6,2).Range.Text = $IPv6Scope.Description
4806 }
4807 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
4808 $table.AutoFitBehavior($wdAutoFitContent)
4809
4810 #return focus back to document
4811 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
4812
4813 #move to the end of the current document
4814 $selection.EndKey($wdStory,$wdMove) | Out-Null
4815 $TableRange = $Null
4816 $Table = $Null
4817
4818 Write-Verbose "$(Get-Date):`t`tGetting scope DNS settings"
4819 WriteWordLine 4 0 "DNS"
4820 $DNSSettings = Get-DHCPServerV6DnsSetting -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0
4821 If($? -and $Null -ne $DNSSettings)
4822 {
4823 GetDNSSettings $DNSSettings "AAAA"
4824 }
4825 Else
4826 {
4827 WriteWordLine 0 0 "Error retrieving IPv6 DNS Settings for scope $($IPv6Scope.Prefix)"
4828 }
4829 $DNSSettings = $Null
4830
4831 Write-Verbose "$(Get-Date):`t`tGetting scope lease settings"
4832 WriteWordLine 4 0 "Lease"
4833
4834 $PrefStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4835 $IPv6Scope.PreferredLifetime.Days, `
4836 $IPv6Scope.PreferredLifetime.Hours, `
4837 $IPv6Scope.PreferredLifetime.Minutes)
4838
4839 $ValidStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4840 $IPv6Scope.ValidLifetime.Days, `
4841 $IPv6Scope.ValidLifetime.Hours, `
4842 $IPv6Scope.ValidLifetime.Minutes)
4843 $TableRange = $doc.Application.Selection.Range
4844 [int]$Columns = 2
4845 [int]$Rows = 2
4846 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
4847 $table.Style = $myHash.Word_TableGrid
4848 $table.Borders.InsideLineStyle = $wdLineStyleNone
4849 $table.Borders.OutsideLineStyle = $wdLineStyleNone
4850 $Table.Cell(1,1).Range.Text = "Preferred life time"
4851 $Table.Cell(1,2).Range.Text = $PrefStr
4852 $Table.Cell(2,1).Range.Text = "Valid life time"
4853 $Table.Cell(2,2).Range.Text = $ValidStr
4854
4855 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
4856 $table.AutoFitBehavior($wdAutoFitContent)
4857
4858 #return focus back to document
4859 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
4860
4861 #move to the end of the current document
4862 $selection.EndKey($wdStory,$wdMove) | Out-Null
4863 $TableRange = $Null
4864 $Table = $Null
4865
4866 If($IncludeLeases)
4867 {
4868 Write-Verbose "$(Get-Date):`t`tGetting leases"
4869 WriteWordLine 4 0 "Address Leases"
4870 $Leases = Get-DHCPServerV6Lease -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object IPAddress
4871 If($? -and $Null -ne $Leases)
4872 {
4873 $TableRange = $doc.Application.Selection.Range
4874 [int]$Columns = 2
4875 If($Leases -is [array])
4876 {
4877 [int]$Rows = $Leases.Count * 8
4878 }
4879 Else
4880 {
4881 [int]$Rows = 7
4882 }
4883 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
4884 $table.Style = $myHash.Word_TableGrid
4885 $table.Borders.InsideLineStyle = $wdLineStyleNone
4886 $table.Borders.OutsideLineStyle = $wdLineStyleNone
4887 [int]$xRow = 0
4888 ForEach($Lease in $Leases)
4889 {
4890 Write-Verbose "$(Get-Date):`t`t`tProcessing lease $($Lease.IPAddress)"
4891 $xRow++
4892 If($Null -ne $Lease.LeaseExpiryTime)
4893 {
4894 $LeaseStr = [string]::format("{0} days, {1} hours, {2} minutes", `
4895 $Lease.LeaseExpiryTime.Day, `
4896 $Lease.LeaseExpiryTime.Hour, `
4897 $Lease.LeaseExpiryTime.Minute)
4898 }
4899 Else
4900 {
4901 $LeaseStr = ""
4902 }
4903
4904 $Table.Cell($xRow,1).Range.Text = "Client IPv6 address"
4905 $Table.Cell($xRow,2).Range.Text = $Lease.IPAddress
4906
4907 $xRow++
4908 $Table.Cell($xRow,1).Range.Text = "Name"
4909 $Table.Cell($xRow,2).Range.Text = $Lease.HostName
4910
4911 $xRow++
4912 $Table.Cell($xRow,1).Range.Text = "Lease Expiration"
4913 $Table.Cell($xRow,2).Range.Text = $LeaseStr
4914
4915 $xRow++
4916 $Table.Cell($xRow,1).Range.Text = "IAID"
4917 $Table.Cell($xRow,2).Range.Text = $Lease.Iaid
4918
4919 $xRow++
4920 $Table.Cell($xRow,1).Range.Text = "Type"
4921 $Table.Cell($xRow,2).Range.Text = $Lease.AddressType
4922
4923 $xRow++
4924 $Table.Cell($xRow,1).Range.Text = "Unique ID"
4925 $Table.Cell($xRow,2).Range.Text = $Lease.ClientDuid
4926
4927 $xRow++
4928 $Table.Cell($xRow,1).Range.Text = "Description"
4929 $Table.Cell($xRow,2).Range.Text = $Lease.Description
4930
4931 #skip a row for spacing
4932 $xRow++
4933 }
4934 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
4935 $table.AutoFitBehavior($wdAutoFitContent)
4936
4937 #return focus back to document
4938 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
4939
4940 #move to the end of the current document
4941 $selection.EndKey($wdStory,$wdMove) | Out-Null
4942 $TableRange = $Null
4943 $Table = $Null
4944 }
4945 ElseIf(!$?)
4946 {
4947 WriteWordLine 0 0 "Error retrieving leases for scope $IPv6Scope.Prefix"
4948 }
4949 Else
4950 {
4951 WriteWordLine 0 1 "<None>"
4952 }
4953 $Leases = $Null
4954 }
4955
4956 Write-Verbose "$(Get-Date):`t`tGetting exclusions"
4957 WriteWordLine 4 0 "Exclusions"
4958 $Exclusions = Get-DHCPServerV6ExclusionRange -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object StartRange
4959 If($? -and $Null -ne $Exclusions)
4960 {
4961 $TableRange = $doc.Application.Selection.Range
4962 [int]$Columns = 2
4963 If($Exclusions -is [array])
4964 {
4965 [int]$Rows = $Exclusions.Count + 1
4966 }
4967 Else
4968 {
4969 [int]$Rows = 2
4970 }
4971 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
4972 $table.Style = $myHash.Word_TableGrid
4973 $table.Borders.InsideLineStyle = $wdLineStyleNone
4974 $table.Borders.OutsideLineStyle = $wdLineStyleNone
4975 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
4976 $Table.Cell(1,1).Range.Font.Bold = $True
4977 $Table.Cell(1,1).Range.Text = "Start IP Address"
4978 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
4979 $Table.Cell(1,2).Range.Font.Bold = $True
4980 $Table.Cell(1,2).Range.Text = "End IP Address"
4981 [int]$xRow = 1
4982 ForEach($Exclusion in $Exclusions)
4983 {
4984 $xRow++
4985 $Table.Cell($xRow,1).Range.Text = $Exclusion.StartRange
4986 $Table.Cell($xRow,2).Range.Text = $Exclusion.EndRange
4987 }
4988 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
4989 $table.AutoFitBehavior($wdAutoFitContent)
4990
4991 #return focus back to document
4992 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
4993
4994 #move to the end of the current document
4995 $selection.EndKey($wdStory,$wdMove) | Out-Null
4996 $TableRange = $Null
4997 $Table = $Null
4998 }
4999 ElseIf(!$?)
5000 {
5001 WriteWordLine 0 0 "Error retrieving exclusions for scope $IPv6Scope.Prefix"
5002 }
5003 Else
5004 {
5005 WriteWordLine 0 1 "<None>"
5006 }
5007 $Exclusions = $Null
5008
5009 Write-Verbose "$(Get-Date):`t`tGetting reservations"
5010 WriteWordLine 4 0 "Reservations"
5011 $Reservations = Get-DHCPServerV6Reservation -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object IPAddress
5012 If($? -and $Null -ne $Reservations)
5013 {
5014 ForEach($Reservation in $Reservations)
5015 {
5016 Write-Verbose "$(Get-Date):`t`t`tProcessing reservation $($Reservation.Name)"
5017 $TableRange = $doc.Application.Selection.Range
5018 [int]$Columns = 2
5019 If([string]::IsNullOrEmpty($Reservation.Description))
5020 {
5021 [int]$Rows = 4
5022 }
5023 Else
5024 {
5025 [int]$Rows = 5
5026 }
5027 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
5028 $table.Style = $myHash.Word_TableGrid
5029 $table.Borders.InsideLineStyle = $wdLineStyleNone
5030 $table.Borders.OutsideLineStyle = $wdLineStyleNone
5031 $Table.Cell(1,1).Range.Text = "Reservation name"
5032 $Table.Cell(1,2).Range.Text = $Reservation.Name
5033 $Table.Cell(2,1).Range.Text = "IPv6 address"
5034 $Table.Cell(2,2).Range.Text = $Reservation.IPAddress
5035 $Table.Cell(3,1).Range.Text = "DUID"
5036 $Table.Cell(3,2).Range.Text = $Reservation.ClientDuid
5037 $Table.Cell(4,1).Range.Text = "IAID"
5038 $Table.Cell(4,2).Range.Text = $Reservation.Iaid
5039 If(![string]::IsNullOrEmpty($Reservation.Description))
5040 {
5041 $Table.Cell(5,1).Range.Text = "Description"
5042 $Table.Cell(5,2).Range.Text = $Reservation.Description
5043 }
5044 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
5045 $table.AutoFitBehavior($wdAutoFitContent)
5046
5047 #return focus back to document
5048 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
5049
5050 #move to the end of the current document
5051 $selection.EndKey($wdStory,$wdMove) | Out-Null
5052 $TableRange = $Null
5053 $Table = $Null
5054
5055 Write-Verbose "$(Get-Date):`t`t`tGetting DNS settings"
5056 $DNSSettings = Get-DHCPServerV6DnsSetting -ComputerName $DHCPServerName -IPAddress $Reservation.IPAddress -EA 0
5057 If($? -and $Null -ne $DNSSettings)
5058 {
5059 GetDNSSettings $DNSSettings "AAAA"
5060 }
5061 Else
5062 {
5063 WriteWordLine 0 0 "Error to retrieving DNS Settings for reserved IP address $Reservation.IPAddress"
5064 }
5065 $DNSSettings = $Null
5066 }
5067 }
5068 ElseIf(!$?)
5069 {
5070 WriteWordLine 0 0 "Error retrieving reservations for scope $IPv6Scope.Prefix"
5071 }
5072 Else
5073 {
5074 WriteWordLine 0 1 "<None>"
5075 }
5076 $Reservations = $Null
5077
5078 Write-Verbose "$(Get-Date):Getting IPv6 scope options"
5079 WriteWordLine 4 0 "Scope Options"
5080 $ScopeOptions = Get-DHCPServerV6OptionValue -All -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object OptionId
5081
5082 If($? -and $Null -ne $ScopeOptions)
5083 {
5084 $TableRange = $doc.Application.Selection.Range
5085 [int]$Columns = 2
5086 If($ScopeOptions -is [array])
5087 {
5088 [int]$Rows = $ScopeOptions.Count * 4
5089 }
5090 Else
5091 {
5092 [int]$Rows = 3
5093 }
5094 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
5095 $table.Style = $myHash.Word_TableGrid
5096 $table.Borders.InsideLineStyle = $wdLineStyleNone
5097 $table.Borders.OutsideLineStyle = $wdLineStyleNone
5098 [int]$xRow = 0
5099 ForEach($ScopeOption in $ScopeOptions)
5100 {
5101 Write-Verbose "$(Get-Date):`t`t`tProcessing option name $($ScopeOption.Name)"
5102 $xRow++
5103 $Table.Cell($xRow,1).Range.Text = "Option Name"
5104 $Table.Cell($xRow,2).Range.Text = "$($ScopeOption.OptionId.ToString("00000")) $($ScopeOption.Name)"
5105
5106 $xRow++
5107 $Table.Cell($xRow,1).Range.Text = "Vendor"
5108 If([string]::IsNullOrEmpty($ScopeOption.VendorClass))
5109 {
5110 $Table.Cell($xRow,2).Range.Text = "Standard"
5111 }
5112 Else
5113 {
5114 $Table.Cell($xRow,2).Range.Text = $ScopeOption.VendorClass
5115 }
5116
5117 $xRow++
5118 $Table.Cell($xRow,1).Range.Text = "Value"
5119 $Table.Cell($xRow,2).Range.Text = "$($ScopeOption.Value)"
5120
5121 #for spacing
5122 $xRow++
5123 }
5124 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
5125 $table.AutoFitBehavior($wdAutoFitContent)
5126
5127 #return focus back to document
5128 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
5129
5130 #move to the end of the current document
5131 $selection.EndKey($wdStory,$wdMove) | Out-Null
5132 $TableRange = $Null
5133 $Table = $Null
5134 }
5135 ElseIf(!$?)
5136 {
5137 WriteWordLine 0 0 "Error retrieving IPv6 scope options"
5138 }
5139 Else
5140 {
5141 WriteWordLine 0 1 "<None>"
5142 }
5143 $ScopeOptions = $Null
5144
5145 Write-Verbose "$(Get-Date):`t`tGetting Scope statistics"
5146 WriteWordLine 4 0 "Statistics"
5147
5148 $Statistics = Get-DHCPServerV6ScopeStatistics -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0
5149
5150 If($? -and $Null -ne $Statistics)
5151 {
5152 GetShortStatistics $Statistics
5153 }
5154 ElseIf(!$?)
5155 {
5156 WriteWordLine 0 0 "Error retrieving scope statistics"
5157 }
5158 Else
5159 {
5160 WriteWordLine 0 1 "<None>"
5161 }
5162 $Statistics = $Null
5163}
5164
5165Function GetIPv6ScopeData_HTML
5166{
5167}
5168
5169Function GetIPv6ScopeData_Text
5170{
5171 Write-Verbose "$(Get-Date):`tGetting IPv6 scope data for scope $($IPv6Scope.Name)"
5172 Line 0 "Scope [$($IPv6Scope.Prefix)] $($IPv6Scope.Name)"
5173 Line 1 "General"
5174 Line 2 "Prefix`t`t: " $IPv6Scope.Prefix
5175 Line 2 "Preference`t: " $IPv6Scope.Preference
5176 Line 2 "Available Range`t: "
5177 Line 3 "Start`t: $($IPv6Scope.Prefix)0:0:0:1"
5178 Line 3 "End`t: $($IPv6Scope.Prefix)ffff:ffff:ffff:ffff"
5179 If(![string]::IsNullOrEmpty($IPv6Scope.Description))
5180 {
5181 Line 2 "Description`t: " $IPv6Scope.Description
5182 }
5183
5184 Write-Verbose "$(Get-Date):`t`tGetting scope DNS settings"
5185 Line 1 "DNS"
5186 $DNSSettings = Get-DHCPServerV6DnsSetting -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0
5187 If($? -and $Null -ne $DNSSettings)
5188 {
5189 GetDNSSettings $DNSSettings "AAAA"
5190 }
5191 Else
5192 {
5193 Line 0 "Error retrieving IPv6 DNS Settings for scope $($IPv6Scope.Prefix)"
5194 }
5195 $DNSSettings = $Null
5196
5197 Write-Verbose "$(Get-Date):`t`tGetting scope lease settings"
5198 Line 1 "Lease"
5199
5200 $PrefStr = [string]::format("{0} days, {1} hours, {2} minutes", `
5201 $IPv6Scope.PreferredLifetime.Days, `
5202 $IPv6Scope.PreferredLifetime.Hours, `
5203 $IPv6Scope.PreferredLifetime.Minutes)
5204
5205 $ValidStr = [string]::format("{0} days, {1} hours, {2} minutes", `
5206 $IPv6Scope.ValidLifetime.Days, `
5207 $IPv6Scope.ValidLifetime.Hours, `
5208 $IPv6Scope.ValidLifetime.Minutes)
5209 Line 2 "Preferred life time`t: " $PrefStr
5210 Line 2 "Valid life time`t`t: " $ValidStr
5211
5212 If($IncludeLeases)
5213 {
5214 Write-Verbose "$(Get-Date):`t`tGetting leases"
5215 Line 1 "Address Leases:"
5216 $Leases = Get-DHCPServerV6Lease -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object IPAddress
5217 If($? -and $Null -ne $Leases)
5218 {
5219 ForEach($Lease in $Leases)
5220 {
5221 Write-Verbose "$(Get-Date):`t`t`tProcessing lease $($Lease.IPAddress)"
5222 If($Null -ne $Lease.LeaseExpiryTime)
5223 {
5224 $LeaseStr = [string]::format("{0} days, {1} hours, {2} minutes", `
5225 $Lease.LeaseExpiryTime.Day, `
5226 $Lease.LeaseExpiryTime.Hour, `
5227 $Lease.LeaseExpiryTime.Minute)
5228 }
5229 Else
5230 {
5231 $LeaseStr = ""
5232 }
5233
5234 Line 2 "Client IPv6 address: " $Lease.IPAddress
5235 Line 2 "Name`t`t`t: " $Lease.HostName
5236 Line 2 "Lease Expiration`t: " $LeaseStr
5237 Line 2 "IAID`t`t`t: " $Lease.Iaid
5238 Line 2 "Type`t`t`t: " $Lease.AddressType
5239 Line 2 "Unique ID`t`t: " $Lease.ClientDuid
5240 If(![string]::IsNullOrEmpty($Lease.Description))
5241 {
5242 Line 2 "Description`t`t: " $Lease.Description
5243 }
5244
5245 #skip a row for spacing
5246 Line 0 ""
5247 }
5248 }
5249 ElseIf(!$?)
5250 {
5251 Line 0 "Error retrieving leases for scope $IPv6Scope.Prefix"
5252 }
5253 Else
5254 {
5255 Line 2 "<None>"
5256 }
5257 $Leases = $Null
5258 }
5259
5260 Write-Verbose "$(Get-Date):`t`tGetting exclusions"
5261 Line 1 "Exclusions:"
5262 $Exclusions = Get-DHCPServerV6ExclusionRange -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object StartRange
5263 If($? -and $Null -ne $Exclusions)
5264 {
5265 ForEach($Exclusion in $Exclusions)
5266 {
5267 Line 2 "Start IP Address`t: " $Exclusion.StartRange
5268 Line 2 "End IP Address`t`t: " $Exclusion.EndRange
5269 Line 0 ""
5270 }
5271 }
5272 ElseIf(!$?)
5273 {
5274 Line 0 "Error retrieving exclusions for scope $IPv6Scope.Prefix"
5275 }
5276 Else
5277 {
5278 Line 2 "<None>"
5279 }
5280 $Exclusions = $Null
5281
5282 Write-Verbose "$(Get-Date):`t`tGetting reservations"
5283 Line 1 "Reservations:"
5284 $Reservations = Get-DHCPServerV6Reservation -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object IPAddress
5285 If($? -and $Null -ne $Reservations)
5286 {
5287 ForEach($Reservation in $Reservations)
5288 {
5289 Write-Verbose "$(Get-Date):`t`t`tProcessing reservation $($Reservation.Name)"
5290 Line 2 "Reservation name: " $Reservation.Name
5291 Line 2 "IPv6 address: " $Reservation.IPAddress
5292 Line 2 "DUID`t`t: " $Reservation.ClientDuid
5293 Line 2 "IAID`t`t: " $Reservation.Iaid
5294 If(![string]::IsNullOrEmpty($Reservation.Description))
5295 {
5296 Line 2 "Description`t: " $Reservation.Description
5297 }
5298
5299 Write-Verbose "$(Get-Date):`t`t`tGetting DNS settings"
5300 $DNSSettings = Get-DHCPServerV6DnsSetting -ComputerName $DHCPServerName -IPAddress $Reservation.IPAddress -EA 0
5301 If($? -and $Null -ne $DNSSettings)
5302 {
5303 GetDNSSettings $DNSSettings "AAAA"
5304 }
5305 Else
5306 {
5307 Line 0 "Error to retrieving DNS Settings for reserved IP address $Reservation.IPAddress"
5308 }
5309 $DNSSettings = $Null
5310 }
5311 }
5312 ElseIf(!$?)
5313 {
5314 Line 0 "Error retrieving reservations for scope $IPv6Scope.Prefix"
5315 }
5316 Else
5317 {
5318 Line 2 "<None>"
5319 }
5320 $Reservations = $Null
5321
5322 Write-Verbose "$(Get-Date):Getting IPv6 scope options"
5323 Line 1 "Scope Options:"
5324 $ScopeOptions = Get-DHCPServerV6OptionValue -All -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0 | Sort-Object OptionId
5325
5326 If($? -and $Null -ne $ScopeOptions)
5327 {
5328 ForEach($ScopeOption in $ScopeOptions)
5329 {
5330 Write-Verbose "$(Get-Date):`t`t`tProcessing option name $($ScopeOption.Name)"
5331 Line 2 "Option Name`t: $($ScopeOption.OptionId.ToString("00000")) $($ScopeOption.Name)"
5332 Line 2 "Vendor`t`t: " -NoNewLine
5333 If([string]::IsNullOrEmpty($ScopeOption.VendorClass))
5334 {
5335 Line 0 "Standard"
5336 }
5337 Else
5338 {
5339 Line 0 $ScopeOption.VendorClass
5340 }
5341 Line 2 "Value`t`t: $($ScopeOption.Value)"
5342
5343 #for spacing
5344 Line 0 ""
5345 }
5346 }
5347 ElseIf(!$?)
5348 {
5349 Line 0 "Error retrieving IPv6 scope options"
5350 }
5351 Else
5352 {
5353 Line 2 "<None>"
5354 }
5355 $ScopeOptions = $Null
5356
5357 Write-Verbose "$(Get-Date):`t`tGetting Scope statistics"
5358 Line 1 "Statistics:"
5359
5360 $Statistics = Get-DHCPServerV6ScopeStatistics -ComputerName $DHCPServerName -Prefix $IPv6Scope.Prefix -EA 0
5361
5362 If($? -and $Null -ne $Statistics)
5363 {
5364 GetShortStatistics $Statistics
5365 }
5366 ElseIf(!$?)
5367 {
5368 Line 0 "Error retrieving scope statistics"
5369 }
5370 Else
5371 {
5372 Line 2 "<None>"
5373 }
5374 $Statistics = $Null
5375}
5376
5377Function GetIPV6ScopeData
5378{
5379 Param([object]$IPv6Scope)
5380
5381 If($MSWord -or $PDF)
5382 {
5383 GetIPv6ScopeData_WordPDF
5384 }
5385 ElseIf($Text)
5386 {
5387 GetIPv6ScopeData_Text
5388 }
5389 ElseIf($HTML)
5390 {
5391 GetIPv6ScopeData_HTML
5392 }
5393}
5394
5395Function GetDNSSettings
5396{
5397 Param([object]$DNSSettings, [string]$As)
5398
5399 If($MSWord -or $PDF)
5400 {
5401 $TableRange = $doc.Application.Selection.Range
5402 [int]$Columns = 2
5403 [int]$Rows = 4
5404 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
5405 $table.Style = $myHash.Word_TableGrid
5406 $table.Borders.InsideLineStyle = $wdLineStyleNone
5407 $table.Borders.OutsideLineStyle = $wdLineStyleNone
5408 [int]$xxRow = 1
5409 $Table.Cell($xxRow,1).Range.Text = "Enable DNS dynamic updates"
5410 If($DNSSettings.DynamicUpdates -eq "Never")
5411 {
5412 $Table.Cell($xxRow,2).Range.Text = "Disabled"
5413 }
5414 ElseIf($DNSSettings.DynamicUpdates -eq "OnClientRequest")
5415 {
5416 $Table.Cell($xxRow,2).Range.Text = "Enabled"
5417 $xxRow++
5418 $Table.Cell($xxRow,1).Range.Text = "Dynamically update DNS $($As) and PTR records only if requested by the DHCP clients"
5419 }
5420 ElseIf($DNSSettings.DynamicUpdates -eq "Always")
5421 {
5422 $Table.Cell($xxRow,2).Range.Text = "Enabled"
5423 $xxRow++
5424 $Table.Cell($xxRow,1).Range.Text = "Always dynamically update DNS $($As) and PTR records"
5425 }
5426 $xxRow++
5427 $Table.Cell($xxRow,1).Range.Text = "Discard $($As) and PTR records when lease is deleted"
5428 If($DNSSettings.DeleteDnsRROnLeaseExpiry)
5429 {
5430 $Table.Cell($xxRow,2).Range.Text = "Enabled"
5431 }
5432 Else
5433 {
5434 $Table.Cell($xxRow,2).Range.Text = "Disabled"
5435 }
5436 $xxRow++
5437 $Table.Cell($xxRow,1).Range.Text = "Name Protection"
5438 If($DNSSettings.NameProtection)
5439 {
5440 $Table.Cell($xxRow,2).Range.Text = "Enabled"
5441 }
5442 Else
5443 {
5444 $Table.Cell($xxRow,2).Range.Text = "Disabled"
5445 }
5446
5447 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
5448 $table.AutoFitBehavior($wdAutoFitContent)
5449
5450 #return focus back to document
5451 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
5452
5453 #move to the end of the current document
5454 $selection.EndKey($wdStory,$wdMove) | Out-Null
5455 $TableRange = $Null
5456 $Table = $Null
5457 }
5458 ElseIf($Text)
5459 {
5460 Line 2 "Enable DNS dynamic updates`t`t`t: " -NoNewLine
5461 If($DNSSettings.DynamicUpdates -eq "Never")
5462 {
5463 Line 0 "Disabled"
5464 }
5465 ElseIf($DNSSettings.DynamicUpdates -eq "OnClientRequest")
5466 {
5467 Line 0 "Enabled"
5468 Line 2 "Dynamically update DNS $($As) & PTR records only if requested by the DHCP clients"
5469 }
5470 ElseIf($DNSSettings.DynamicUpdates -eq "Always")
5471 {
5472 Line 0 "Enabled"
5473 Line 2 "Always dynamically update DNS $($As) & PTR records"
5474 }
5475 Line 2 "Discard $($As) & PTR records when lease deleted`t: " -NoNewLine
5476 If($DNSSettings.DeleteDnsRROnLeaseExpiry)
5477 {
5478 Line 0 "Enabled"
5479 }
5480 Else
5481 {
5482 Line 0 "Disabled"
5483 }
5484 Line 2 "Name Protection`t`t`t`t`t: " -NoNewLine
5485 If($DNSSettings.NameProtection)
5486 {
5487 Line 0 "Enabled"
5488 }
5489 Else
5490 {
5491 Line 0 "Disabled"
5492 }
5493 }
5494 ElseIf($HTML)
5495 {
5496 }
5497}
5498
5499Function GetShortStatistics
5500{
5501 Param([object]$Statistics)
5502
5503 If($MSWord -or $PDF)
5504 {
5505 $TableRange = $doc.Application.Selection.Range
5506 [int]$Columns = 2
5507 [int]$Rows = 4
5508 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
5509 $table.Style = $myHash.Word_TableGrid
5510 $table.Borders.InsideLineStyle = $wdLineStyleSingle
5511 $table.Borders.OutsideLineStyle = $wdLineStyleSingle
5512 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
5513 $Table.Cell(1,1).Range.Font.Bold = $True
5514 $Table.Cell(1,1).Range.Text = "Description"
5515 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
5516 $Table.Cell(1,2).Range.Font.Bold = $True
5517 $Table.Cell(1,2).Range.Text = "Details"
5518
5519 $Table.Cell(2,1).Range.Text = "Total Addresses"
5520 [decimal]$TotalAddresses = "{0:N0}" -f ($Statistics.AddressesFree + $Statistics.AddressesInUse)
5521 $Table.Cell(2,2).Range.Text = $TotalAddresses.ToString()
5522 $Table.Cell(3,1).Range.Text = "In Use"
5523 [int]$InUsePercent = "{0:N0}" -f $Statistics.PercentageInUse.ToString()
5524 $Table.Cell(3,2).Range.Text = "$($Statistics.AddressesInUse) ($($InUsePercent))%"
5525 $Table.Cell(4,1).Range.Text = "Available"
5526 If($TotalAddresses -ne 0)
5527 {
5528 [int]$AvailablePercent = "{0:N0}" -f (($Statistics.AddressesFree / $TotalAddresses) * 100)
5529 }
5530 Else
5531 {
5532 [int]$AvailablePercent = 0
5533 }
5534 $Table.Cell(4,2).Range.Text = "$($Statistics.AddressesFree) ($($AvailablePercent))%"
5535
5536 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
5537 $table.AutoFitBehavior($wdAutoFitContent)
5538
5539 #return focus back to document
5540 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
5541
5542 #move to the end of the current document
5543 $selection.EndKey($wdStory,$wdMove) | Out-Null
5544 $TableRange = $Null
5545 $Table = $Null
5546 }
5547 ElseIf($Text)
5548 {
5549 Line 2 "Description" -NoNewLine
5550 Line 1 "Details"
5551 Line 2 "Total Addresses`t" -NoNewLine
5552 [decimal]$TotalAddresses = ($Statistics.AddressesFree + $Statistics.AddressesInUse)
5553 $tmp = "{0:N0}" -f $TotalAddresses
5554 Line 0 $tmp
5555 Line 2 "In Use`t`t" -NoNewLine
5556 [int]$InUsePercent = "{0:N0}" -f $Statistics.PercentageInUse
5557 $tmp = "{0:N0}" -f $Statistics.AddressesInUse
5558 Line 0 "$($tmp) ($($InUsePercent))%"
5559 Line 2 "Available`t" -NoNewLine
5560 If($TotalAddresses -ne 0)
5561 {
5562 [int]$AvailablePercent = "{0:N0}" -f (($Statistics.AddressesFree / $TotalAddresses) * 100)
5563 }
5564 Else
5565 {
5566 [int]$AvailablePercent = 0
5567 }
5568 $tmp = "{0:N0}" -f $Statistics.AddressesFree
5569 Line 0 "$($tmp) ($($AvailablePercent))%"
5570 Line 0 ""
5571 }
5572 ElseIf($HTML)
5573 {
5574 }
5575}
5576
5577Function InsertBlankLine
5578{
5579 If($MSWord -or $PDF)
5580 {
5581 WriteWordLine 0 0 ""
5582 }
5583 ElseIf($Text)
5584 {
5585 Line 0 ""
5586 }
5587 ElseIf($HTML)
5588 {
5589 }
5590}
5591
5592Function TestComputerName
5593{
5594 Param([string]$Cname)
5595 If(![String]::IsNullOrEmpty($CName))
5596 {
5597 #get computer name
5598 #first test to make sure the computer is reachable
5599 Write-Verbose "$(Get-Date): Testing to see if $($CName) is online and reachable"
5600 If(Test-Connection -ComputerName $CName -quiet)
5601 {
5602 Write-Verbose "$(Get-Date): Server $($CName) is online."
5603 }
5604 Else
5605 {
5606 Write-Verbose "$(Get-Date): Computer $($CName) is offline"
5607 $ErrorActionPreference = $SaveEAPreference
5608 Write-Error "`n`n`t`tComputer $($CName) is offline.`n`t`tScript cannot continue.`n`n"
5609 Exit
5610 }
5611 }
5612
5613 #if computer name is localhost, get actual computer name
5614 If($CName -eq "localhost")
5615 {
5616 $CName = $env:ComputerName
5617 Write-Verbose "$(Get-Date): Computer name has been renamed from localhost to $($CName)"
5618 Write-Verbose "$(Get-Date): Testing to see if $($CName) is a DHCP Server"
5619 $results = Get-DHCPServerVersion -ComputerName $CName -EA 0
5620 If($? -and $Null -ne $results)
5621 {
5622 #the computer is a dhcp server
5623 Write-Verbose "$(Get-Date): Computer $($CName) is a DHCP Server"
5624 Return $CName
5625 }
5626 ElseIf(!$? -or $Null -eq $results)
5627 {
5628 #the computer is not a dhcp server
5629 Write-Verbose "$(Get-Date): Computer $($CName) is not a DHCP Server"
5630 $ErrorActionPreference = $SaveEAPreference
5631 Write-Error "`n`n`t`tComputer $($CName) is not a DHCP Server.`n`n`t`tRerun the script using -ComputerName with a valid DHCP server name.`n`n`t`tScript cannot continue.`n`n"
5632 Exit
5633 }
5634 }
5635
5636 #if computer name is an IP address, get host name from DNS
5637 #http://blogs.technet.com/b/gary/archive/2009/08/29/resolve-ip-addresses-to-hostname-using-powershell.aspx
5638 #help from Michael B. Smith
5639 $ip = $CName -as [System.Net.IpAddress]
5640 If($ip)
5641 {
5642 $Result = [System.Net.Dns]::gethostentry($ip)
5643
5644 If($? -and $Null -ne $Result)
5645 {
5646 $CName = $Result.HostName
5647 Write-Verbose "$(Get-Date): Computer name has been renamed from $($ip) to $($CName)"
5648 Write-Verbose "$(Get-Date): Testing to see if $($CName) is a DHCP Server"
5649 $results = Get-DHCPServerVersion -ComputerName $CName -EA 0
5650 If($? -and $Null -ne $results)
5651 {
5652 #the computer is a dhcp server
5653 Write-Verbose "$(Get-Date): Computer $($CName) is a DHCP Server"
5654 Return $CName
5655 }
5656 ElseIf(!$? -or $Null -eq $results)
5657 {
5658 #the computer is not a dhcp server
5659 Write-Verbose "$(Get-Date): Computer $($CName) is not a DHCP Server"
5660 $ErrorActionPreference = $SaveEAPreference
5661 Write-Error "`n`n`t`tComputer $($CName) is not a DHCP Server.`n`n`t`tRerun the script using -ComputerName with a valid DHCP server name.`n`n`t`tScript cannot continue.`n`n"
5662 Exit
5663 }
5664 }
5665 Else
5666 {
5667 Write-Warning "Unable to resolve $($CName) to a hostname"
5668 }
5669 }
5670 Else
5671 {
5672 Write-Verbose "$(Get-Date): Testing to see if $($CName) is a DHCP Server"
5673 $results = Get-DHCPServerVersion -ComputerName $CName -EA 0
5674 If($? -and $Null -ne $results)
5675 {
5676 #the computer is a dhcp server
5677 Write-Verbose "$(Get-Date): Computer $($CName) is a DHCP Server"
5678 Return $CName
5679 }
5680 ElseIf(!$? -or $Null -eq $results)
5681 {
5682 #the computer is not a dhcp server
5683 Write-Verbose "$(Get-Date): Computer $($CName) is not a DHCP Server"
5684 $ErrorActionPreference = $SaveEAPreference
5685 Write-Error "`n`n`t`tComputer $($CName) is not a DHCP Server.`n`n`t`tRerun the script using -ComputerName with a valid DHCP server name.`n`n`t`tScript cannot continue.`n`n"
5686 Exit
5687 }
5688 }
5689 Return $CName
5690}
5691
5692Function ShowScriptOptions
5693{
5694 Write-Verbose "$(Get-Date): "
5695 Write-Verbose "$(Get-Date): "
5696 Write-Verbose "$(Get-Date): AddDateTime : $($AddDateTime)"
5697 If($MSWORD -or $PDF)
5698 {
5699 Write-Verbose "$(Get-Date): Company Name : $($Script:CoName)"
5700 }
5701 Write-Verbose "$(Get-Date): ComputerName : $($ComputerName)"
5702 If($MSWORD -or $PDF)
5703 {
5704 Write-Verbose "$(Get-Date): Cover Page : $($CoverPage)"
5705 }
5706 Write-Verbose "$(Get-Date): Dev : $($Dev)"
5707 If($Dev)
5708 {
5709 Write-Verbose "$(Get-Date): DevErrorFile : $($Script:DevErrorFile)"
5710 }
5711 Write-Verbose "$(Get-Date): Filename1 : $($Script:Filename1)"
5712 If($PDF)
5713 {
5714 Write-Verbose "$(Get-Date): Filename2 : $($Script:Filename2)"
5715 }
5716 Write-Verbose "$(Get-Date): Folder : $($Folder)"
5717 Write-Verbose "$(Get-Date): From : $($From)"
5718 Write-Verbose "$(Get-Date): Include Leases: $($IncludeLeases)"
5719 Write-Verbose "$(Get-Date): Save As HTML : $($HTML)"
5720 Write-Verbose "$(Get-Date): Save As PDF : $($PDF)"
5721 Write-Verbose "$(Get-Date): Save As TEXT : $($TEXT)"
5722 Write-Verbose "$(Get-Date): Save As WORD : $($MSWORD)"
5723 Write-Verbose "$(Get-Date): ScriptInfo : $($ScriptInfo)"
5724 Write-Verbose "$(Get-Date): Smtp Port : $($SmtpPort)"
5725 Write-Verbose "$(Get-Date): Smtp Server : $($SmtpServer)"
5726 Write-Verbose "$(Get-Date): Title : $($Script:Title)"
5727 Write-Verbose "$(Get-Date): To : $($To)"
5728 Write-Verbose "$(Get-Date): Use SSL : $($UseSSL)"
5729 If($MSWORD -or $PDF)
5730 {
5731 Write-Verbose "$(Get-Date): User Name : $($UserName)"
5732 }
5733 Write-Verbose "$(Get-Date): "
5734 Write-Verbose "$(Get-Date): OS Detected : $($Script:RunningOS)"
5735 Write-Verbose "$(Get-Date): PoSH version : $($Host.Version)"
5736 Write-Verbose "$(Get-Date): PSCulture : $($PSCulture)"
5737 Write-Verbose "$(Get-Date): PSUICulture : $($PSUICulture)"
5738 If($MSWORD -or $PDF)
5739 {
5740 Write-Verbose "$(Get-Date): Word language : $($Script:WordLanguageValue)"
5741 Write-Verbose "$(Get-Date): Word version : $($Script:WordProduct)"
5742 }
5743 Write-Verbose "$(Get-Date): "
5744 Write-Verbose "$(Get-Date): Script start : $($Script:StartTime)"
5745 Write-Verbose "$(Get-Date): "
5746 Write-Verbose "$(Get-Date): "
5747
5748}
5749
5750#region email function
5751Function SendEmail
5752{
5753 Param([string]$Attachments)
5754 Write-Verbose "$(Get-Date): Prepare to email"
5755
5756 $emailAttachment = $Attachments
5757 $emailSubject = $Script:Title
5758 $emailBody = @"
5759Hello, <br />
5760<br />
5761$Script:Title is attached.
5762"@
5763
5764 If($Dev)
5765 {
5766 Out-File -FilePath $Script:DevErrorFile -InputObject $error 4>$Null
5767 }
5768
5769 $error.Clear()
5770
5771 If($UseSSL)
5772 {
5773 Write-Verbose "$(Get-Date): Trying to send email using current user's credentials with SSL"
5774 Send-MailMessage -Attachments $emailAttachment -Body $emailBody -BodyAsHtml -From $From `
5775 -Port $SmtpPort -SmtpServer $SmtpServer -Subject $emailSubject -To $To `
5776 -UseSSL *>$Null
5777 }
5778 Else
5779 {
5780 Write-Verbose "$(Get-Date): Trying to send email using current user's credentials without SSL"
5781 Send-MailMessage -Attachments $emailAttachment -Body $emailBody -BodyAsHtml -From $From `
5782 -Port $SmtpPort -SmtpServer $SmtpServer -Subject $emailSubject -To $To *>$Null
5783 }
5784
5785 $e = $error[0]
5786
5787 If($e.Exception.ToString().Contains("5.7.57"))
5788 {
5789 #The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
5790 Write-Verbose "$(Get-Date): Current user's credentials failed. Ask for usable credentials."
5791
5792 If($Dev)
5793 {
5794 Out-File -FilePath $Script:DevErrorFile -InputObject $error -Append 4>$Null
5795 }
5796
5797 $error.Clear()
5798
5799 $emailCredentials = Get-Credential -Message "Enter the email account and password to send email"
5800
5801 If($UseSSL)
5802 {
5803 Send-MailMessage -Attachments $emailAttachment -Body $emailBody -BodyAsHtml -From $From `
5804 -Port $SmtpPort -SmtpServer $SmtpServer -Subject $emailSubject -To $To `
5805 -UseSSL -credential $emailCredentials *>$Null
5806 }
5807 Else
5808 {
5809 Send-MailMessage -Attachments $emailAttachment -Body $emailBody -BodyAsHtml -From $From `
5810 -Port $SmtpPort -SmtpServer $SmtpServer -Subject $emailSubject -To $To `
5811 -credential $emailCredentials *>$Null
5812 }
5813
5814 $e = $error[0]
5815
5816 If($? -and $Null -eq $e)
5817 {
5818 Write-Verbose "$(Get-Date): Email successfully sent using new credentials"
5819 }
5820 Else
5821 {
5822 Write-Verbose "$(Get-Date): Email was not sent:"
5823 Write-Warning "$(Get-Date): Exception: $e.Exception"
5824 }
5825 }
5826 Else
5827 {
5828 Write-Verbose "$(Get-Date): Email was not sent:"
5829 Write-Warning "$(Get-Date): Exception: $e.Exception"
5830 }
5831}
5832#endregion
5833
5834#Script begins
5835
5836$script:startTime = Get-Date
5837
5838If($TEXT)
5839{
5840 $global:output = ""
5841}
5842
5843$ComputerName = TestComputerName $ComputerName
5844$DHCPServerName = $ComputerName
5845
5846[string]$Script:Title = "DHCP Inventory Report for Server $($DHCPServerName)"
5847SetFileName1andFileName2 "$($DHCPServerName)_DHCP_Inventory"
5848
5849Write-Verbose "$(Get-Date): Server Properties and Configuration"
5850Write-Verbose "$(Get-Date): "
5851
5852Write-Verbose "$(Get-Date): Getting DHCP server information"
5853If($MSWord -or $PDF)
5854{
5855 $selection.InsertNewPage()
5856 WriteWordLine 1 0 "DHCP Server Information"
5857 WriteWordLine 0 0 "Server name: "$DHCPServerName
5858}
5859ElseIf($Text)
5860{
5861 Line 0 "DHCP Server Information"
5862 Line 1 "Server name`t: "$DHCPServerName
5863}
5864ElseIf($HTML)
5865{
5866}
5867
5868$DHCPDB = Get-DHCPServerDatabase -ComputerName $DHCPServerName -EA 0
5869
5870If($? -and $Null -ne $DHCPDB)
5871{
5872 If($MSWord -or $PDF)
5873 {
5874 WriteWordLine 0 0 "Database path: " $DHCPDB.FileName.SubString(0,($DHCPDB.FileName.LastIndexOf('\')))
5875 WriteWordLine 0 0 "Backup path: " $DHCPDB.BackupPath
5876 }
5877 ElseIf($Text)
5878 {
5879 Line 1 "Database path`t: " $DHCPDB.FileName.SubString(0,($DHCPDB.FileName.LastIndexOf('\')))
5880 Line 1 "Backup path`t: " $DHCPDB.BackupPath
5881 }
5882 ElseIf($HTML)
5883 {
5884 }
5885}
5886Else
5887{
5888 If($MSWord -or $PDF)
5889 {
5890 WriteWordLine 0 0 "Error retrieving DHCP Server Database information"
5891 }
5892 ElseIf($Text)
5893 {
5894 Line 0 "Error retrieving DHCP Server Database information"
5895 }
5896 ElseIf($HTML)
5897 {
5898 }
5899}
5900
5901InsertBlankLine
5902
5903$DHCPDB = $Null
5904
5905[bool]$GotServerSettings = $False
5906$ServerSettings = Get-DHCPServerSetting -ComputerName $DHCPServerName -EA 0
5907
5908If($? -and $Null -ne $ServerSettings)
5909{
5910 $GotServerSettings = $True
5911 #some properties of $ServerSettings will be needed later
5912 If($ServerSettings.IsAuthorized)
5913 {
5914 If($MSWord -or $PDF)
5915 {
5916 WriteWordLine 0 0 "DHCP server is authorized"
5917 }
5918 ElseIf($Text)
5919 {
5920 Line 1 "DHCP server is authorized"
5921 }
5922 ElseIf($HTML)
5923 {
5924 }
5925 }
5926 Else
5927 {
5928 If($MSWord -or $PDF)
5929 {
5930 WriteWordLine 0 0 "DHCP server is not authorized"
5931 }
5932 ElseIf($Text)
5933 {
5934 Line 1 "DHCP server is not authorized"
5935 }
5936 ElseIf($HTML)
5937 {
5938 }
5939 }
5940}
5941Else
5942{
5943 If($MSWord -or $PDF)
5944 {
5945 WriteWordLine 0 0 "Error retrieving DHCP Server setting information"
5946 }
5947 ElseIf($Text)
5948 {
5949 Line 0 "Error retrieving DHCP Server setting information"
5950 }
5951 ElseIf($HTML)
5952 {
5953 }
5954}
5955InsertBlankLine
5956[gc]::collect()
5957
5958Write-Verbose "$(Get-Date): `tGetting IPv4 bindings"
5959$IPv4Bindings = Get-DHCPServerV4Binding -ComputerName $DHCPServerName -EA 0 | Sort-Object IPAddress
5960
5961If($? -and $Null -ne $IPv4Bindings)
5962{
5963 If($MSWord -or $PDF)
5964 {
5965 WriteWordLine 0 0 "Connections and server bindings"
5966 }
5967 ElseIf($Text)
5968 {
5969 Line 0 "Connections and server bindings"
5970 }
5971 ElseIf($HTML)
5972 {
5973 }
5974
5975 ForEach($IPv4Binding in $IPv4Bindings)
5976 {
5977 If($IPv4Binding.BindingState)
5978 {
5979 If($MSWord -or $PDF)
5980 {
5981 WriteWordLine 0 1 "Enabled " -NoNewLine
5982 }
5983 ElseIf($Text)
5984 {
5985 Line 1 "Enabled " -NoNewLine
5986 }
5987 ElseIf($HTML)
5988 {
5989 }
5990 }
5991 Else
5992 {
5993 If($MSWord -or $PDF)
5994 {
5995 WriteWordLine 0 1 "Disabled " -NoNewLine
5996 }
5997 ElseIf($Text)
5998 {
5999 Line 1 "Disabled " -NoNewLine
6000 }
6001 ElseIf($HTML)
6002 {
6003 }
6004 }
6005 If($MSWord -or $PDF)
6006 {
6007 WriteWordLine 0 0 "$($IPv4Binding.IPAddress) $($IPv4Binding.InterfaceAlias)"
6008 }
6009 ElseIf($Text)
6010 {
6011 Line 0 "$($IPv4Binding.IPAddress) $($IPv4Binding.InterfaceAlias)"
6012 }
6013 ElseIf($HTML)
6014 {
6015 }
6016 }
6017}
6018ElseIf(!$?)
6019{
6020 If($MSWord -or $PDF)
6021 {
6022 WriteWordLine 0 0 "Error retrieving IPv4 server bindings"
6023 }
6024 ElseIf($Text)
6025 {
6026 Line 0 "Error retrieving IPv4 server bindings"
6027 }
6028 ElseIf($HTML)
6029 {
6030 }
6031}
6032Else
6033{
6034 If($MSWord -or $PDF)
6035 {
6036 WriteWordLine 0 1 "There were no IPv4 server bindings"
6037 }
6038 ElseIf($Text)
6039 {
6040 Line 1 "There were no IPv4 server bindings"
6041 }
6042 ElseIf($HTML)
6043 {
6044 }
6045}
6046$IPv4Bindings = $Null
6047[gc]::collect()
6048
6049InsertBlankLine
6050
6051Write-Verbose "$(Get-Date): `tGetting IPv6 bindings"
6052$IPv6Bindings = Get-DHCPServerV6Binding -ComputerName $DHCPServerName -EA 0 | Sort-Object IPAddress
6053
6054If($? -and $Null -ne $IPv6Bindings)
6055{
6056 WriteWordLine 0 0 "Connections and server bindings:"
6057 ForEach($IPv6Binding in $IPv6Bindings)
6058 {
6059 If($IPv6Binding.BindingState)
6060 {
6061 If($MSWord -or $PDF)
6062 {
6063 WriteWordLine 0 1 "Enabled " -NoNewLine
6064 }
6065 ElseIf($Text)
6066 {
6067 Line 1 "Enabled " -NoNewLine
6068 }
6069 ElseIf($HTML)
6070 {
6071 }
6072 }
6073 Else
6074 {
6075 If($MSWord -or $PDF)
6076 {
6077 WriteWordLine 0 1 "Disabled " -NoNewLine
6078 }
6079 ElseIf($Text)
6080 {
6081 Line 1 "Disabled " -NoNewLine
6082 }
6083 ElseIf($HTML)
6084 {
6085 }
6086 }
6087 If($MSWord -or $PDF)
6088 {
6089 WriteWordLine 0 0 "$($IPv6Binding.IPAddress) $($IPv6Binding.InterfaceAlias)"
6090 }
6091 ElseIf($Text)
6092 {
6093 Line 0 "$($IPv6Binding.IPAddress) $($IPv6Binding.InterfaceAlias)"
6094 }
6095 ElseIf($HTML)
6096 {
6097 }
6098 }
6099}
6100ElseIf(!$?)
6101{
6102 If($MSWord -or $PDF)
6103 {
6104 WriteWordLine 0 0 "Error retrieving IPv6 server bindings"
6105 }
6106 ElseIf($Text)
6107 {
6108 Line 0 "Error retrieving IPv6 server bindings"
6109 }
6110 ElseIf($HTML)
6111 {
6112 }
6113}
6114Else
6115{
6116 If($MSWord -or $PDF)
6117 {
6118 WriteWordLine 0 1 "There were no IPv6 server bindings"
6119 }
6120 ElseIf($Text)
6121 {
6122 Line 1 "There were no IPv6 server bindings"
6123 }
6124 ElseIf($HTML)
6125 {
6126 }
6127}
6128$IPv6Bindings = $Null
6129[gc]::collect()
6130
6131If($MSWord -or $PDF)
6132{
6133 $selection.InsertNewPage()
6134 WriteWordLine 2 0 "IPv4"
6135 WriteWordLine 3 0 "Properties"
6136}
6137ElseIf($Text)
6138{
6139 Line 0 ""
6140 Line 0 "IPv4"
6141 Line 0 "Properties"
6142}
6143ElseIf($HTML)
6144{
6145}
6146
6147Write-Verbose "$(Get-Date): Getting IPv4 properties"
6148Write-Verbose "$(Get-Date): `tGetting IPv4 general settings"
6149If($MSWord -or $PDF)
6150{
6151 WriteWordLine 4 0 "General"
6152}
6153ElseIf($Text)
6154{
6155 Line 1 "General"
6156}
6157ElseIf($HTML)
6158{
6159}
6160
6161[bool]$GotAuditSettings = $False
6162$AuditSettings = Get-DHCPServerAuditLog -ComputerName $DHCPServerName -EA 0
6163
6164If($? -and $Null -ne $AuditSettings)
6165{
6166 $GotAuditSettings = $True
6167 If($AuditSettings.Enable)
6168 {
6169 If($MSWord -or $PDF)
6170 {
6171 WriteWordLine 0 1 "DHCP audit logging is enabled"
6172 }
6173 ElseIf($Text)
6174 {
6175 Line 2 "DHCP audit logging is enabled"
6176 }
6177 ElseIf($HTML)
6178 {
6179 }
6180 }
6181 Else
6182 {
6183 If($MSWord -or $PDF)
6184 {
6185 WriteWordLine 0 1 "DHCP audit logging is disabled"
6186 }
6187 ElseIf($Text)
6188 {
6189 Line 2 "DHCP audit logging is disabled"
6190 }
6191 ElseIf($HTML)
6192 {
6193 }
6194 }
6195}
6196ElseIf(!$?)
6197{
6198 If($MSWord -or $PDF)
6199 {
6200 WriteWordLine 0 0 "Error retrieving audit log settings"
6201 }
6202 ElseIf($Text)
6203 {
6204 Line 0 "Error retrieving audit log settings"
6205 }
6206 ElseIf($HTML)
6207 {
6208 }
6209}
6210Else
6211{
6212 If($MSWord -or $PDF)
6213 {
6214 WriteWordLine 0 0 "There were no audit log settings"
6215 }
6216 ElseIf($Text)
6217 {
6218 Line 0 "There were no audit log settings"
6219 }
6220 ElseIf($HTML)
6221 {
6222 }
6223}
6224[gc]::collect()
6225
6226#"HKLM:\SYSTEM\CurrentControlSet\Services\DHCPServer\Parameters" "BootFileTable"
6227
6228#Define the variable to hold the BOOTP Table
6229$BOOTPKey="SYSTEM\CurrentControlSet\Services\DHCPServer\Parameters"
6230
6231#Create an instance of the Registry Object and open the HKLM base key
6232$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$DHCPServerName)
6233
6234#Drill down into the BOOTP key using the OpenSubKey Method
6235$regkey1=$reg.OpenSubKey($BOOTPKey)
6236
6237#Retrieve an array of string that contain all the subkey names
6238If($Null -ne $regkey1)
6239{
6240 $BOOTPTable = $regkey1.GetValue("BootFileTable")
6241}
6242Else
6243{
6244 $BOOTPTable = $Null
6245}
6246
6247If($Null -ne $BOOTPTable)
6248{
6249 If($MSWord -or $PDF)
6250 {
6251 WriteWordLine 0 1 "Show the BOOTP table folder is enabled"
6252 }
6253 ElseIf($Text)
6254 {
6255 Line 2 "Show the BOOTP table folder is enabled"
6256 }
6257 ElseIf($HTML)
6258 {
6259 }
6260}
6261
6262#DNS settings
6263Write-Verbose "$(Get-Date): `tGetting IPv4 DNS settings"
6264If($MSWord -or $PDF)
6265{
6266 WriteWordLine 4 0 "DNS"
6267}
6268ElseIf($Text)
6269{
6270 Line 1 "DNS"
6271}
6272ElseIf($HTML)
6273{
6274}
6275
6276$DNSSettings = Get-DHCPServerV4DnsSetting -ComputerName $DHCPServerName -EA 0
6277If($? -and $Null -ne $DNSSettings)
6278{
6279 GetDNSSettings $DNSSettings "A"
6280}
6281Else
6282{
6283 If($MSWord -or $PDF)
6284 {
6285 WriteWordLine 0 0 "Error retrieving IPv4 DNS Settings for DHCP server $DHCPServerName"
6286 }
6287 ElseIf($Text)
6288 {
6289 Line 0 "Error retrieving IPv4 DNS Settings for DHCP server $DHCPServerName"
6290 }
6291 ElseIf($HTML)
6292 {
6293 }
6294}
6295$DNSSettings = $Null
6296[gc]::collect()
6297
6298#now back to some server settings
6299Write-Verbose "$(Get-Date): `tGetting IPv4 NAP settings"
6300If($MSWord -or $PDF)
6301{
6302 WriteWordLine 4 0 "Network Access Protection"
6303}
6304ElseIf($Text)
6305{
6306 Line 1 "Network Access Protection"
6307}
6308ElseIf($HTML)
6309{
6310}
6311
6312If($GotServerSettings)
6313{
6314 If($MSWord -or $PDF)
6315 {
6316 WriteWordLine 0 1 "Network Access Protection is " -NoNewLine
6317 If($ServerSettings.NapEnabled)
6318 {
6319 WriteWordLine 0 0 "Enabled on all scopes"
6320 }
6321 Else
6322 {
6323 WriteWordLine 0 0 "Disabled on all scopes"
6324 }
6325 WriteWordLine 0 1 "DHCP server behavior when NPS is unreachable: " -NoNewLine
6326 Switch($ServerSettings.NpsUnreachableAction)
6327 {
6328 "Full" {WriteWordLine 0 0 "Full Access"}
6329 "Restricted" {WriteWordLine 0 0 "Restricted Access"}
6330 "NoAccess" {WriteWordLine 0 0 "Drop Client Packet"}
6331 Default {WriteWordLine 0 0 "Unable to determine NPS unreachable action: $($ServerSettings.NpsUnreachableAction)"}
6332 }
6333 }
6334 ElseIf($Text)
6335 {
6336 Line 2 "Network Access Protection is " -NoNewLine
6337 If($ServerSettings.NapEnabled)
6338 {
6339 Line 0 "Enabled on all scopes"
6340 }
6341 Else
6342 {
6343 Line 0 "Disabled on all scopes"
6344 }
6345 Line 2 "DHCP server behavior when NPS is unreachable: " -NoNewLine
6346 Switch($ServerSettings.NpsUnreachableAction)
6347 {
6348 "Full" {Line 0 "Full Access"}
6349 "Restricted" {Line 0 "Restricted Access"}
6350 "NoAccess" {Line 0 "Drop Client Packet"}
6351 Default {Line 0 "Unable to determine NPS unreachable action: $($ServerSettings.NpsUnreachableAction)"}
6352 }
6353 }
6354 ElseIf($HTML)
6355 {
6356 }
6357}
6358
6359#filters
6360Write-Verbose "$(Get-Date): `tGetting IPv4 filters"
6361If($MSWord -or $PDF)
6362{
6363 WriteWordLine 4 0 "Filters"
6364}
6365ElseIf($Text)
6366{
6367 Line 1 "Filters"
6368}
6369ElseIf($HTML)
6370{
6371}
6372
6373$MACFilters = Get-DHCPServerV4FilterList -ComputerName $DHCPServerName -EA 0
6374
6375If($? -and $Null -ne $MACFilters)
6376{
6377 If($MSWord -or $PDF)
6378 {
6379 WriteWordLine 0 1 "Enable Allow list: " -NoNewLine
6380 If($MACFilters.Allow)
6381 {
6382 WriteWordLine 0 0 "Enabled"
6383 }
6384 Else
6385 {
6386 WriteWordLine 0 0 "Disabled"
6387 }
6388 WriteWordLine 0 1 "Enable Deny list: " -NoNewLine
6389 If($MACFilters.Deny)
6390 {
6391 WriteWordLine 0 0 "Enabled"
6392 }
6393 Else
6394 {
6395 WriteWordLine 0 0 "Disabled"
6396 }
6397 }
6398 ElseIf($Text)
6399 {
6400 Line 2 "Enable Allow list`t: " -NoNewLine
6401 If($MACFilters.Allow)
6402 {
6403 Line "Enabled"
6404 }
6405 Else
6406 {
6407 Line 0 "Disabled"
6408 }
6409 Line 2 "Enable Deny list`t: " -NoNewLine
6410 If($MACFilters.Deny)
6411 {
6412 Line 0 "Enabled"
6413 }
6414 Else
6415 {
6416 Line 0 "Disabled"
6417 }
6418 }
6419 ElseIf($HTML)
6420 {
6421 }
6422}
6423ElseIf(!$?)
6424{
6425 If($MSWord -or $PDF)
6426 {
6427 WriteWordLine 0 0 "Error retrieving MAC filters for DHCP server $DHCPServerName"
6428 }
6429 ElseIf($Text)
6430 {
6431 Line 0 "Error retrieving MAC filters for DHCP server $DHCPServerName"
6432 }
6433 ElseIf($HTML)
6434 {
6435 }
6436}
6437Else
6438{
6439 If($MSWord -or $PDF)
6440 {
6441 WriteWordLine 0 1 "There were no MAC filters for DHCP server $DHCPServerName"
6442 }
6443 ElseIf($Text)
6444 {
6445 Line 2 "There were no MAC filters for DHCP server $DHCPServerName"
6446 }
6447 ElseIf($HTML)
6448 {
6449 }
6450}
6451$MACFilters = $Null
6452[gc]::collect()
6453
6454#failover
6455Write-Verbose "$(Get-Date): `tGetting IPv4 Failover"
6456If($MSWord -or $PDF)
6457{
6458 WriteWordLine 4 0 "Failover"
6459}
6460ElseIf($Text)
6461{
6462 Line 1 "Failover"
6463}
6464ElseIf($HTML)
6465{
6466}
6467
6468$Failovers = Get-DHCPServerV4Failover -ComputerName $DHCPServerName -EA 0
6469
6470If($? -and $Null -ne $Failovers)
6471{
6472 If($MSWord -or $PDF)
6473 {
6474 $TableRange = $doc.Application.Selection.Range
6475 [int]$Columns = 2
6476 If($Failovers -is [array])
6477 {
6478 [int]$Rows = ($Failovers.Count * 8) - 1
6479 #subtract the very last row used for spacing
6480 }
6481 Else
6482 {
6483 [int]$Rows = 7
6484 }
6485 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
6486 $table.Style = $myHash.Word_TableGrid
6487 $table.Borders.InsideLineStyle = $wdLineStyleNone
6488 $table.Borders.OutsideLineStyle = $wdLineStyleNone
6489 [int]$xRow = 0
6490 ForEach($Failover in $Failovers)
6491 {
6492 Write-Verbose "$(Get-Date):`t`tProcessing failover $($Failover.Name)"
6493 $xRow++
6494 $Table.Cell($xRow,1).Range.Text = "Relationship name"
6495 $Table.Cell($xRow,2).Range.Text = $Failover.Name
6496
6497 $xRow++
6498 $Table.Cell($xRow,1).Range.Text = "State of the server"
6499 Switch($Failover.State)
6500 {
6501 "NoState"
6502 {
6503 $Table.Cell($xRow,2).Range.Text = "No State"
6504 }
6505 "Normal"
6506 {
6507 $Table.Cell($xRow,2).Range.Text = "Normal"
6508 }
6509 "Init"
6510 {
6511 $Table.Cell($xRow,2).Range.Text = "Iitializing"
6512 }
6513 "CommunicationInterrupted"
6514 {
6515 $Table.Cell($xRow,2).Range.Text = "Communication Interrupted"
6516 }
6517 "PartnerDown"
6518 {
6519 $Table.Cell($xRow,2).Range.Text = "Normal"
6520 }
6521 "PotentialConflict"
6522 {
6523 $Table.Cell($xRow,2).Range.Text = "Potential Conflict"
6524 }
6525 "Startup"
6526 {
6527 $Table.Cell($xRow,2).Range.Text = "Starting Up"
6528 }
6529 "ResolutionInterrupted"
6530 {
6531 $Table.Cell($xRow,2).Range.Text = "Resolution Interrupted"
6532 }
6533 "ConflictDone"
6534 {
6535 $Table.Cell($xRow,2).Range.Text = "Conflict Done"
6536 }
6537 "Recover"
6538 {
6539 $Table.Cell($xRow,2).Range.Text = "Recover"
6540 }
6541 "RecoverWait"
6542 {
6543 $Table.Cell($xRow,2).Range.Text = "Recover Wait"
6544 }
6545 "RecoverDone"
6546 {
6547 $Table.Cell($xRow,2).Range.Text = "Recover Done"
6548 }
6549 Default
6550 {
6551 $Table.Cell($xRow,2).Range.Text = "Unable to determine server state"
6552 }
6553 }
6554
6555 $xRow++
6556 $Table.Cell($xRow,1).Range.Text = "Partner Server"
6557 $Table.Cell($xRow,2).Range.Text = $Failover.PartnerServer
6558
6559 $xRow++
6560 $Table.Cell($xRow,1).Range.Text = "Mode"
6561 $Table.Cell($xRow,2).Range.Text = $Failover.Mode
6562
6563 $xRow++
6564 $Table.Cell($xRow,1).Range.Text = "Message Authentication"
6565 If($Failover.EnableAuth)
6566 {
6567 $Table.Cell($xRow,2).Range.Text = "Enabled"
6568 }
6569 Else
6570 {
6571 $Table.Cell($xRow,2).Range.Text = "Disabled"
6572 }
6573
6574 If($Failover.Mode -eq "LoadBalance")
6575 {
6576 $xRow++
6577 $Table.Cell($xRow,1).Range.Text = "Local server"
6578 $Table.Cell($xRow,2).Range.Text = "$($Failover.LoadBalancePercent)%"
6579
6580 $xRow++
6581 $Table.Cell($xRow,1).Range.Text = "Partner Server"
6582 $tmp = (100 - $($Failover.LoadBalancePercent))
6583 $Table.Cell($xRow,2).Range.Text = "$($tmp)%"
6584 $tmp = $Null
6585 }
6586 Else
6587 {
6588 $xRow++
6589 $Table.Cell($xRow,1).Range.Text = "Role of this server"
6590 $Table.Cell($xRow,2).Range.Text = $Failover.ServerRole
6591
6592 $xRow++
6593 $Table.Cell($xRow,1).Range.Text = "Addresses reserved for standby server"
6594 $Table.Cell($xRow,2).Range.Text = "$($Failover.ReservePercent)%"
6595 }
6596
6597 #skip a row for spacing
6598 $xRow++
6599 }
6600 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
6601 $table.AutoFitBehavior($wdAutoFitContent)
6602
6603 #return focus back to document
6604 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
6605
6606 #move to the end of the current document
6607 $selection.EndKey($wdStory,$wdMove) | Out-Null
6608 $TableRange = $Null
6609 $Table = $Null
6610 }
6611 ElseIf($Text)
6612 {
6613 ForEach($Failover in $Failovers)
6614 {
6615 Write-Verbose "$(Get-Date):`t`tProcessing failover $($Failover.Name)"
6616 Line 2 "Relationship name: " $Failover.Name
6617
6618 Line 2 "State of the server`t: " -NoNewLine
6619 Switch($Failover.State)
6620 {
6621 "NoState"
6622 {
6623 Line 0 "No State"
6624 }
6625 "Normal"
6626 {
6627 Line 0 "Normal"
6628 }
6629 "Init"
6630 {
6631 Line 0 "Iitializing"
6632 }
6633 "CommunicationInterrupted"
6634 {
6635 Line 0 "Communication Interrupted"
6636 }
6637 "PartnerDown"
6638 {
6639 Line 0 "Normal"
6640 }
6641 "PotentialConflict"
6642 {
6643 Line 0 "Potential Conflict"
6644 }
6645 "Startup"
6646 {
6647 Line 0 "Starting Up"
6648 }
6649 "ResolutionInterrupted"
6650 {
6651 Line 0 "Resolution Interrupted"
6652 }
6653 "ConflictDone"
6654 {
6655 Line 0 "Conflict Done"
6656 }
6657 "Recover"
6658 {
6659 Line 0 "Recover"
6660 }
6661 "RecoverWait"
6662 {
6663 Line 0 "Recover Wait"
6664 }
6665 "RecoverDone"
6666 {
6667 Line 0 "Recover Done"
6668 }
6669 Default
6670 {
6671 Line 0 "Unable to determine server state"
6672 }
6673 }
6674
6675 Line 2 "Partner Server`t`t: " $Failover.PartnerServer
6676 Line 2 "Mode`t`t`t: " $Failover.Mode
6677 Line 2 "Message Authentication`t: " -NoNewLine
6678 If($Failover.EnableAuth)
6679 {
6680 Line 0 "Enabled"
6681 }
6682 Else
6683 {
6684 Line 0 "Disabled"
6685 }
6686
6687 If($Failover.Mode -eq "LoadBalance")
6688 {
6689 $tmp = (100 - $($Failover.LoadBalancePercent))
6690 Line 2 "Local server`t`t: $($Failover.LoadBalancePercent)%"
6691 Line 2 "Partner Server`t`t: $($tmp)%"
6692 $tmp = $Null
6693 }
6694 Else
6695 {
6696 Line 2 "Role of this server`t: " $Failover.ServerRole
6697 Line 2 "Addresses reserved for standby server: $($Failover.ReservePercent)%"
6698 }
6699
6700 #skip a row for spacing
6701 Line 0 ""
6702 }
6703 }
6704 ElseIf($HTML)
6705 {
6706 }
6707}
6708Else
6709{
6710 If($MSWord -or $PDF)
6711 {
6712 WriteWordLine 0 1 "There was no Failover configured for DHCP server $DHCPServerName"
6713 }
6714 ElseIf($Text)
6715 {
6716 Line 2 "There was no Failover configured for DHCP server $DHCPServerName"
6717 }
6718 ElseIf($HTML)
6719 {
6720 }
6721}
6722$Failovers = $Null
6723[gc]::collect()
6724
6725#Advanced
6726Write-Verbose "$(Get-Date): `tGetting IPv4 advanced settings"
6727If($MSWord -or $PDF)
6728{
6729 WriteWordLine 4 0 "Advanced"
6730}
6731ElseIf($Text)
6732{
6733 Line 1 "Advanced"
6734}
6735ElseIf($HTML)
6736{
6737}
6738
6739If($GotServerSettings)
6740{
6741 If($MSWord -or $PDF)
6742 {
6743 WriteWordLine 0 1 "Conflict detection attempts: " $ServerSettings.ConflictDetectionAttempts
6744 }
6745 ElseIf($Text)
6746 {
6747 Line 2 "Conflict detection attempts`t: " $ServerSettings.ConflictDetectionAttempts
6748 }
6749 ElseIf($HTML)
6750 {
6751 }
6752}
6753
6754If($GotAuditSettings)
6755{
6756 If($MSWord -or $PDF)
6757 {
6758 WriteWordLine 0 1 "Audit log file path: " $AuditSettings.Path
6759 }
6760 ElseIf($Text)
6761 {
6762 Line 2 "Audit log file path`t`t: " $AuditSettings.Path
6763 }
6764 ElseIf($HTML)
6765 {
6766 }
6767}
6768
6769#added 18-Jan-2016
6770#get dns update credentials
6771Write-Verbose "$(Get-Date): `tGetting DNS dynamic update registration credentials"
6772$DNSUpdateSettings = Get-DhcpServerDnsCredential -ComputerName $DHCPServerName -EA 0
6773
6774If($? -and $Null -ne $DNSUpdateSettings)
6775{
6776 If($MSWord -or $PDF)
6777 {
6778 WriteWordLine 0 2 "DNS dynamic update registration credentials: "
6779 WriteWordLine 0 3 "User name: " $DNSUpdateSettings.UserName
6780 WriteWordLine 0 3 "Domain: " $DNSUpdateSettings.DomainName
6781 }
6782 ElseIf($Text)
6783 {
6784 Line 2 "DNS dynamic update registration credentials: "
6785 Line 3 "User name`t: " $DNSUpdateSettings.UserName
6786 Line 3 "Domain`t`t: " $DNSUpdateSettings.DomainName
6787 }
6788 ElseIf($HTML)
6789 {
6790 }
6791}
6792ElseIf(!$?)
6793{
6794 If($MSWord -or $PDF)
6795 {
6796 WriteWordLine 0 0 "Error retrieving DNS Update Credentials for DHCP server $DHCPServerName"
6797 }
6798 ElseIf($Text)
6799 {
6800 Line 0 "Error retrieving DNS Update Credentials for DHCP server $DHCPServerName"
6801 }
6802 ElseIf($HTML)
6803 {
6804 }
6805}
6806Else
6807{
6808 If($MSWord -or $PDF)
6809 {
6810 WriteWordLine 0 1 "There were no DNS Update Credentials for DHCP server $DHCPServerName"
6811 }
6812 ElseIf($Text)
6813 {
6814 Line 2 "There were no DNS Update Credentials for DHCP server $DHCPServerName"
6815 }
6816 ElseIf($HTML)
6817 {
6818 }
6819}
6820$DNSUpdateSettings = $Null
6821[gc]::collect()
6822
6823If($MSWord -or $PDF)
6824{
6825 WriteWordLine 4 0 "Statistics"
6826}
6827ElseIf($Text)
6828{
6829 Line 1 "Statistics"
6830}
6831ElseIf($HTML)
6832{
6833}
6834[gc]::collect()
6835
6836$Statistics = Get-DHCPServerV4Statistics -ComputerName $DHCPServerName -EA 0
6837
6838If($? -and $Null -ne $Statistics)
6839{
6840 $UpTime = $(Get-Date) - $Statistics.ServerStartTime
6841 $Str = [string]::format("{0} days, {1} hours, {2} minutes, {3} seconds", `
6842 $UpTime.Days, `
6843 $UpTime.Hours, `
6844 $UpTime.Minutes, `
6845 $UpTime.Seconds)
6846 [int]$InUsePercent = "{0:N0}" -f $Statistics.PercentageInUse
6847 [int]$AvailablePercent = "{0:N0}" -f $Statistics.PercentageAvailable
6848
6849 If($MSWord -or $PDF)
6850 {
6851 $TableRange = $doc.Application.Selection.Range
6852 [int]$Columns = 2
6853 [int]$Rows = 16
6854 Write-Verbose "$(Get-Date): `tAdd IPv4 statistics table to doc"
6855 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
6856 $table.Style = $myHash.Word_TableGrid
6857 $table.Borders.InsideLineStyle = $wdLineStyleSingle
6858 $table.Borders.OutsideLineStyle = $wdLineStyleSingle
6859 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
6860 $Table.Cell(1,1).Range.Font.Bold = $True
6861 $Table.Cell(1,1).Range.Text = "Description"
6862 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
6863 $Table.Cell(1,2).Range.Font.Bold = $True
6864 $Table.Cell(1,2).Range.Text = "Details"
6865
6866 $Table.Cell(2,1).Range.Text = "Start Time"
6867 $Table.Cell(2,2).Range.Text = $Statistics.ServerStartTime.ToString()
6868 $Table.Cell(3,1).Range.Text = "Up Time"
6869 $Table.Cell(3,2).Range.Text = $Str
6870 $Table.Cell(4,1).Range.Text = "Discovers"
6871 $Table.Cell(4,2).Range.Text = $Statistics.Discovers.ToString()
6872 $Table.Cell(5,1).Range.Text = "Offers"
6873 $Table.Cell(5,2).Range.Text = $Statistics.Offers.ToString()
6874 $Table.Cell(6,1).Range.Text = "Delayed Offers"
6875 $Table.Cell(6,2).Range.Text = $Statistics.DelayedOffers.ToString()
6876 $Table.Cell(7,1).Range.Text = "Requests"
6877 $Table.Cell(7,2).Range.Text = $Statistics.Requests.ToString()
6878 $Table.Cell(8,1).Range.Text = "Acks"
6879 $Table.Cell(8,2).Range.Text = $Statistics.Acks.ToString()
6880 $Table.Cell(9,1).Range.Text = "Nacks"
6881 $Table.Cell(9,2).Range.Text = $Statistics.Naks.ToString()
6882 $Table.Cell(10,1).Range.Text = "Declines"
6883 $Table.Cell(10,2).Range.Text = $Statistics.Declines.ToString()
6884 $Table.Cell(11,1).Range.Text = "Releases"
6885 $Table.Cell(11,2).Range.Text = $Statistics.Releases.ToString()
6886 $Table.Cell(12,1).Range.Text = "Total Scopes"
6887 $Table.Cell(12,2).Range.Text = $Statistics.TotalScopes.ToString()
6888 $Table.Cell(13,1).Range.Text = "Scopes with delay configured"
6889 $Table.Cell(13,2).Range.Text = $Statistics.ScopesWithDelayConfigured.ToString()
6890 $Table.Cell(14,1).Range.Text = "Total Addresses"
6891 $Table.Cell(14,2).Range.Text = "{0:N0}" -f $Statistics.TotalAddresses.ToString()
6892 $Table.Cell(15,1).Range.Text = "In Use"
6893 $Table.Cell(15,2).Range.Text = "$($Statistics.AddressesInUse) ($($InUsePercent))%"
6894 $Table.Cell(16,1).Range.Text = "Available"
6895 $Table.Cell(16,2).Range.Text = "{0:N0}" -f "$($Statistics.AddressesAvailable) ($($AvailablePercent))%"
6896
6897 $table.AutoFitBehavior($wdAutoFitContent)
6898
6899 #return focus back to document
6900 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
6901
6902 #move to the end of the current document
6903 $selection.EndKey($wdStory,$wdMove) | Out-Null
6904 $TableRange = $Null
6905 $Table = $Null
6906 Write-Verbose "$(Get-Date): Finished IPv4 statistics table"
6907 Write-Verbose "$(Get-Date): "
6908 }
6909 ElseIf($Text)
6910 {
6911 Line 2 "Description" -NoNewLine
6912 Line 3 "Details"
6913
6914 Line 2 "Start Time:" -NoNewLine
6915 Line 3 $Statistics.ServerStartTime
6916 Line 2 "Up Time:" -NoNewLine
6917 Line 3 $Str
6918 Line 2 "Discovers:" -NoNewLine
6919 Line 3 $Statistics.Discovers
6920 Line 2 "Offers:" -NoNewLine
6921 Line 4 $Statistics.Offers
6922 Line 2 "Delayed Offers:" -NoNewLine
6923 Line 3 $Statistics.DelayedOffers
6924 Line 2 "Requests:" -NoNewLine
6925 Line 3 $Statistics.Requests
6926 Line 2 "Acks:" -NoNewLine
6927 Line 4 $Statistics.Acks
6928 Line 2 "Nacks:" -NoNewLine
6929 Line 4 $Statistics.Naks
6930 Line 2 "Declines:" -NoNewLine
6931 Line 3 $Statistics.Declines
6932 Line 2 "Releases:" -NoNewLine
6933 Line 3 $Statistics.Releases
6934 Line 2 "Total Scopes:" -NoNewLine
6935 Line 3 $Statistics.TotalScopes
6936 Line 2 "Scopes w/delay configured:" -NoNewLine
6937 Line 1 $Statistics.ScopesWithDelayConfigured
6938 Line 2 "Total Addresses:" -NoNewLine
6939 $tmp = "{0:N0}" -f $Statistics.TotalAddresses
6940 Line 2 $tmp
6941 Line 2 "In Use:" -NoNewLine
6942 Line 4 "$($Statistics.AddressesInUse) ($($InUsePercent))%"
6943 Line 2 "Available:" -NoNewLine
6944 $tmp = "{0:N0}" -f $Statistics.AddressesAvailable
6945 Line 3 "$($tmp) ($($AvailablePercent))%"
6946 }
6947 ElseIf($HTML)
6948 {
6949 }
6950}
6951ElseIf(!$?)
6952{
6953 If($MSWord -or $PDF)
6954 {
6955 WriteWordLine 0 0 "Error retrieving IPv4 statistics"
6956 }
6957 ElseIf($Text)
6958 {
6959 Line 0 "Error retrieving IPv4 statistics"
6960 }
6961 ElseIf($HTML)
6962 {
6963 }
6964}
6965Else
6966{
6967 If($MSWord -or $PDF)
6968 {
6969 WriteWordLine 0 0 "There were no IPv4 statistics"
6970 }
6971 ElseIf($Text)
6972 {
6973 Line 0 "There were no IPv4 statistics"
6974 }
6975 ElseIf($HTML)
6976 {
6977 }
6978}
6979$Statistics = $Null
6980[gc]::collect()
6981
6982Write-Verbose "$(Get-Date):Build array of Allow/Deny filters"
6983$Filters = Get-DHCPServerV4Filter -ComputerName $DHCPServerName -EA 0
6984
6985Write-Verbose "$(Get-Date): Getting IPv4 Superscopes"
6986$IPv4Superscopes = Get-DHCPServerV4Superscope -ComputerName $DHCPServerName -EA 0
6987
6988If($? -and $Null -ne $IPv4Superscopes)
6989{
6990 ForEach($IPv4Superscope in $IPv4Superscopes)
6991 {
6992 If(![string]::IsNullOrEmpty($IPv4Superscope.SuperscopeName))
6993 {
6994 If($MSWord -or $PDF)
6995 {
6996 #put each superscope on a new page
6997 $selection.InsertNewPage()
6998 Write-Verbose "$(Get-Date): `tGetting IPv4 superscope data for scope $($IPv4Superscope.SuperscopeName)"
6999 WriteWordLine 3 0 "Superscope [$($IPv4Superscope.SuperscopeName)]"
7000
7001 #get superscope statistics first
7002 $Statistics = Get-DHCPServerV4SuperscopeStatistics -ComputerName $DHCPServerName -Name $IPv4Superscope.SuperscopeName -EA 0
7003
7004 If($? -and $Null -ne $Statistics)
7005 {
7006 GetShortStatistics $Statistics
7007 }
7008 ElseIf(!$?)
7009 {
7010 WriteWordLine 0 0 "Error retrieving superscope statistics"
7011 }
7012 Else
7013 {
7014 WriteWordLine 0 1 "There were no statistics for the superscope"
7015 }
7016 $Statistics = $Null
7017
7018 $xScopeIds = $IPv4Superscope.ScopeId
7019 [int]$StartLevel = 4
7020 ForEach($xScopeId in $xScopeIds)
7021 {
7022 Write-Verbose "$(Get-Date): Processing scope id $($xScopeId) for Superscope $($IPv4Superscope.SuperscopeName)"
7023 $IPv4Scope = Get-DHCPServerV4Scope -ComputerName $DHCPServerName -ScopeId $xScopeId -EA 0
7024
7025 If($? -and $Null -ne $IPv4Scope)
7026 {
7027 GetIPv4ScopeData $IPv4Scope $StartLevel
7028 }
7029 Else
7030 {
7031 WriteWordLine 0 0 "Error retrieving Superscope data for scope $($xScopeId)"
7032 }
7033 }
7034 }
7035 ElseIf($Text)
7036 {
7037 Write-Verbose "$(Get-Date): `tGetting IPv4 superscope data for scope $($IPv4Superscope.SuperscopeName)"
7038 Line 0 ""
7039 Line 0 "Superscope [$($IPv4Superscope.SuperscopeName)]"
7040
7041 #get superscope statistics first
7042 $Statistics = Get-DHCPServerV4SuperscopeStatistics -ComputerName $DHCPServerName -Name $IPv4Superscope.SuperscopeName -EA 0
7043
7044 If($? -and $Null -ne $Statistics)
7045 {
7046 Line 1 "Statistics:"
7047 GetShortStatistics $Statistics
7048 }
7049 ElseIf(!$?)
7050 {
7051 Line 0 "Error retrieving superscope statistics"
7052 }
7053 Else
7054 {
7055 Line 2 "There were no statistics for the superscope"
7056 }
7057 $Statistics = $Null
7058
7059 $xScopeIds = $IPv4Superscope.ScopeId
7060 [int]$StartLevel = 4
7061 ForEach($xScopeId in $xScopeIds)
7062 {
7063 Write-Verbose "$(Get-Date): Processing scope id $($xScopeId) for Superscope $($IPv4Superscope.SuperscopeName)"
7064 $IPv4Scope = Get-DHCPServerV4Scope -ComputerName $DHCPServerName -ScopeId $xScopeId -EA 0
7065
7066 If($? -and $Null -ne $IPv4Scope)
7067 {
7068 GetIPv4ScopeData $IPv4Scope $StartLevel
7069 }
7070 Else
7071 {
7072 Line 0 "Error retrieving Superscope data for scope $($xScopeId)"
7073 }
7074 }
7075 }
7076 ElseIf($HTML)
7077 {
7078 }
7079 }
7080 }
7081}
7082ElseIf(!$?)
7083{
7084 If($MSWord -or $PDF)
7085 {
7086 WriteWordLine 0 0 "Error retrieving IPv4 Superscopes"
7087 }
7088 ElseIf($Text)
7089 {
7090 Line 0 "Error retrieving IPv4 Superscopes"
7091 }
7092 ElseIf($HTML)
7093 {
7094 }
7095}
7096Else
7097{
7098 If($MSWord -or $PDF)
7099 {
7100 WriteWordLine 0 1 "There were no IPv4 Superscopes"
7101 }
7102 ElseIf($Text)
7103 {
7104 Line 2 "There were no IPv4 Superscopes"
7105 }
7106 ElseIf($HTML)
7107 {
7108 }
7109}
7110$IPv4Superscopes = $Null
7111[gc]::collect()
7112
7113Write-Verbose "$(Get-Date): Getting IPv4 scopes"
7114$IPv4Scopes = Get-DHCPServerV4Scope -ComputerName $DHCPServerName -EA 0
7115
7116If($? -and $Null -ne $IPv4Scopes)
7117{
7118 [int]$StartLevel = 3
7119 ForEach($IPv4Scope in $IPv4Scopes)
7120 {
7121 GetIPv4ScopeData $IPv4Scope $StartLevel
7122 }
7123}
7124ElseIf(!$?)
7125{
7126 If($MSWord -or $PDF)
7127 {
7128 WriteWordLine 0 0 "Error retrieving IPv4 scopes"
7129 }
7130 ElseIf($Text)
7131 {
7132 Line 0 "Error retrieving IPv4 scopes"
7133 }
7134 ElseIf($HTML)
7135 {
7136 }
7137}
7138Else
7139{
7140 If($MSWord -or $PDF)
7141 {
7142 WriteWordLine 0 1 "There were no IPv4 scopes"
7143 }
7144 ElseIf($Text)
7145 {
7146 Line 2 "There were no IPv4 scopes"
7147 }
7148 ElseIf($HTML)
7149 {
7150 }
7151}
7152$IPv4Scopes = $Null
7153$Filters = $Null
7154[gc]::collect()
7155
7156$CmdletName = "Get-DHCPServerV4MulticastScope"
7157If(Get-Command $CmdletName -Module "DHCPServer" -EA 0)
7158{
7159 Write-Verbose "$(Get-Date): Getting IPv4 Multicast scopes"
7160 $IPv4MulticastScopes = Get-DHCPServerV4MulticastScope -ComputerName $DHCPServerName -EA 0
7161
7162 If($? -and $Null -ne $IPv4MulticastScopes)
7163 {
7164 ForEach($IPv4MulticastScope in $IPv4MulticastScopes)
7165 {
7166 If($Null -ne $IPv4MulticastScope.LeaseDuration)
7167 {
7168 $DurationStr = [string]::format("{0} days, {1} hours, {2} minutes", `
7169 $IPv4MulticastScope.LeaseDuration.Day, `
7170 $IPv4MulticastScope.LeaseDuration.Hour, `
7171 $IPv4MulticastScope.LeaseDuration.Minute)
7172 }
7173 Else
7174 {
7175 $DurationStr = "Unlimited"
7176 }
7177
7178 If($MSWord -or $PDF)
7179 {
7180 #put each scope on a new page
7181 $selection.InsertNewPage()
7182 Write-Verbose "$(Get-Date): `tGetting IPv4 multicast scope data for scope $($IPv4MulticastScope.Name)"
7183 WriteWordLine 3 0 "Multicast Scope [$($IPv4MulticastScope.Name)]"
7184 WriteWordLine 4 0 "General"
7185 $TableRange = $doc.Application.Selection.Range
7186 [int]$Columns = 2
7187 [int]$Rows = 6
7188 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
7189 $table.Style = $myHash.Word_TableGrid
7190 $table.Borders.InsideLineStyle = $wdLineStyleNone
7191 $table.Borders.OutsideLineStyle = $wdLineStyleNone
7192 $Table.Cell(1,1).Range.Text = "Name"
7193 $Table.Cell(1,2).Range.Text = $IPv4MulticastScope.Name
7194 $Table.Cell(2,1).Range.Text = "Start IP address"
7195 $Table.Cell(2,2).Range.Text = $IPv4MulticastScope.StartRange
7196 $Table.Cell(3,1).Range.Text = "End IP address"
7197 $Table.Cell(3,2).Range.Text = $IPv4MulticastScope.EndRange
7198 $Table.Cell(4,1).Range.Text = "Time to live"
7199 $Table.Cell(4,2).Range.Text = $IPv4MulticastScope.Ttl
7200 $Table.Cell(5,1).Range.Text = "Lease duration"
7201 $Table.Cell(5,2).Range.Text = $DurationStr
7202 $Table.Cell(6,1).Range.Text = "Description"
7203 $Table.Cell(6,2).Range.Text = $IPv4MulticastScope.Description
7204
7205 $table.AutoFitBehavior($wdAutoFitContent)
7206
7207 #return focus back to document
7208 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
7209
7210 #move to the end of the current document
7211 $selection.EndKey($wdStory,$wdMove) | Out-Null
7212 $TableRange = $Null
7213 $Table = $Null
7214
7215 WriteWordLine 4 0 "Lifetime"
7216 WriteWordLine 0 1 "Multicast scope lifetime: " -NoNewLine
7217 If([string]::IsNullOrEmpty($IPv4MulticastScope.ExpiryTime))
7218 {
7219 WriteWordLine 0 0 "Infinite"
7220 }
7221 Else
7222 {
7223 WriteWordLine 0 0 "Multicast scope expires on $($IPv4MulticastScope.ExpiryTime)"
7224 }
7225
7226 Write-Verbose "$(Get-Date):`t`tGetting exclusions"
7227 WriteWordLine 4 0 "Exclusions"
7228 $Exclusions = Get-DHCPServerV4MulticastExclusionRange -ComputerName $DHCPServerName -Name $IPv4MulticastScope.Name -EA 0
7229 If($? -and $Null -ne $Exclusions)
7230 {
7231 $TableRange = $doc.Application.Selection.Range
7232 [int]$Columns = 2
7233 If($Exclusions -is [array])
7234 {
7235 [int]$Rows = $Exclusions.Count + 1
7236 }
7237 Else
7238 {
7239 [int]$Rows = 2
7240 }
7241 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
7242 $table.Style = $myHash.Word_TableGrid
7243 $table.Borders.InsideLineStyle = $wdLineStyleNone
7244 $table.Borders.OutsideLineStyle = $wdLineStyleNone
7245 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
7246 $Table.Cell(1,1).Range.Font.Bold = $True
7247 $Table.Cell(1,1).Range.Text = "Start IP Address"
7248 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
7249 $Table.Cell(1,2).Range.Font.Bold = $True
7250 $Table.Cell(1,2).Range.Text = "End IP Address"
7251 [int]$xRow = 1
7252 ForEach($Exclusion in $Exclusions)
7253 {
7254 $xRow++
7255 $Table.Cell($xRow,1).Range.Text = $Exclusion.StartRange
7256 $Table.Cell($xRow,2).Range.Text = $Exclusion.EndRange
7257 }
7258 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
7259 $table.AutoFitBehavior($wdAutoFitContent)
7260
7261 #return focus back to document
7262 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
7263
7264 #move to the end of the current document
7265 $selection.EndKey($wdStory,$wdMove) | Out-Null
7266 $TableRange = $Null
7267 $Table = $Null
7268 }
7269 ElseIf(!$?)
7270 {
7271 WriteWordLine 0 0 "Error retrieving exclusions for multicast scope"
7272 }
7273 Else
7274 {
7275 WriteWordLine 0 1 "<None>"
7276 }
7277
7278 #leases
7279 If($IncludeLeases)
7280 {
7281 Write-Verbose "$(Get-Date):`t`tGetting leases"
7282
7283 WriteWordLine 4 0 "Address Leases"
7284 $Leases = Get-DHCPServerV4MulticastLease -ComputerName $DHCPServerName -Name $IPv4MulticastScope.Name -EA 0 | Sort-Object IPAddress
7285 If($? -and $Null -ne $Leases)
7286 {
7287 $TableRange = $doc.Application.Selection.Range
7288 [int]$Columns = 2
7289 If($Leases -is [array])
7290 {
7291 [int]$Rows = ($Leases.Count * 7) - 1
7292 #subtract the very last row used for spacing
7293 }
7294 Else
7295 {
7296 [int]$Rows = 6
7297 }
7298 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
7299 $table.Style = $myHash.Word_TableGrid
7300 $table.Borders.InsideLineStyle = $wdLineStyleNone
7301 $table.Borders.OutsideLineStyle = $wdLineStyleNone
7302 [int]$xRow = 0
7303 ForEach($Lease in $Leases)
7304 {
7305 Write-Verbose "$(Get-Date):`t`t`tProcessing lease $($Lease.IPAddress)"
7306 If($Null -ne $Lease.LeaseExpiryTime)
7307 {
7308 $LeaseEndStr = [string]::format("{0} days, {1} hours, {2} minutes", `
7309 $Lease.LeaseExpiryTime.Day, `
7310 $Lease.LeaseExpiryTime.Hour, `
7311 $Lease.LeaseExpiryTime.Minute)
7312 }
7313 Else
7314 {
7315 $LeaseEndStr = ""
7316 }
7317
7318 If($Null -ne $Lease.LeaseExpiryTime)
7319 {
7320 $LeaseStartStr = [string]::format("{0} days, {1} hours, {2} minutes", `
7321 $Lease.LeaseStartTime.Day, `
7322 $Lease.LeaseStartTime.Hour, `
7323 $Lease.LeaseStartTime.Minute)
7324 }
7325 Else
7326 {
7327 $LeaseStartStr = ""
7328 }
7329
7330 $xRow++
7331 $Table.Cell($xRow,1).Range.Text = "Client IP address"
7332 $Table.Cell($xRow,2).Range.Text = $Lease.IPAddress
7333
7334 $xRow++
7335 $Table.Cell($xRow,1).Range.Text = "Name"
7336 $Table.Cell($xRow,2).Range.Text = $Lease.HostName
7337
7338 $xRow++
7339 $Table.Cell($xRow,1).Range.Text = "Lease Expiration"
7340 If([string]::IsNullOrEmpty($Lease.LeaseExpiryTime))
7341 {
7342 $Table.Cell($xRow,2).Range.Text = "Unlimited"
7343 }
7344 Else
7345 {
7346 $Table.Cell($xRow,2).Range.Text = $LeaseEndStr
7347 }
7348
7349 $xRow++
7350 $Table.Cell($xRow,1).Range.Text = "Lease Start"
7351 If([string]::IsNullOrEmpty($Lease.LeaseStartTime))
7352 {
7353 $Table.Cell($xRow,2).Range.Text = "Unlimited"
7354 }
7355 Else
7356 {
7357 $Table.Cell($xRow,2).Range.Text = $LeaseStartStr
7358 }
7359
7360 $xRow++
7361 $Table.Cell($xRow,1).Range.Text = "Address State"
7362 $Table.Cell($xRow,2).Range.Text = $Lease.AddressState
7363
7364 $xRow++
7365 $Table.Cell($xRow,1).Range.Text = "MAC address"
7366 $Table.Cell($xRow,2).Range.Text = $Lease.ClientID
7367
7368 #skip a row for spacing
7369 $xRow++
7370 }
7371 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
7372 $table.AutoFitBehavior($wdAutoFitContent)
7373
7374 #return focus back to document
7375 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
7376
7377 #move to the end of the current document
7378 $selection.EndKey($wdStory,$wdMove) | Out-Null
7379 $TableRange = $Null
7380 $Table = $Null
7381 }
7382 ElseIf(!$?)
7383 {
7384 WriteWordLine 0 0 "Error retrieving leases for scope"
7385 }
7386 Else
7387 {
7388 WriteWordLine 0 1 "<None>"
7389 }
7390 $Leases = $Null
7391 }
7392
7393 Write-Verbose "$(Get-Date):`t`tGetting Multicast Scope statistics"
7394 WriteWordLine 4 0 "Statistics"
7395
7396 $Statistics = Get-DHCPServerV4MulticastScopeStatistics -ComputerName $DHCPServerName -Name $IPv4MulticastScope.Name -EA 0
7397
7398 If($? -and $Null -ne $Statistics)
7399 {
7400 GetShortStatistics $Statistics
7401 }
7402 ElseIf(!$?)
7403 {
7404 WriteWordLine 0 0 "Error retrieving multicast scope statistics"
7405 }
7406 Else
7407 {
7408 WriteWordLine 0 1 "<None>"
7409 }
7410 $Statistics = $Null
7411 }
7412 ElseIf($Text)
7413 {
7414 Write-Verbose "$(Get-Date): `tGetting IPv4 multicast scope data for scope $($IPv4MulticastScope.Name)"
7415 Line 0 "Multicast Scope [$($IPv4MulticastScope.Name)]"
7416 Line 1 "General:"
7417 Line 2 "Name`t`t`t: " $IPv4MulticastScope.Name
7418 Line 2 "Start IP address`t: " $IPv4MulticastScope.StartRange
7419 Line 2 "End IP address`t`t: " $IPv4MulticastScope.EndRange
7420 Line 2 "Time to live`t`t: " $IPv4MulticastScope.Ttl
7421 Line 2 "Lease duration`t`t: " $DurationStr
7422 If(![string]::IsNullOrEmpty($IPv4MulticastScope.Description))
7423 {
7424 Line 2 "Description`t`t: " $IPv4MulticastScope.Description
7425 }
7426
7427 Line 1 "Lifetime:"
7428 Line 2 "Multicast scope lifetime: " -NoNewLine
7429 If([string]::IsNullOrEmpty($IPv4MulticastScope.ExpiryTime))
7430 {
7431 Line 0 "Infinite"
7432 }
7433 Else
7434 {
7435 Line 0 "Multicast scope expires on $($IPv4MulticastScope.ExpiryTime)"
7436 }
7437
7438 Write-Verbose "$(Get-Date):`t`tGetting exclusions"
7439 Line 1 "Exclusions:"
7440 $Exclusions = Get-DHCPServerV4MulticastExclusionRange -ComputerName $DHCPServerName -Name $IPv4MulticastScope.Name -EA 0
7441 If($? -and $Null -ne $Exclusions)
7442 {
7443 Line 2 "Start IP Address`tEnd IP Address"
7444 ForEach($Exclusion in $Exclusions)
7445 {
7446 Line 2 $Exclusion.StartRange -NoNewLine
7447 Line 2 $Exclusion.EndRange
7448 }
7449 }
7450 ElseIf(!$?)
7451 {
7452 Line 0 "Error retrieving exclusions for multicast scope"
7453 }
7454 Else
7455 {
7456 Line 2 "<None>"
7457 }
7458
7459 #leases
7460 If($IncludeLeases)
7461 {
7462 Write-Verbose "$(Get-Date):`t`tGetting leases"
7463
7464 Line 1 "Address Leases:"
7465 $Leases = Get-DHCPServerV4MulticastLease -ComputerName $DHCPServerName -Name $IPv4MulticastScope.Name -EA 0 | Sort-Object IPAddress
7466 If($? -and $Null -ne $Leases)
7467 {
7468 ForEach($Lease in $Leases)
7469 {
7470 Write-Verbose "$(Get-Date):`t`t`tProcessing lease $($Lease.IPAddress)"
7471 If($Null -ne $Lease.LeaseExpiryTime)
7472 {
7473 $LeaseEndStr = [string]::format("{0} days, {1} hours, {2} minutes", `
7474 $Lease.LeaseExpiryTime.Day, `
7475 $Lease.LeaseExpiryTime.Hour, `
7476 $Lease.LeaseExpiryTime.Minute)
7477 }
7478 Else
7479 {
7480 $LeaseEndStr = ""
7481 }
7482
7483 If($Null -ne $Lease.LeaseExpiryTime)
7484 {
7485 $LeaseStartStr = [string]::format("{0} days, {1} hours, {2} minutes", `
7486 $Lease.LeaseStartTime.Day, `
7487 $Lease.LeaseStartTime.Hour, `
7488 $Lease.LeaseStartTime.Minute)
7489 }
7490 Else
7491 {
7492 $LeaseStartStr = ""
7493 }
7494
7495 Line 2 "Client IP address`t: " $Lease.IPAddress
7496 Line 2 "Name`t`t`t: " $Lease.HostName
7497 Line 2 "Lease Expiration`t: " -NoNewLine
7498 If([string]::IsNullOrEmpty($Lease.LeaseExpiryTime))
7499 {
7500 Line 0 "Unlimited"
7501 }
7502 Else
7503 {
7504 Line 0 $LeaseEndStr
7505 }
7506
7507 Line 2 "Lease Start`t`t: " -NoNewLine
7508 If([string]::IsNullOrEmpty($Lease.LeaseStartTime))
7509 {
7510 Line 0 "Unlimited"
7511 }
7512 Else
7513 {
7514 Line 0 $LeaseStartStr
7515 }
7516
7517 Line 2 "Address State`t`t: " $Lease.AddressState
7518 Line 2 "MAC address`t: " $Lease.ClientID
7519
7520 #skip a row for spacing
7521 Line 0 ""
7522 }
7523 }
7524 ElseIf(!$?)
7525 {
7526 Line 0 "Error retrieving leases for scope"
7527 }
7528 Else
7529 {
7530 Line 2 "<None>"
7531 }
7532 $Leases = $Null
7533 }
7534
7535 Write-Verbose "$(Get-Date):`t`tGetting Multicast Scope statistics"
7536 Line 1 "Statistics:"
7537
7538 $Statistics = Get-DHCPServerV4MulticastScopeStatistics -ComputerName $DHCPServerName -Name $IPv4MulticastScope.Name -EA 0
7539
7540 If($? -and $Null -ne $Statistics)
7541 {
7542 GetShortStatistics $Statistics
7543 }
7544 ElseIf(!$?)
7545 {
7546 Line 0 "Error retrieving multicast scope statistics"
7547 }
7548 Else
7549 {
7550 Line 2 "<None>"
7551 }
7552 $Statistics = $Null
7553 }
7554 ElseIf($HTML)
7555 {
7556 }
7557 }
7558 }
7559 ElseIf(!$?)
7560 {
7561 If($MSWord -or $PDF)
7562 {
7563 WriteWordLine 0 0 "Error retrieving IPv4 Multicast scopes"
7564 }
7565 ElseIf($Text)
7566 {
7567 Line 0 "Error retrieving IPv4 Multicast scopes"
7568 }
7569 ElseIf($HTML)
7570 {
7571 }
7572 }
7573 Else
7574 {
7575 If($MSWord -or $PDF)
7576 {
7577 WriteWordLine 0 1 "There were no IPv4 Multicast scopes"
7578 }
7579 ElseIf($Text)
7580 {
7581 Line 2 "There were no IPv4 Multicast scopes"
7582 }
7583 ElseIf($HTML)
7584 {
7585 }
7586 }
7587 $IPv4MulticastScopes = $Null
7588}
7589[gc]::collect()
7590
7591#bootp table
7592If($Null -ne $BOOTPTable)
7593{
7594 Write-Verbose "$(Get-Date):IPv4 BOOTP Table"
7595
7596 If($MSWord -or $PDF)
7597 {
7598
7599 $selection.InsertNewPage()
7600 WriteWordLine 3 0 "BOOTP Table"
7601
7602 $TableRange = $doc.Application.Selection.Range
7603 [int]$Columns = 3
7604 If($BOOTPTable -is [array])
7605 {
7606 [int]$Rows = $BOOTPTable.Count + 1
7607 }
7608 Else
7609 {
7610 [int]$Rows = 2
7611 }
7612 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
7613 $table.Style = $myHash.Word_TableGrid
7614 $table.Borders.InsideLineStyle = $wdLineStyleNone
7615 $table.Borders.OutsideLineStyle = $wdLineStyleNone
7616 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
7617 $Table.Cell(1,1).Range.Font.Bold = $True
7618 $Table.Cell(1,1).Range.Text = "Boot Image"
7619 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
7620 $Table.Cell(1,2).Range.Font.Bold = $True
7621 $Table.Cell(1,2).Range.Text = "File Name"
7622 $Table.Cell(1,3).Shading.BackgroundPatternColor = $wdColorGray15
7623 $Table.Cell(1,3).Range.Font.Bold = $True
7624 $Table.Cell(1,3).Range.Text = "File Server"
7625 [int]$xRow = 1
7626 ForEach($Item in $BOOTPTable)
7627 {
7628 $xRow++
7629 $ItemParts = $Item.Split(",")
7630 $Table.Cell($xRow,1).Range.Text = $ItemParts[0]
7631 $Table.Cell($xRow,2).Range.Text = $ItemParts[1]
7632 $Table.Cell($xRow,3).Range.Text = $ItemParts[2]
7633 }
7634
7635 $table.AutoFitBehavior($wdAutoFitContent)
7636
7637 #return focus back to document
7638 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
7639
7640 #move to the end of the current document
7641 $selection.EndKey($wdStory,$wdMove) | Out-Null
7642 $TableRange = $Null
7643 $Table = $Null
7644 }
7645 ElseIf($Text)
7646 {
7647 Line 1 "BOOTP Table"
7648
7649 ForEach($Item in $BOOTPTable)
7650 {
7651 $ItemParts = $Item.Split(",")
7652 Line 2 "Boot Image`t: " $ItemParts[0]
7653 Line 2 "File Name`t: " $ItemParts[1]
7654 Line 2 "FIle Server`t: " $ItemParts[2]
7655 Line 0 ""
7656 }
7657 }
7658 ElseIf($HTML)
7659 {
7660 }
7661}
7662[gc]::collect()
7663
7664#Server Options
7665Write-Verbose "$(Get-Date):Getting IPv4 server options"
7666
7667If($MSWord -or $PDF)
7668{
7669 $selection.InsertNewPage()
7670 WriteWordLine 3 0 "Server Options"
7671}
7672ElseIf($Text)
7673{
7674 Line 1 "Server Options"
7675}
7676ElseIf($HTML)
7677{
7678}
7679
7680$ServerOptions = Get-DHCPServerV4OptionValue -All -ComputerName $DHCPServerName -EA 0 | Where {$_.OptionID -ne 81} | Sort-Object OptionId
7681
7682If($? -and $Null -ne $ServerOptions)
7683{
7684 If($MSWord -or $PDF)
7685 {
7686 $TableRange = $doc.Application.Selection.Range
7687 [int]$Columns = 2
7688 If($ServerOptions -is [array])
7689 {
7690 [int]$Rows = ($ServerOptions.Count * 5)
7691 }
7692 Else
7693 {
7694 [int]$Rows = 4
7695 }
7696 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
7697 $table.Style = $myHash.Word_TableGrid
7698 $table.Borders.InsideLineStyle = $wdLineStyleNone
7699 $table.Borders.OutsideLineStyle = $wdLineStyleNone
7700 [int]$xRow = 0
7701 ForEach($ServerOption in $ServerOptions)
7702 {
7703 Write-Verbose "$(Get-Date):`t`t`tProcessing option name $($ServerOption.Name)"
7704 $xRow++
7705 $Table.Cell($xRow,1).Range.Text = "Option Name"
7706 $Table.Cell($xRow,2).Range.Text = "$($ServerOption.OptionId.ToString("000")) $($ServerOption.Name)"
7707
7708 $xRow++
7709 $Table.Cell($xRow,1).Range.Text = "Vendor"
7710 If([string]::IsNullOrEmpty($ServerOption.VendorClass))
7711 {
7712 $Table.Cell($xRow,2).Range.Text = "Standard"
7713 }
7714 Else
7715 {
7716 $Table.Cell($xRow,2).Range.Text = $ServerOption.VendorClass
7717 }
7718
7719 $xRow++
7720 $Table.Cell($xRow,1).Range.Text = "Value"
7721 $Table.Cell($xRow,2).Range.Text = $ServerOption.Value.ToString()
7722
7723 $xRow++
7724 $Table.Cell($xRow,1).Range.Text = "Policy Name"
7725 If([string]::IsNullOrEmpty($ServerOption.PolicyName))
7726 {
7727 $Table.Cell($xRow,2).Range.Text = "<None>"
7728 }
7729 Else
7730 {
7731 $Table.Cell($xRow,2).Range.Text = $ServerOption.PolicyName
7732 }
7733 #for spacing
7734 $xRow++
7735 }
7736 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
7737 $table.AutoFitBehavior($wdAutoFitContent)
7738
7739 #return focus back to document
7740 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
7741
7742 #move to the end of the current document
7743 $selection.EndKey($wdStory,$wdMove) | Out-Null
7744 $TableRange = $Null
7745 $Table = $Null
7746 }
7747 ElseIf($Text)
7748 {
7749 ForEach($ServerOption in $ServerOptions)
7750 {
7751 Write-Verbose "$(Get-Date):`t`t`tProcessing option name $($ServerOption.Name)"
7752 Line 2 "Option Name`t: $($ServerOption.OptionId.ToString("000")) $($ServerOption.Name)"
7753 Line 2 "Vendor`t`t: " -NoNewLine
7754 If([string]::IsNullOrEmpty($ServerOption.VendorClass))
7755 {
7756 Line 0 "Standard"
7757 }
7758 Else
7759 {
7760 Line 0 $ServerOption.VendorClass
7761 }
7762
7763 Line 2 "Value`t`t: " $ServerOption.Value
7764 Line 2 "Policy Name`t: " -NoNewLine
7765 If([string]::IsNullOrEmpty($ServerOption.PolicyName))
7766 {
7767 Line 0 "<None>"
7768 }
7769 Else
7770 {
7771 Line 0 $ServerOption.PolicyName
7772 }
7773 #for spacing
7774 Line 0 ""
7775 }
7776 }
7777 ElseIf($HTML)
7778 {
7779 }
7780}
7781ElseIf($? -and $Null -eq $ServerOptions)
7782{
7783 If($MSWord -or $PDF)
7784 {
7785 WriteWordLine 0 1 "There were no IPv4 server options"
7786 }
7787 ElseIf($Text)
7788 {
7789 Line 2 "There were no IPv4 server options"
7790 }
7791 ElseIf($HTML)
7792 {
7793 }
7794}
7795Else
7796{
7797 If($MSWord -or $PDF)
7798 {
7799 WriteWordLine 0 0 "Error retrieving IPv4 server options"
7800 }
7801 ElseIf($Text)
7802 {
7803 Line 0 "Error retrieving IPv4 server options"
7804 }
7805 ElseIf($HTML)
7806 {
7807 }
7808}
7809$ServerOptions = $Null
7810[gc]::collect()
7811
7812#Policies
7813Write-Verbose "$(Get-Date):Getting IPv4 policies"
7814If($MSWord -or $PDF)
7815{
7816 WriteWordLine 3 0 "Policies"
7817}
7818ElseIf($Text)
7819{
7820 Line 1 "Policies"
7821}
7822ElseIf($HTML)
7823{
7824}
7825
7826$Policies = Get-DHCPServerV4Policy -ComputerName $DHCPServerName -EA 0 | Sort-Object ProcessingOrder
7827
7828If($? -and $Null -ne $Policies)
7829{
7830 If($MSWord -or $PDF)
7831 {
7832 $TableRange = $doc.Application.Selection.Range
7833 [int]$Columns = 2
7834 If($Policies -is [array])
7835 {
7836 [int]$Rows = $Policies.Count * 6
7837 }
7838 Else
7839 {
7840 [int]$Rows = 5
7841 }
7842 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
7843 $table.Style = $myHash.Word_TableGrid
7844 $table.Borders.InsideLineStyle = $wdLineStyleNone
7845 $table.Borders.OutsideLineStyle = $wdLineStyleNone
7846 [int]$xRow = 0
7847 ForEach($Policy in $Policies)
7848 {
7849 $xRow++
7850 $Table.Cell($xRow,1).Range.Text = "Policy Name"
7851 $Table.Cell($xRow,2).Range.Text = $Policy.Name
7852
7853 $xRow++
7854 $Table.Cell($xRow,1).Range.Text = "Description"
7855 $Table.Cell($xRow,2).Range.Text = $Policy.Description
7856
7857 $xRow++
7858 $Table.Cell($xRow,1).Range.Text = "Processing Order"
7859 $Table.Cell($xRow,2).Range.Text = $Policy.ProcessingOrder
7860
7861 $xRow++
7862 $Table.Cell($xRow,1).Range.Text = "Level"
7863 $Table.Cell($xRow,2).Range.Text = "Server"
7864
7865 $xRow++
7866 $Table.Cell($xRow,1).Range.Text = "State"
7867 If($Policy.Enabled)
7868 {
7869 $Table.Cell($xRow,2).Range.Text = "Enabled"
7870 }
7871 Else
7872 {
7873 $Table.Cell($xRow,2).Range.Text = "Disabled"
7874 }
7875 #for spacing
7876 $xRow++
7877 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
7878 $table.AutoFitBehavior($wdAutoFitContent)
7879
7880 #return focus back to document
7881 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
7882
7883 #move to the end of the current document
7884 $selection.EndKey($wdStory,$wdMove) | Out-Null
7885 $TableRange = $Null
7886 $Table = $Null
7887 }
7888 }
7889 ElseIf($Text)
7890 {
7891 ForEach($Policy in $Policies)
7892 {
7893 Line 2 "Policy Name`t`t: " $Policy.Name
7894 If(![string]::IsNullOrEmpty($Policy.Description))
7895 {
7896 Line 2 "Description`t`t: " $Policy.Description
7897 }
7898 Line 2 "Processing Order`t: " $Policy.ProcessingOrder
7899 Line 2 "Level`t`t`t: Server"
7900 Line 2 "State`t`t`t: " -NoNewLine
7901 If($Policy.Enabled)
7902 {
7903 Line 0 "Enabled"
7904 }
7905 Else
7906 {
7907 Line 0 "Disabled"
7908 }
7909 #for spacing
7910 Line 0 ""
7911 }
7912 }
7913 ElseIf($HTML)
7914 {
7915 }
7916}
7917ElseIf(!$?)
7918{
7919 If($MSWord -or $PDF)
7920 {
7921 WriteWordLine 0 0 "Error retrieving IPv4 policies"
7922 }
7923 ElseIf($Text)
7924 {
7925 Line 0 "Error retrieving IPv4 policies"
7926 }
7927 ElseIf($HTML)
7928 {
7929 }
7930}
7931Else
7932{
7933 If($MSWord -or $PDF)
7934 {
7935 WriteWordLine 0 1 "There were no IPv4 policies"
7936 }
7937 ElseIf($Text)
7938 {
7939 Line 2 "There were no IPv4 policies"
7940 }
7941 ElseIf($HTML)
7942 {
7943 }
7944}
7945$Policies = $Null
7946[gc]::collect()
7947
7948#Filters
7949Write-Verbose "$(Get-Date):Getting IPv4 filters"
7950If($MSWord -or $PDF)
7951{
7952 WriteWordLine 3 0 "Filters"
7953}
7954ElseIf($Text)
7955{
7956 Line 1 "Filters"
7957}
7958ElseIf($HTML)
7959{
7960}
7961
7962Write-Verbose "$(Get-Date):`tAllow filters"
7963$AllowFilters = Get-DHCPServerV4Filter -List Allow -ComputerName $DHCPServerName -EA 0 | Sort-Object MacAddress
7964
7965If($? -and $Null -ne $AllowFilters)
7966{
7967 If($MSWord -or $PDF)
7968 {
7969 WriteWordLine 4 0 "Allow"
7970 $TableRange = $doc.Application.Selection.Range
7971 [int]$Columns = 2
7972 If($AllowFilters -is [array])
7973 {
7974 [int]$Rows = $AllowFilters.Count + 1
7975 }
7976 Else
7977 {
7978 [int]$Rows = 2
7979 }
7980 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
7981 $table.Style = $myHash.Word_TableGrid
7982 $table.Borders.InsideLineStyle = $wdLineStyleNone
7983 $table.Borders.OutsideLineStyle = $wdLineStyleNone
7984 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
7985 $Table.Cell(1,1).Range.Font.Bold = $True
7986 $Table.Cell(1,1).Range.Text = "MAC Address"
7987 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
7988 $Table.Cell(1,2).Range.Font.Bold = $True
7989 $Table.Cell(1,2).Range.Text = "Description"
7990 [int]$xRow = 1
7991 ForEach($AllowFilter in $AllowFilters)
7992 {
7993 $xRow++
7994 $Table.Cell($xRow,1).Range.Text = $AllowFilter.MacAddress
7995 $Table.Cell($xRow,2).Range.Text = $AllowFilter.Description
7996 }
7997 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
7998 $table.AutoFitBehavior($wdAutoFitContent)
7999
8000 #return focus back to document
8001 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
8002
8003 #move to the end of the current document
8004 $selection.EndKey($wdStory,$wdMove) | Out-Null
8005 $TableRange = $Null
8006 $Table = $Null
8007 }
8008 ElseIf($Text)
8009 {
8010 Line 2 "Allow"
8011 ForEach($AllowFilter in $AllowFilters)
8012 {
8013 Line 3 "MAC Address`t: " $AllowFilter.MacAddress
8014 Line 3 "Description`t: " $AllowFilter.Description
8015 Line 0 ""
8016 }
8017 }
8018 ElseIf($HTML)
8019 {
8020 }
8021}
8022ElseIf(!$?)
8023{
8024 If($MSWord -or $PDF)
8025 {
8026 WriteWordLine 0 0 "Error retrieving IPv4 allow filters"
8027 }
8028 ElseIf($Text)
8029 {
8030 Line 0 "Error retrieving IPv4 allow filters"
8031 }
8032 ElseIf($HTML)
8033 {
8034 }
8035}
8036Else
8037{
8038 If($MSWord -or $PDF)
8039 {
8040 WriteWordLine 0 1 "There were no IPv4 allow filters"
8041 }
8042 ElseIf($Text)
8043 {
8044 Line 2 "There were no IPv4 allow filters"
8045 }
8046 ElseIf($HTML)
8047 {
8048 }
8049}
8050$AllowFilters = $Null
8051[gc]::collect()
8052
8053Write-Verbose "$(Get-Date):`tDeny filters"
8054$DenyFilters = Get-DHCPServerV4Filter -List Deny -ComputerName $DHCPServerName -EA 0 | Sort-Object MacAddress
8055If($? -and $Null -ne $DenyFilters)
8056{
8057 If($MSWord -or $PDF)
8058 {
8059 WriteWordLine 4 0 "Deny"
8060 $TableRange = $doc.Application.Selection.Range
8061 [int]$Columns = 2
8062 If($DenyFilters -is [array])
8063 {
8064 [int]$Rows = $DenyFilters.Count + 1
8065 }
8066 Else
8067 {
8068 [int]$Rows = 2
8069 }
8070 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
8071 $table.Style = $myHash.Word_TableGrid
8072 $table.Borders.InsideLineStyle = $wdLineStyleNone
8073 $table.Borders.OutsideLineStyle = $wdLineStyleNone
8074 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
8075 $Table.Cell(1,1).Range.Font.Bold = $True
8076 $Table.Cell(1,1).Range.Text = "MAC Address"
8077 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
8078 $Table.Cell(1,2).Range.Font.Bold = $True
8079 $Table.Cell(1,2).Range.Text = "Description"
8080 [int]$xRow = 1
8081
8082 ForEach($DenyFilter in $DenyFilters)
8083 {
8084 $xRow++
8085 $Table.Cell($xRow,1).Range.Text = $DenyFilter.MacAddress
8086 $Table.Cell($xRow,2).Range.Text = $DenyFilter.Description
8087 }
8088 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
8089 $table.AutoFitBehavior($wdAutoFitContent)
8090
8091 #return focus back to document
8092 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
8093
8094 #move to the end of the current document
8095 $selection.EndKey($wdStory,$wdMove) | Out-Null
8096 $TableRange = $Null
8097 $Table = $Null
8098 }
8099 ElseIf($Text)
8100 {
8101 Line 2 "Deny"
8102 ForEach($DenyFilter in $DenyFilters)
8103 {
8104 Line 3 "MAC Address`t: " $DenyFilter.MacAddress
8105 Line 3 "Description`t: " $DenyFilter.Description
8106 Line 0 ""
8107 }
8108 }
8109 ElseIf($HTML)
8110 {
8111 }
8112}
8113ElseIf(!$?)
8114{
8115 If($MSWord -or $PDF)
8116 {
8117 WriteWordLine 0 0 "Error retrieving IPv4 deny filters"
8118 }
8119 ElseIf($Text)
8120 {
8121 Line 0 "Error retrieving IPv4 deny filters"
8122 }
8123 ElseIf($HTML)
8124 {
8125 }
8126}
8127Else
8128{
8129 If($MSWord -or $PDF)
8130 {
8131 WriteWordLine 0 1 "There were no IPv4 deny filters"
8132 }
8133 ElseIf($Text)
8134 {
8135 Line 2 "There were no IPv4 deny filters"
8136 }
8137 ElseIf($HTML)
8138 {
8139 }
8140}
8141$DenyFilters = $Null
8142[gc]::collect()
8143
8144#IPv6
8145
8146If($MSWord -or $PDF)
8147{
8148 $selection.InsertNewPage()
8149 WriteWordLine 2 0 "IPv6"
8150 WriteWordLine 3 0 "Properties"
8151
8152 Write-Verbose "$(Get-Date): Getting IPv6 properties"
8153 Write-Verbose "$(Get-Date): `tGetting IPv6 general settings"
8154 WriteWordLine 4 0 "General"
8155
8156 If($GotAuditSettings)
8157 {
8158 If($AuditSettings.Enable)
8159 {
8160 WriteWordLine 0 1 "DHCP audit logging is enabled"
8161 }
8162 Else
8163 {
8164 WriteWordLine 0 1 "DHCP audit logging is disabled"
8165 }
8166 }
8167 ElseIf(!$?)
8168 {
8169 WriteWordLine 0 0 "Error retrieving audit log settings"
8170 }
8171 Else
8172 {
8173 WriteWordLine 0 1 "There were no audit log settings"
8174 }
8175
8176 #DNS settings
8177 Write-Verbose "$(Get-Date): `tGetting IPv6 DNS settings"
8178 WriteWordLine 4 0 "DNS"
8179 $DNSSettings = Get-DHCPServerV6DnsSetting -ComputerName $DHCPServerName -EA 0
8180 If($? -and $Null -ne $DNSSettings)
8181 {
8182 GetDNSSettings $DNSSettings "AAAA"
8183 }
8184 Else
8185 {
8186 WriteWordLine 0 0 "Error retrieving IPv6 DNS Settings for DHCP server $DHCPServerName"
8187 }
8188 $DNSSettings = $Null
8189
8190 #Advanced
8191 Write-Verbose "$(Get-Date): `tGetting IPv6 advanced settings"
8192 WriteWordLine 4 0 "Advanced"
8193 If($GotAuditSettings)
8194 {
8195 WriteWordLine 0 1 "Audit log file path " $AuditSettings.Path
8196 }
8197 $AuditSettings = $Null
8198
8199 #added 18-Jan-2016
8200 #get dns update credentials
8201 Write-Verbose "$(Get-Date): `tGetting DNS dynamic update registration credentials"
8202 $DNSUpdateSettings = Get-DhcpServerDnsCredential -ComputerName $DHCPServerName -EA 0
8203
8204 If($? -and $Null -ne $DNSUpdateSettings)
8205 {
8206 If($MSWord -or $PDF)
8207 {
8208 WriteWordLine 0 2 "DNS dynamic update registration credentials: "
8209 WriteWordLine 0 3 "User name: " $DNSUpdateSettings.UserName
8210 WriteWordLine 0 3 "Domain: " $DNSUpdateSettings.DomainName
8211 }
8212 ElseIf($Text)
8213 {
8214 Line 2 "DNS dynamic update registration credentials: "
8215 Line 3 "User name`t: " $DNSUpdateSettings.UserName
8216 Line 3 "Domain`t`t: " $DNSUpdateSettings.DomainName
8217 }
8218 ElseIf($HTML)
8219 {
8220 }
8221 }
8222 ElseIf(!$?)
8223 {
8224 If($MSWord -or $PDF)
8225 {
8226 WriteWordLine 0 0 "Error retrieving DNS Update Credentials for DHCP server $DHCPServerName"
8227 }
8228 ElseIf($Text)
8229 {
8230 Line 0 "Error retrieving DNS Update Credentials for DHCP server $DHCPServerName"
8231 }
8232 ElseIf($HTML)
8233 {
8234 }
8235 }
8236 Else
8237 {
8238 If($MSWord -or $PDF)
8239 {
8240 WriteWordLine 0 1 "There were no DNS Update Credentials for DHCP server $DHCPServerName"
8241 }
8242 ElseIf($Text)
8243 {
8244 Line 2 "There were no DNS Update Credentials for DHCP server $DHCPServerName"
8245 }
8246 ElseIf($HTML)
8247 {
8248 }
8249 }
8250 $DNSUpdateSettings = $Null
8251 [gc]::collect()
8252
8253 WriteWordLine 4 0 "Statistics"
8254 $Statistics = Get-DHCPServerV6Statistics -ComputerName $DHCPServerName -EA 0
8255
8256 If($? -and $Null -ne $Statistics)
8257 {
8258 $UpTime = $(Get-Date) - $Statistics.ServerStartTime
8259 $Str = [string]::format("{0} days, {1} hours, {2} minutes, {3} seconds", `
8260 $UpTime.Days, `
8261 $UpTime.Hours, `
8262 $UpTime.Minutes, `
8263 $UpTime.Seconds)
8264
8265 $TableRange = $doc.Application.Selection.Range
8266 [int]$Columns = 2
8267 [int]$Rows = 16
8268 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
8269 $table.Style = $myHash.Word_TableGrid
8270 $table.Borders.InsideLineStyle = $wdLineStyleSingle
8271 $table.Borders.OutsideLineStyle = $wdLineStyleSingle
8272 $Table.Cell(1,1).Shading.BackgroundPatternColor = $wdColorGray15
8273 $Table.Cell(1,1).Range.Font.Bold = $True
8274 $Table.Cell(1,1).Range.Text = "Description"
8275 $Table.Cell(1,2).Shading.BackgroundPatternColor = $wdColorGray15
8276 $Table.Cell(1,2).Range.Font.Bold = $True
8277 $Table.Cell(1,2).Range.Text = "Details"
8278
8279 $Table.Cell(2,1).Range.Text = "Start Time"
8280 $Table.Cell(2,2).Range.Text = $Statistics.ServerStartTime.ToString()
8281 $Table.Cell(3,1).Range.Text = "Up Time"
8282 $Table.Cell(3,2).Range.Text = $Str
8283 $Table.Cell(4,1).Range.Text = "Solicits"
8284 $Table.Cell(4,2).Range.Text = $Statistics.Solicits.ToString()
8285 $Table.Cell(5,1).Range.Text = "Advertises"
8286 $Table.Cell(5,2).Range.Text = $Statistics.Advertises.ToString()
8287 $Table.Cell(6,1).Range.Text = "Requests"
8288 $Table.Cell(6,2).Range.Text = $Statistics.Requests.ToString()
8289 $Table.Cell(7,1).Range.Text = "Replies"
8290 $Table.Cell(7,2).Range.Text = $Statistics.Replies.ToString()
8291 $Table.Cell(8,1).Range.Text = "Renews"
8292 $Table.Cell(8,2).Range.Text = $Statistics.Renews.ToString()
8293 $Table.Cell(9,1).Range.Text = "Rebinds"
8294 $Table.Cell(9,2).Range.Text = $Statistics.Rebinds.ToString()
8295 $Table.Cell(10,1).Range.Text = "Confirms"
8296 $Table.Cell(10,2).Range.Text = $Statistics.Confirms.ToString()
8297 $Table.Cell(11,1).Range.Text = "Declines"
8298 $Table.Cell(11,2).Range.Text = $Statistics.Declines.ToString()
8299 $Table.Cell(12,1).Range.Text = "Releases"
8300 $Table.Cell(12,2).Range.Text = $Statistics.Releases.ToString()
8301 $Table.Cell(13,1).Range.Text = "Total Scopes"
8302 $Table.Cell(13,2).Range.Text = $Statistics.TotalScopes.ToString()
8303 $Table.Cell(14,1).Range.Text = "Total Addresses"
8304 $tmp = "{0:N0}" -f $Statistics.TotalAddresses
8305 $Table.Cell(14,2).Range.Text = $tmp
8306 $Table.Cell(15,1).Range.Text = "In Use"
8307 [int]$InUsePercent = "{0:N0}" -f $Statistics.PercentageInUse.ToString()
8308 $Table.Cell(15,2).Range.Text = "$($Statistics.AddressesInUse) ($($InUsePercent)%)"
8309 $Table.Cell(16,1).Range.Text = "Available"
8310 [int]$AvailablePercent = "{0:N0}" -f $Statistics.PercentageAvailable.ToString()
8311 $tmp = "{0:N0}" -f $Statistics.AddressesAvailable
8312 $Table.Cell(16,2).Range.Text = "$($tmp) ($($AvailablePercent)%)"
8313
8314 $Table.Rows.SetLeftIndent($Indent1TabStops,$wdAdjustNone)
8315 $table.AutoFitBehavior($wdAutoFitContent)
8316
8317 #return focus back to document
8318 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
8319
8320 #move to the end of the current document
8321 $selection.EndKey($wdStory,$wdMove) | Out-Null
8322 $TableRange = $Null
8323 $Table = $Null
8324 }
8325 ElseIf(!$?)
8326 {
8327 WriteWordLine 0 0 "Error retrieving IPv6 statistics"
8328 }
8329 Else
8330 {
8331 WriteWordLine 0 0 "There were no IPv6 statistics"
8332 }
8333 $Statistics = $Null
8334
8335 Write-Verbose "$(Get-Date):Getting IPv6 scopes"
8336 $IPv6Scopes = Get-DHCPServerV6Scope -ComputerName $DHCPServerName -EA 0
8337
8338 If($? -and $Null -ne $IPv6Scopes)
8339 {
8340 $selection.InsertNewPage()
8341 ForEach($IPv6Scope in $IPv6Scopes)
8342 {
8343 GetIPv6ScopeData $IPv6Scope
8344 }
8345 }
8346 ElseIf(!$?)
8347 {
8348 WriteWordLine 0 0 "Error retrieving IPv6 scopes"
8349 }
8350 Else
8351 {
8352 WriteWordLine 0 1 "There were no IPv6 scopes"
8353 }
8354 $IPv6Scopes = $Null
8355
8356 Write-Verbose "$(Get-Date):Getting IPv6 server options"
8357 $selection.InsertNewPage()
8358 WriteWordLine 3 0 "Server Options"
8359
8360 $ServerOptions = Get-DHCPServerV6OptionValue -All -ComputerName $DHCPServerName -EA 0 | Sort-Object OptionId
8361
8362 If($? -and $Null -ne $ServerOptions)
8363 {
8364 $TableRange = $doc.Application.Selection.Range
8365 [int]$Columns = 2
8366 If($ServerOptions -is [array])
8367 {
8368 [int]$Rows = $ServerOptions.Count * 4
8369 }
8370 Else
8371 {
8372 [int]$Rows = 3
8373 }
8374 $Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
8375 $table.Style = $myHash.Word_TableGrid
8376 $table.Borders.InsideLineStyle = $wdLineStyleNone
8377 $table.Borders.OutsideLineStyle = $wdLineStyleNone
8378 [int]$xRow = 0
8379 ForEach($ServerOption in $ServerOptions)
8380 {
8381 $xRow++
8382 $Table.Cell($xRow,1).Range.Text = "Option Name"
8383 $Table.Cell($xRow,2).Range.Text = "$($ServerOption.OptionId.ToString("00000")) $($ServerOption.Name)"
8384
8385 $xRow++
8386 $Table.Cell($xRow,1).Range.Text = "Vendor"
8387 If([string]::IsNullOrEmpty($ServerOption.VendorClass))
8388 {
8389 $Table.Cell($xRow,2).Range.Text = "Standard"
8390 }
8391 Else
8392 {
8393 $Table.Cell($xRow,2).Range.Text = $ServerOption.VendorClass
8394 }
8395
8396 $xRow++
8397 $Table.Cell($xRow,1).Range.Text = "Value"
8398 $Table.Cell($xRow,2).Range.Text = $ServerOption.Value
8399
8400 #for spacing
8401 $xRow++
8402 }
8403 $table.AutoFitBehavior($wdAutoFitContent)
8404
8405 #return focus back to document
8406 $doc.ActiveWindow.ActivePane.view.SeekView = $wdSeekMainDocument
8407
8408 #move to the end of the current document
8409 $selection.EndKey($wdStory,$wdMove) | Out-Null
8410 $TableRange = $Null
8411 $Table = $Null
8412 }
8413 ElseIf(!$?)
8414 {
8415 WriteWordLine 0 0 "Error retrieving IPv6 server options"
8416 }
8417 Else
8418 {
8419 WriteWordLine 0 1 "There were no IPv6 server options"
8420 }
8421 $ServerOptions = $Null
8422}
8423ElseIf($Text)
8424{
8425 Line 0 "IPv6"
8426 Line 0 "Properties"
8427
8428 Write-Verbose "$(Get-Date): Getting IPv6 properties"
8429 Write-Verbose "$(Get-Date): `tGetting IPv6 general settings"
8430 Line 1 "General"
8431
8432 If($GotAuditSettings)
8433 {
8434 If($AuditSettings.Enable)
8435 {
8436 Line 2 "DHCP audit logging is enabled"
8437 }
8438 Else
8439 {
8440 Line 2 "DHCP audit logging is disabled"
8441 }
8442 }
8443 ElseIf(!$?)
8444 {
8445 Line 0 "Error retrieving audit log settings"
8446 }
8447 Else
8448 {
8449 Line 2 "There were no audit log settings"
8450 }
8451
8452 #DNS settings
8453 Write-Verbose "$(Get-Date): `tGetting IPv6 DNS settings"
8454 Line 1 "DNS"
8455 $DNSSettings = Get-DHCPServerV6DnsSetting -ComputerName $DHCPServerName -EA 0
8456 If($? -and $Null -ne $DNSSettings)
8457 {
8458 GetDNSSettings $DNSSettings "AAAA"
8459 }
8460 Else
8461 {
8462 Line 0 "Error retrieving IPv6 DNS Settings for DHCP server $DHCPServerName"
8463 }
8464 $DNSSettings = $Null
8465
8466 #Advanced
8467 Write-Verbose "$(Get-Date): `tGetting IPv6 advanced settings"
8468 Line 1 "Advanced"
8469 If($GotAuditSettings)
8470 {
8471 Line 2 "Audit log file path " $AuditSettings.Path
8472 }
8473 $AuditSettings = $Null
8474
8475 #added 18-Jan-2016
8476 #get dns update credentials
8477 Write-Verbose "$(Get-Date): `tGetting DNS dynamic update registration credentials"
8478 $DNSUpdateSettings = Get-DhcpServerDnsCredential -ComputerName $DHCPServerName -EA 0
8479
8480 If($? -and $Null -ne $DNSUpdateSettings)
8481 {
8482 Line 2 "DNS dynamic update registration credentials: "
8483 Line 3 "User name`t: " $DNSUpdateSettings.UserName
8484 Line 3 "Domain`t`t: " $DNSUpdateSettings.DomainName
8485 }
8486 ElseIf(!$?)
8487 {
8488 Line 0 "Error retrieving DNS Update Credentials for DHCP server $DHCPServerName"
8489 }
8490 Else
8491 {
8492 Line 2 "There were no DNS Update Credentials for DHCP server $DHCPServerName"
8493 }
8494 $DNSUpdateSettings = $Null
8495 [gc]::collect()
8496
8497 Line 1 "Statistics"
8498 $Statistics = Get-DHCPServerV6Statistics -ComputerName $DHCPServerName -EA 0
8499
8500 If($? -and $Null -ne $Statistics)
8501 {
8502 $UpTime = $(Get-Date) - $Statistics.ServerStartTime
8503 $Str = [string]::format("{0} days, {1} hours, {2} minutes, {3} seconds", `
8504 $UpTime.Days, `
8505 $UpTime.Hours, `
8506 $UpTime.Minutes, `
8507 $UpTime.Seconds)
8508 [int]$InUsePercent = "{0:N0}" -f $Statistics.PercentageInUse
8509 [int]$AvailablePercent = "{0:N0}" -f $Statistics.PercentageAvailable
8510
8511 Line 2 "Description" -NoNewLine
8512 Line 2 "Details"
8513
8514 Line 2 "Start Time: " -NoNewLine
8515 Line 2 $Statistics.ServerStartTime
8516 Line 2 "Up Time: " -NoNewLine
8517 Line 2 $Str
8518 Line 2 "Solicits: " -NoNewLine
8519 Line 2 $Statistics.Solicits
8520 Line 2 "Advertises: " -NoNewLine
8521 Line 2 $Statistics.Advertises
8522 Line 2 "Requests: " -NoNewLine
8523 Line 2 $Statistics.Requests
8524 Line 2 "Replies: " -NoNewLine
8525 Line 2 $Statistics.Replies
8526 Line 2 "Renews: " -NoNewLine
8527 Line 2 $Statistics.Renews
8528 Line 2 "Rebinds: " -NoNewLine
8529 Line 2 $Statistics.Rebinds
8530 Line 2 "Confirms: " -NoNewLine
8531 Line 2 $Statistics.Confirms
8532 Line 2 "Declines: " -NoNewLine
8533 Line 2 $Statistics.Declines
8534 Line 2 "Releases: " -NoNewLine
8535 Line 2 $Statistics.Releases
8536 Line 2 "Total Scopes: " -NoNewLine
8537 Line 2 $Statistics.TotalScopes
8538 Line 2 "Total Addresses: " -NoNewLine
8539 $tmp = "{0:N0}" -f $Statistics.TotalAddresses
8540 Line 1 $tmp
8541 Line 2 "In Use: " -NoNewLine
8542 Line 2 "$($Statistics.AddressesInUse) ($($InUsePercent)%)"
8543 Line 2 "Available: " -NoNewLine
8544 $tmp = "{0:N0}" -f $Statistics.AddressesAvailable
8545 Line 2 "$($tmp) ($($AvailablePercent)%)"
8546 }
8547 ElseIf(!$?)
8548 {
8549 Line 0 "Error retrieving IPv6 statistics"
8550 }
8551 Else
8552 {
8553 Line 0 "There were no IPv6 statistics"
8554 }
8555
8556 $Statistics = $Null
8557
8558 Write-Verbose "$(Get-Date):Getting IPv6 scopes"
8559 $IPv6Scopes = Get-DHCPServerV6Scope -ComputerName $DHCPServerName -EA 0
8560
8561 If($? -and $Null -ne $IPv6Scopes)
8562 {
8563 ForEach($IPv6Scope in $IPv6Scopes)
8564 {
8565 GetIPv6ScopeData $IPv6Scope
8566 }
8567 }
8568 ElseIf(!$?)
8569 {
8570 Line 0 "Error retrieving IPv6 scopes"
8571 }
8572 Else
8573 {
8574 Line 1 "There were no IPv6 scopes"
8575 }
8576 $IPv6Scopes = $Null
8577
8578 Write-Verbose "$(Get-Date): Getting IPv6 server options"
8579 Line 0 "Server Options"
8580
8581 $ServerOptions = Get-DHCPServerV6OptionValue -All -ComputerName $DHCPServerName -EA 0 | Sort-Object OptionId
8582
8583 If($? -and $Null -ne $ServerOptions)
8584 {
8585 ForEach($ServerOption in $ServerOptions)
8586 {
8587 Line 1 "Option Name`t: $($ServerOption.OptionId.ToString("00000")) $($ServerOption.Name)"
8588 Line 1 "Vendor`t`t: " -NoNewLine
8589 If([string]::IsNullOrEmpty($ServerOption.VendorClass))
8590 {
8591 Line 0 "Standard"
8592 }
8593 Else
8594 {
8595 Line 0 $ServerOption.VendorClass
8596 }
8597 Line 1 "Value`t`t: " $ServerOption.Value
8598
8599 #for spacing
8600 Line 0 ""
8601 }
8602 }
8603 ElseIf(!$?)
8604 {
8605 Line 0 "Error retrieving IPv6 server options"
8606 }
8607 Else
8608 {
8609 Line 2 "There were no IPv6 server options"
8610 }
8611 $ServerOptions = $Null
8612}
8613ElseIf($HTML)
8614{
8615}
8616
8617Write-Verbose "$(Get-Date): Finishing up document"
8618#end of document processing
8619
8620$AbstractTitle = "DHCP Inventory"
8621$SubjectTitle = "DHCP Inventory"
8622UpdateDocumentProperties $AbstractTitle $SubjectTitle
8623
8624If($MSWORD -or $PDF)
8625{
8626 SaveandCloseDocumentandShutdownWord
8627}
8628ElseIf($Text)
8629{
8630 SaveandCloseTextDocument
8631}
8632ElseIf($HTML)
8633{
8634 SaveandCloseHTMLDocument
8635}
8636
8637Write-Verbose "$(Get-Date): Script has completed"
8638Write-Verbose "$(Get-Date): "
8639$GotFile = $False
8640
8641If($PDF)
8642{
8643 If(Test-Path "$($Script:FileName2)")
8644 {
8645 Write-Verbose "$(Get-Date): $($Script:FileName2) is ready for use"
8646 Write-Verbose "$(Get-Date): "
8647 $GotFile = $True
8648 }
8649 Else
8650 {
8651 Write-Warning "$(Get-Date): Unable to save the output file, $($Script:FileName2)"
8652 Write-Error "Unable to save the output file, $($Script:FileName2)"
8653 }
8654}
8655Else
8656{
8657 If(Test-Path "$($Script:FileName1)")
8658 {
8659 Write-Verbose "$(Get-Date): $($Script:FileName1) is ready for use"
8660 Write-Verbose "$(Get-Date): "
8661 $GotFile = $True
8662 }
8663 Else
8664 {
8665 Write-Warning "$(Get-Date): Unable to save the output file, $($Script:FileName1)"
8666 Write-Error "Unable to save the output file, $($Script:FileName1)"
8667 }
8668}
8669
8670#email output file if requested
8671If($GotFile -and ![System.String]::IsNullOrEmpty( $SmtpServer ))
8672{
8673 If($PDF)
8674 {
8675 $emailAttachment = $Script:FileName2
8676 }
8677 Else
8678 {
8679 $emailAttachment = $Script:FileName1
8680 }
8681 SendEmail $emailAttachment
8682}
8683
8684Write-Verbose "$(Get-Date): "
8685
8686#http://poshtips.com/measuring-elapsed-time-in-powershell/
8687Write-Verbose "$(Get-Date): Script started: $($Script:StartTime)"
8688Write-Verbose "$(Get-Date): Script ended: $(Get-Date)"
8689$runtime = $(Get-Date) - $Script:StartTime
8690$Str = [string]::format("{0} days, {1} hours, {2} minutes, {3}.{4} seconds", `
8691 $runtime.Days, `
8692 $runtime.Hours, `
8693 $runtime.Minutes, `
8694 $runtime.Seconds,
8695 $runtime.Milliseconds)
8696Write-Verbose "$(Get-Date): Elapsed time: $($Str)"
8697
8698If($Dev)
8699{
8700 If($SmtpServer -eq "")
8701 {
8702 Out-File -FilePath $Script:DevErrorFile -InputObject $error 4>$Null
8703 }
8704 Else
8705 {
8706 Out-File -FilePath $Script:DevErrorFile -InputObject $error -Append 4>$Null
8707 }
8708}
8709
8710If($ScriptInfo)
8711{
8712 $SIFile = "$($pwd.Path)\DHCPInventoryScriptInfo_$(Get-Date -f yyyy-MM-dd_HHmm).txt"
8713 Out-File -FilePath $SIFile -InputObject "" 4>$Null
8714 Out-File -FilePath $SIFile -Append -InputObject "Add DateTime : $($AddDateTime)" 4>$Null
8715 If($MSWORD -or $PDF)
8716 {
8717 Out-File -FilePath $SIFile -Append -InputObject "Company Name : $($Script:CoName)" 4>$Null
8718 }
8719 Out-File -FilePath $SIFile -Append -InputObject "ComputerName : $($ComputerName)" 4>$Null
8720 If($MSWORD -or $PDF)
8721 {
8722 Out-File -FilePath $SIFile -Append -InputObject "Cover Page : $($CoverPage)" 4>$Null
8723 }
8724 Out-File -FilePath $SIFile -Append -InputObject "Dev : $($Dev)" 4>$Null
8725 If($Dev)
8726 {
8727 Out-File -FilePath $SIFile -Append -InputObject "DevErrorFile : $($Script:DevErrorFile)" 4>$Null
8728 }
8729 Out-File -FilePath $SIFile -Append -InputObject "Filename1 : $($Script:FileName1)" 4>$Null
8730 If($PDF)
8731 {
8732 Out-File -FilePath $SIFile -Append -InputObject "Filename2 : $($Script:FileName2)" 4>$Null
8733 }
8734 Out-File -FilePath $SIFile -Append -InputObject "Folder : $($Folder)" 4>$Null
8735 Out-File -FilePath $SIFile -Append -InputObject "From : $($From)" 4>$Null
8736 Out-File -FilePath $SIFile -Append -InputObject "Include Leases: $($IncludeLeases)" 4>$Null
8737 Out-File -FilePath $SIFile -Append -InputObject "Save As HTML : $($HTML)" 4>$Null
8738 Out-File -FilePath $SIFile -Append -InputObject "Save As PDF : $($PDF)" 4>$Null
8739 Out-File -FilePath $SIFile -Append -InputObject "Save As TEXT : $($TEXT)" 4>$Null
8740 Out-File -FilePath $SIFile -Append -InputObject "Save As WORD : $($MSWORD)" 4>$Null
8741 Out-File -FilePath $SIFile -Append -InputObject "Script Info : $($ScriptInfo)" 4>$Null
8742 Out-File -FilePath $SIFile -Append -InputObject "Smtp Port : $($SmtpPort)" 4>$Null
8743 Out-File -FilePath $SIFile -Append -InputObject "Smtp Server : $($SmtpServer)" 4>$Null
8744 Out-File -FilePath $SIFile -Append -InputObject "Title : $($Script:Title)" 4>$Null
8745 Out-File -FilePath $SIFile -Append -InputObject "To : $($To)" 4>$Null
8746 Out-File -FilePath $SIFile -Append -InputObject "Use SSL : $($UseSSL)" 4>$Null
8747 If($MSWORD -or $PDF)
8748 {
8749 Out-File -FilePath $SIFile -Append -InputObject "User Name : $($UserName)" 4>$Null
8750 }
8751 Out-File -FilePath $SIFile -Append -InputObject "" 4>$Null
8752 Out-File -FilePath $SIFile -Append -InputObject "OS Detected : $($Script:RunningOS)" 4>$Null
8753 Out-File -FilePath $SIFile -Append -InputObject "PoSH version : $($Host.Version)" 4>$Null
8754 Out-File -FilePath $SIFile -Append -InputObject "PSCulture : $($PSCulture)" 4>$Null
8755 Out-File -FilePath $SIFile -Append -InputObject "PSUICulture : $($PSUICulture)" 4>$Null
8756 If($MSWORD -or $PDF)
8757 {
8758 Out-File -FilePath $SIFile -Append -InputObject "Word language : $($Script:WordLanguageValue)" 4>$Null
8759 Out-File -FilePath $SIFile -Append -InputObject "Word version : $($Script:WordProduct)" 4>$Null
8760 }
8761 Out-File -FilePath $SIFile -Append -InputObject "" 4>$Null
8762 Out-File -FilePath $SIFile -Append -InputObject "Script start : $($Script:StartTime)" 4>$Null
8763 Out-File -FilePath $SIFile -Append -InputObject "Elapsed time : $($Str)" 4>$Null
8764}
8765
8766$runtime = $Null
8767$Str = $Null
8768$ErrorActionPreference = $SaveEAPreference