Skip to main content

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.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.