Generate status report for servers on different domain using Power shell

0

I am trying to generate server status HTML report for list of servers mentioned in text file. Some of my servers are in different domains so it gives me error.

Error :

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\raj.negi.RESONATE\Desktop\WinServ-Status.ps1:143 char:32
+             $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName ...
+                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

It is working fine for the servers which is in same domain from where i running the script. enter image description here

I am able to ping successfully the other domain server.

[CmdletBinding()]
Param(
    $ServerFile = "C:\ServerStatus\ServerList.txt",
    $OutputPath = "C:\ServerStatus",
    [alias("DiskAlert")]
    $DiskAlertThreshold,
    [alias("CpuAlert")]
    $CpuAlertThreshold,
    [alias("MemAlert")]
    $MemAlertThreshold,
    [alias("Refresh")]
    $RefreshTime,
    [alias("SendTo")]
    $MailTo,
    [alias("From")]
    $MailFrom,
    [alias("Smtp")]
    $SmtpServer,
    [alias("User")]
    $SmtpUser,
    [alias("Pwd")]
    $SmtpPwd,
    [switch]$UseSsl)

## Function to get the up time from the server
Function Get-UpTime
{
    param([string] $LastBootTime)
    $Uptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBootTime)
    "$($Uptime.Days) days $($Uptime.Hours)h $($Uptime.Minutes)m"
}

