Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
7.1 kB
2
Indexable
Never
# JSON is not iterable in Jquery if only one element .. to fix 
# Change from Last Certificate to Certificate which is bound to https


$csv = Import-Csv -Path C:\Temp\serverliste3.csv
#$csv

$serverlist = $csv

$ErrorActionPreference = 'Stop'
#Load 
$machines = @($serverlist)

$report = foreach ($row in $csv) {
    try {
        $icmp = Test-Connection -BufferSize 32 -Count 1 -ComputerName $machines.Machine -Quiet # typo here $_ should be $machine

        $object = [pscustomobject]@{
            Machine        = $row.Machine
            CNAME          = $row.CNAME
            Loadbalancer   = $row.Loadbalancer
            #ICMP          = $icmp
            ICMP           = 'online'
            WebBinding     = ''
            IISInstalled   = ''
            NetIsInstalled = ''
            NotBefore      = ''
            NotAfter       = ''
            Subject        = ''
            ErrorMessage   = ''
            lastUpdate     = ''
            InstanceId     = ''
            Source         = ''
            TimeGenerated  = ''
            Message        = ''
        }
        
        if (-not $icmp) {
        		$object.ICMP = 'offline'
            $object
            continue
				}

        $capture = Invoke-Command -ComputerName $row.Machine {
        $object = [pscustomobject]@{

                WebBinding     = $false
                IISInstalled   = $false
                NetIsInstalled = $false
                NotBefore      = 'No Cert'
                NotAfter       = 'No Cert'
                Subject        = 'No Subject'
                lastUpdate     = 'false'
                InstanceId     = ''
                Source         = ''
                TimeGenerated  = ''
                Message        = ''
            }

            if(Get-WindowsOptionalFeature -Online -FeatureName NetFx3ServerFeatures) {
                $object.NetIsInstalled = "Installed"
            }
            $lastUpdate = (Get-HotFix) | Select -Last 1 
            if ($lastUpdate) {
                $object.lastUpdate = $lastUpdate.InstalledOn.ToString("dd/MM/yyyy") 
            }
           if(-not(Get-Module -Name WebAdministration -ListAvailable)) 
            {
                $object
                return
            }

            if (Get-WebBinding -Name "Default Web Site" -Protocol "https") {
                $object.WebBinding = "443"
            }
            if (Get-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole) {
                $object.IISInstalled = "Installed"
            }
            $cert = Get-ChildItem Cert:\LocalMachine\My | Select -Last 1
            if ($cert) {
                $object.NotBefore = $cert.NotBefore.ToString('dd/MM/yyyy')
                $object.NotAfter  = $cert.NotAfter.ToString('dd/MM/yyyy')
                $object.Subject   = $cert.Subject
            }

           ### Here i would like to present the last 5 (if not older then 2 weeks ) 

			   try {
                $eventlog = Get-EventLog -LogName Epic -EntryType Error -Newest 1 -ErrorAction Stop # Should be Stop
                    $object.InstanceID    = $eventlog.InstanceId
                    $object.Source        = $eventlog.Source
                    $object.TimeGenerated = '{0:dd/MM/yyyy}' -f $eventlog.TimeGenerated
                    $object.Message       = $eventlog.Message
            } catch {
            		# Just ignore this
            }
            # after everything is checked, you can output the object:
            $object
        }
        
        # if we're here it's because ICM succeded so we can updated the local `$object` with the remote results
        $object.WebBinding     = $capture.WebBinding
        $object.IISInstalled   = $capture.IISInstalled
        $object.NetIsInstalled = $capture.NetIsInstalled
        $object.NotBefore      = $capture.NotBefore
        $object.NotAfter       = $capture.NotAfter
        $object.lastUpdate     = $capture.lastUpdate
        $object.Subject        = $capture.Subject
        $object.InstanceId     = $capture.InstanceId
        $object.Source         = $capture.Source
        $object.TimeGenerated  = $capture.TimeGenerated
        $object.Message        = $capture.Message
    }
    catch {
        $errormsg = $object.ErrorMessage = $_.Exception.Message
    } finally {
    		$object
    }
}

# now $report has all the results of all machines :)
    $wb  =       $report | WHERE CNAME         -like "wb*"
    $ep  =       $report | WHERE CNAME         -like "ep*"
    $mc  =       $report | WHERE CNAME         -like "mc*"
    $ic  =       $report | WHERE CNAME         -like "ic*"
    $bw  =       $report | WHERE CNAME         -like "bw*"
    $bs  =       $report | WHERE CNAME         -like "bs*"
    $ec  =       $report | WHERE CNAME         -like "ec*"
    $hw  =       $report | WHERE CNAME         -like "hw*"
    $ku  =       $report | WHERE CNAME         -like "ku*"
    $sp  =       $report | WHERE CNAME         -like "sp*"
    $ce =        $report | WHERE CNAME         -like "ce*"
    $off =       $report | WHERE ICMP          -eq   'offline'
    $messages =  $report | WHERE Message       -ne   ""
    $nocert   =  $report | WHERE NotBefore     -eq   'No Cert'
    $nonet    =  $report | WHERE NetIsInsalled -eq   $false
    $nohttps  =  $report | WHERE WebBinding    -eq   $false
   
$nocert   | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\nocert.json"    # done
$nonet    | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\nonet.json"     # done
$nohttps  | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\nohttps.json"   # done
$messages | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\messages.json"  # done
$off      | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\off.json"       # done
$ce       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\ce.json"        # done
$ep       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\ep.json"        # done
$mc       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\mc.json"        # done
$ic       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\ic.json"        # done
$bw       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\bw.json"        # done
$bs       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\bs.json"        # done
$ec       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\ec.json"        # done
$hw       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\hw.json"        # done
$ku       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\ku.json"        # done
$sp       | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\sp.json"        # done
$report   | ConvertTo-Json | Set-Content "C:\inetpub\wwwroot\dashboard\json\overview.json"  # done