Resolve Unique IP and Send Email?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Resolve Unique IP and Send Email?

Post by ITEngineer »

Hi All,

The below contents are from the $Results variable I got from my larger script.

Code: Select all

MachineName     : PRDDC03.domain.com
Id              : 4624
TimeCreated     : 4/09/2020 12:12:12 PM
Account         : DOMAIN\SVC-SQL
LogonType       : 3
LogonTypeString : Network
WorkstationName :
SourceIP        : 172.16.32.99
SourceName      :

MachineName     : PRDDC02-VM.domain.com
Id              : 4624
TimeCreated     : 3/09/2020 10:21:27 AM
Account         : DOMAIN\SVC-prtg
LogonType       : 3
LogonTypeString : Network
WorkstationName :
SourceIP        : 10.10.13.97
SourceName      :

MachineName     : AD01.domain.com
Id              : 4624
TimeCreated     : 3/09/2020 9:15:27 AM
Account         : DOMAIN\Admin.local
LogonType       : 3
LogonTypeString : Network
WorkstationName :
SourceIP        : 192.168.32.58
SourceName      :

MachineName     : SVRDC33.domain.com
Id              : 4624
TimeCreated     : 28/08/2020 4:51:13 PM
Account         : DOMAIN\GLobal.Admin
LogonType       : 3
LogonTypeString : Network
WorkstationName :
SourceIP        : 192.168.13.66
SourceName      :

MachineName     : AD01.domain.com
Id              : 4624
TimeCreated     : 28/08/2020 11:54:27 AM
Account         : DOMAIN\svc.avs
LogonType       : 3
LogonTypeString : Network
WorkstationName :
SourceIP        : 192.168.1.66
SourceName      :

MachineName     : AD02.domain.com
Id              : 4624
TimeCreated     : 26/08/2020 9:29:53 AM
Account         : DOMAIN\testing1
LogonType       : 3
LogonTypeString : Network
WorkstationName :
SourceIP        : 192.168.13.77
SourceName      :
How can I get the display for the Unique IP address like the below format:

Code: Select all

192.168.1.66 - LAPTOP66
192.168.32.58 - No-DNS
10.10.13.97 - SVRPRTG01
172.16.32.99 - NoDNS
The below code is not producing the output I wanted:
Write-Host "Unique IP addresses are as follows"**
Write-Host "$(($Results | Select-Object -ExpandProperty SourceIP -Unique)) - Resolve-DnsName -Type A -Name (($Results | Select-Object -ExpandProperty SourceIP -Unique))"
Hence I need your help.

I want to convert the $Results variable as HTML so I can attach it in Send-MailMessage body

Code: Select all

$params = @{
    SmtpServer = 'SMTP.DOMAIN.com'
    From       = "$env:COMPUTERNAME@$env:userdnsdomain"
    To         = 'boss@IT.com'
    Subject    = "The report email as at $(Get-Date -Format 'F')"
    Body       = $Results
    BodyAsHtml = $true
}
Send-MailMessage @params

Thank you in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Resolve Unique IP and Send Email?

Post by jvierra »

To send an object collection as HTML you would use "ConvertTo-Html" .

$body = $results | ConvertTo-Html | Out-String
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Resolve Unique IP and Send Email?

Post by ITEngineer »

jvierra wrote: Tue Sep 15, 2020 3:43 pm To send an object collection as HTML you would use "ConvertTo-Html" .

$body = $results | ConvertTo-Html | Out-String
Hi Mr. Vierra,

Yes, that works partially on the email body to show the $Results content.
However, how to allow the output to be like:

Unique IP address:

Code: Select all

192.168.1.66 - LAPTOP66
192.168.32.58 - No-DNS
10.10.13.97 - SVRPRTG01
172.16.32.99 - NoDNS
No need to be in the HTML table.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Resolve Unique IP and Send Email?

Post by jvierra »

Don't make the body HTML and the line breaks will persist.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

Re: Resolve Unique IP and Send Email?

Post by ITEngineer »

jvierra wrote: Tue Sep 15, 2020 5:43 pm Don't make the body HTML and the line breaks will persist.
Yes, that does make sense.

Thank you Mr, Vierra.
/* IT Engineer */
dellaschm1972
Posts: 3
Last visit: Sat Oct 24, 2020 2:15 pm

Re: Resolve Unique IP and Send Email?

Post by dellaschm1972 »

Thanks for detailed answer its help me lot
This topic is 3 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked