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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...