Help with report

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 4 months ago.

  • Author
    Posts
  • #5141

    by davids at 2012-09-24 17:31:08

    Hi,

    I need some help changing this script so that it meets my needs.

    I need for it to use an array of servers, try to do the TCP connect to port 3389 so that I know RDP (actually Terminal Services) is working and then build up a report like below that is emailed to me.

    ———-Example Report—————-

    Server | RDPStatus
    Server1 | OK
    Server2 | FAIL

    ———script———————–

    [code2=powershell]$port = 3389
    $ErrorActionPreference = "SilentlyContinue"
    foreach ($target in $args) {
    new-object System.Net.Sockets.TcpClient($target, $port) -ErrorVariable sockErr | out-null
    if ($sockerr) {
    write-host "FAIL $target"
    } else {
    write-host "OK $target"
    }
    }[/code2]

    by davids at 2012-09-24 19:27:03

    OK. So I think I'm getting closer.

    This emails me a message, but the body only contains the text of System.Collections.Hashtable

    [code2=powershell]$servers = "dfcu-ptrm13","dfcu-ptrm14","dfcu-ptrm16","dfcu-ptrm17","dfcu-ptrm18","dfcu-ptrm19","dfcu-ptrm20","dfcu-ptrm21","dfcu-ptrm22","dfcu-ptrm23","dfcu-ttrm1","dfcu-psun"

    $MailFrom = "scriptaccount@workemail.com"
    $Mailto = "me@myworkemail.com"
    $EmailSubject = "Remote Desktop Port Open Test"
    $smtpServer = "mysmtpserver"
    $Mailtext = @{}

    $port = 3389

    $ErrorActionPreference = "Continue"

    $RDPping = @{}

    $data = New-Object PSObject -Property @{ Computername = "Computername" ; IP = "IP" ; Status = "Status" ; }
    $data.Computername="$target"

    foreach ($target in $servers) {
    $reachable = new-object System.Net.Sockets.TcpClient($target, $port) -ErrorVariable sockErr | out-null
    if($reachable)
    {
    $IPAddress = [System.Net.Dns]::GetHostAddresses($_.Name)|select-object IPAddressToString -expandproperty IPAddressToString
    $data.Status = "OK"
    }
    else
    {
    $data.Status = "FAIL"
    }

    #if ($sockerr) {
    #write-host "FAIL $target"
    #$data.Status = "FAIL"
    #$RDPping = $RDPping , $data
    #} else {
    #write-host "OK $target"
    #$data.Status = "OK"
    #$RDPping = $RDPping , $data
    #}

    New-Object -TypeName PSObject -Property @{
    SystemName = $_
    Reachable = $reachable
    IPAddress = $IPAddress
    } | Select-Object SystemName,Reachable,IPAddress | Export-Csv -Path C:\export.csv
    }

    $Mailtext = $RDPping + $Date

    $Date = Get-Date

    send-Mailmessage -To $MailTo -From $MailFrom -Subject $EmailSubject -SmtpServer $smtpServer -Body ($Mailtext)[/code2]

    by RichardSiddaway at 2012-09-25 02:58:52

    Try changing to

    -Body $($Maitext)

    by davids at 2012-09-25 15:58:47

    OK. So I changed the script to as below:

    [code2=powershell]$servers = "dfcu-ptrm13","dfcu-ptrm14","dfcu-ptrm16","dfcu-ptrm17","dfcu-ptrm18","dfcu-ptrm19","dfcu-ptrm20","dfcu-ptrm21","dfcu-ptrm22","dfcu-ptrm23","dfcu-ttrm1","dfcu-psun"

    $MailFrom = "scriptaccount@abc.com"
    $Mailto = "me@abc.com"
    $EmailSubject = "Remote Desktop Port Open Test"
    $smtpServer = "abcsmtpserver"

    $port = 3389

    $ErrorActionPreference = "Continue"

    $RDPping = @{}

    $data = New-Object PSObject -Property @{ Computername = "Computername" ; IP = "IP" ; Status = "Status" ; }
    $data.Computername="$target"

    foreach ($target in $servers) {
    $reachable = new-object System.Net.Sockets.TcpClient($target, $port) -ErrorVariable sockErr | out-null
    if($reachable)
    {
    #$IPAddress = [System.Net.Dns]::GetHostAddresses($_.Name)|select-object IPAddressToString -expandproperty IPAddressToString
    $data.Status = "OK"
    $RDPping = $RDPping , $data
    }
    else
    {
    $data.Status = "FAIL"
    $RDPping = $RDPping , $data
    }

    }

    $Mailtext = $RDPping | Select Computername,IP,Status | Out-String

    #$Date = Get-Date

    send-Mailmessage -To $MailTo -From $MailFrom -Subject $EmailSubject -SmtpServer $smtpServer -BodyAsHtml $($Mailtext)[/code2]

    What I get in the email body is:

    [code2=plain]Computername IP Status ———— — —— dfcu-psun IP FAIL[/code2]

    This is not what I was expecting.

    by poshoholic at 2012-09-26 19:21:44

    Try switching from:
    Send-MailMessage ... -BodyAsHtml $($MailText)
    to:
    Send-MailMessage ... -Body $MailText
    Or if you want an HTML email message, you could do this instead:
    $MailHtml = $RDPPing | Select ComputerName,IP,Status | ConvertTo-Html -Fragment
    Send-MailMessage ... -BodyAsHtml $MailHtml

    by davids at 2012-10-03 19:23:21

    OK. So I was still having problems so I changed and I'm now trying to use Chad's script from http://poshcode.org/85

    All I'm doing is building an array of computer names and then running

    [code2=powershell]$NewTermServ | %{.\Test-Port.ps1}[/code2]
    True
    True
    True
    True
    True
    True
    True
    True

    Can anyone advise me how I can get the name of the server to appear as well?

    For example:
    Name RDPConnect
    Server1 True
    Server2 True

    by poshoholic at 2012-10-04 06:16:52

    $NewTermServ | ForEach-Object {
    New-Object PSObject -Property @{
    Name = $_
    RDPConnect = $_ | .\Test-Port.ps1
    }
    }

    Or, if you're using PowerShell 3, you can do it this way:
    $NewTermServ | ForEach-Object {
    [pscustomobject]@{
    Name = $_
    RDPConnect = $_ | .\Test-Port.ps1
    }
    }

    by davids at 2012-10-04 17:50:48

    Thanks Kirk. That works perfectly.

You must be logged in to reply to this topic.