## Begining of the loop. Lower down the loop is broken if the refresh option is not configured.
Do
{
    ## Change value of the following parameter as needed
    $OutputFile = "$OutputPath\WinServ-Status-Report.htm"
    $ServerList = Get-Content $ServerFile
    $Result = @()

    ## Look through the servers in the file provided
    ForEach ($ServerName in $ServerList)
    {
        $PingStatus = Test-Connection -ComputerName $ServerName -Count 1 -Quiet

        ## If server responds, get uptime and disk info
        If ($PingStatus)
        {
            $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ServerName
            $CpuAlert = $false
            $CpuUsage = Get-WmiObject Win32_Processor -Computername $ServerName | Measure-Object -Property LoadPercentage -Average | ForEach-Object {$_.Average; If($_.Average -ge $CpuAlertThreshold){$CpuAlert = $True}; "%"}
            $Uptime = Get-Uptime($OperatingSystem.LastBootUpTime)
            $MemAlert = $false
            $MemUsage = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ServerName | ForEach-Object {“{0:N0}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) * 100)/ $_.TotalVisibleMemorySize); If((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory) * 100)/ $_.TotalVisibleMemorySize -ge $MemAlertThreshold){$MemAlert = $True}; "%"}
            $DiskAlert = $false
            $DiskUsage = Get-WmiObject Win32_LogicalDisk -ComputerName $ServerName | Where-Object {$_.DriveType -eq 3} | Foreach-Object {$_.DeviceID, [Math]::Round((($_.Size - $_.FreeSpace) * 100)/ $_.Size); If([Math]::Round((($_.Size - $_.FreeSpace) * 100)/ $_.Size) -ge $DiskAlertThreshold){$DiskAlert = $True}; "%"}
            $IPv4Address = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $ServerName | Select-Object -Expand IPAddress | Where-Object { ([Net.IPAddress]$_).AddressFamily -eq "InterNetwork" }
        }

        ## Put the results together
        $Result += New-Object PSObject -Property @{
            ServerName = $ServerName
            IPV4Address = $IPv4Address
            Status = $PingStatus
            CpuUsage = $CpuUsage
            CpuAlert = $CpuAlert
            Uptime = $Uptime
            MemUsage = $MemUsage
            MemAlert = $MemAlert
            DiskUsage = $DiskUsage
            DiskAlert = $DiskAlert
        }

        ## Clear the variables after obtaining and storing the results so offline servers don't have duplicate info.
        Clear-Variable IPv4Address
        Clear-Variable Uptime
        Clear-Variable MemUsage
        Clear-Variable CpuUsage
        Clear-Variable DiskUsage
    }

    ## If there is a result put the HTML file together.
    If ($Result -ne $null)
    {
        $HTML = '<style type="text/css">
                p {font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;font-size:14px}
                p {color:#ffffff;}
                #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
                #Header td, #Header th {font-size:15px;text-align:left;border:1px solid #1a1a1a;padding:2px 2px 2px 7px;color:#ffffff;}
                #Header th {font-size:16px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#4B4B4B;color:#ffffff;}
                #Header tr.alt td {color:#ffffff;background-color:#eaf2d3;}
                #Header tr:nth-child(even) {background-color:#4B4B4B;}
                #Header tr:nth-child(odd) {background-color:#4B4B4B;}
                body {background-color: #1a1a1a;}
                </style>
                <head><meta http-equiv="refresh" content="30"></head>'

        $HTML += "<html><body>
            <table border=1 cellpadding=0 cellspacing=0 id=header>
            <tr>
                <th><b><font color=#e6e6e6>Server</font></b></th>
                <th><b><font color=#e6e6e6>IP</font></b></th>
                <th><b><font color=#e6e6e6>Status</font></b></th>
                <th><b><font color=#e6e6e6>CPU Usage</font></b></th>
                <th><b><font color=#e6e6e6>Memory Usage</font></b></th>
                <th><b><font color=#e6e6e6>Disk Usage</font></b></th>
                <th><b><font color=#e6e6e6>Uptime</font></b></th>
            </tr>"

        ## Highlight the alerts if the alerts are triggered.
        ForEach($Entry in $Result)
        {
            If ($Entry.Status -eq $True)
            {
                $HTML += "<td><font color=#00e600>$($Entry.ServerName)</font></td>"
            }

            Else
            {
                $HTML += "<td><font color=#FF4D4D>&#10008 $($Entry.ServerName)</font></td>"
            }

            If ($Entry.Status -eq $True)
            {
                $HTML += "<td><font color=#00e600>$($Entry.IPV4Address)</font></td>"
            }

            Else
            {
                $HTML += "<td><font color=#FF4D4D>&#10008 Offline</font></td>"
            }

            If ($Entry.Status -eq $True)
            {
                $HTML += "<td><font color=#00e600>&#10004 Online</font></td>"
            }

            Else
            {
                $HTML += "<td><font color=#FF4D4D>&#10008 Offline</font></td>"
            }

            If ($Entry.CpuUsage -ne $null)
            {
                If ($Entry.CpuAlert -eq $True)
                {
                    $HTML += "<td><font color=#ffff4d>&#9888 $($Entry.CpuUsage)</font></td>"
                }

                Else
                {
                    $HTML += "<td><font color=#00e600>&#10004 $($Entry.CpuUsage)</font></td>"
                }
            }

            Else
            {
                $HTML += "<td><font color=#FF4D4D>&#10008 Offline</font></td>"
            }

            If ($Entry.MemUsage -ne $null)
            {
                If ($Entry.MemAlert -eq $True)
                {
                    $HTML += "<td><font color=#ffff4d>&#9888 $($Entry.MemUsage)</font></td>"
                }

                Else
                {
                    $HTML += "<td><font color=#00e600>&#10004 $($Entry.MemUsage)</font></td>"
                }
            }

            Else
            {
                $HTML += "<td><font color=#FF4D4D>&#10008 Offline</font></td>"
            }

            If ($Entry.DiskUsage -ne $null)
            {
                If ($Entry.DiskAlert -eq $True)
                {
                    $HTML += "<td><font color=#ffff4d>&#9888 $($Entry.DiskUsage)</font></td>"
                }

                Else
                {
                    $HTML += "<td><font color=#00e600>&#10004 $($Entry.DiskUsage)</font></td>"
                }
            }

            Else
            {
                $HTML += "<td><font color=#FF4D4D>&#10008 Offline</font></td>"
            }

            If ($Entry.Status -eq $True)
            {
                $HTML += "<td><font color=#00e600>$($Entry.Uptime)</font></td>
                          </tr>"
            }

            Else
            {
                $HTML += "<td><font color=#FF4D4D>&#10008 Offline</font></td>
                          </tr>"
            }
        }

        ## Report the date and time the script ran.
        $HTML += "</table><p><font color=#e6e6e6>Status refreshed on: $(Get-Date -Format G)</font></p></body></html>"

        ## Output the HTML file
        $HTML | Out-File $OutputFile

        ## If email was configured, set the variables for the email subject and body
        If ($SmtpServer)
        {
            $MailSubject = "Server Status Report"
            $MailBody = Get-Content -Path $OutputFile | Out-String

            ## If an email password was configured, create a variable with the username and password
            If ($SmtpPwd)
            {
                $SmtpPwdEncrypt = Get-Content $SmtpPwd | ConvertTo-SecureString
                $SmtpCreds = New-Object System.Management.Automation.PSCredential -ArgumentList ($SmtpUser, $SmtpPwdEncrypt)

                ## If ssl was configured, send the email with ssl
                If ($UseSsl)
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -BodyAsHtml -SmtpServer $SmtpServer -UseSsl -Credential $SmtpCreds
                }

                ## If ssl wasn't configured, send the email without ssl
                Else
                {
                    Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -BodyAsHtml -SmtpServer $SmtpServer -Credential $SmtpCreds
                }
            }

            ## If an email username and password were not configured, send the email without authentication
            Else
            {
                Send-MailMessage -To $MailTo -From $MailFrom -Subject $MailSubject -Body $MailBody -BodyAsHtml -SmtpServer $SmtpServer
            }
        }

        ## If the refresh time option is configured, wait the specifed number of seconds then loop.
        If ($RefreshTime -ne $null)
        {
            Start-Sleep -Seconds $RefreshTime
        }
    }
}

## If the refresh time option is not configured, stop the loop.
Until ($RefreshTime -eq $null)

Firewall is Off on all servers.

powershell
cross-domain
asked on Stack Overflow Mar 15, 2018 by rAJ • edited Mar 15, 2018 by rAJ

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0