Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, March 12, 2014

Testing connections with PowerShell and Sending a eMail Report

My budding PowerShell Rock Stars are at it again.  This time we helped out a Network Administrator with testing the connectivity to clients and then email a report.  I’ve come across this type of request several times before.  Those of you who attend my sessions at the SMB Nation Emerging Technology Tour have see some similar code demonstrated.  With this group, I decided to press them a bit and we tried out converting our object that contains our report to HTML.  Here is our result.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

# Dynamic array to hold the output.

$Output = @()

 

# grab a list of computers from AD.

$ComputerList = Get-ADComputer -Filter *

 

# Count the number of computers for the Write-Progress Cmdlet.

$Count = $ComputerList |

         Measure-Object |

         Select-Object -ExpandProperty Count

 

# Control variable

$I = 0

 

 

$ComputerList |

    ForEach-Object {

        # Increment the control variable

        $I++

 

        # Calculate percent completed

        $PC = ($I/$Count)*100

 

        # Display progress bar.

        $Splat = @{"Activity" = "Connectivity Test: $_.Name";

                   "Status" = "$PC% Complete";

                   "PercentComplete" = $PC}

        Write-Progress @Splat

       

        # Create an object for each computer with the client's online status.

        $Output += New-Object -TypeName PSObject -Property @{

            "ComputerName" = $_.Name;

            "Status" = Test-Connection -ComputerName $_.Name -Quiet -Count 1

        }

     

    }

# Convert the output to HTML

$MailOutput = $Output | ConvertTo-HTML -Fragment | Out-String

 

# Send the email.

$Splat = @{"TO" = "Administrator@TechTour.com";

           "From" = "Allofus@HP.com";

           "Subject" = "Connectivity Test";

           "BodyAsHTML" = $True;

           "Body" = $MailOutput;

           "SMTPServer" = "Tech-EX1"}

Send-MailMessage @Splat

 

Line 2 is a dynamic array to hold our output.

Line 5 - You may want to consider parameterizing the filter to better meet your needs.

Lines 8 – 10 get a count of how many clients will be evaluated.  This is used to generate a progress bar.

Line 13 is our control variable for the progress bar.

Lines 16 – 36 perform several activities.  Line 19 will increment our counter.  Line 22 will perform the math to give us a percentage for the progress bar.

Lines 25 – 27 creates a splat for the cmdlet Write-Progress

Line 28 implements the progress bar.

Lines 31-34 test the connection to the clients.  You can see that we are creating our object holding our results while we are running the connection test.  Take a look at line 33.  We are creating a property called “Status” and setting equal to the result of Test-Connection. We are using the –Quiet parameter to return only a $TRUE or $FALSE from Test-Connection

Line 38 converts the objects stored in $Output into HTML code.  We are using the ConvertTo-HTML cmdlet to do the conversion.  By default, this creates a complete HTML page.   This will not work with the Send-MailMessage cmdlet so we use the –Fragment parameter to produce only code for an HTML table which will work with Send-MailMessage.

Lines 41 – 36 creates the splat for Send-MailMessage.

Line 47 sends the email.

No comments: