Untitled

mail@pastecode.io avatar
unknown
powershell
2 years ago
4.5 kB
4
Indexable
Never

$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]@{
        if(-not(Get-Module -Name WebAdministration -ListAvailable)) 
            {
                $object.IIS = "not installed"
                $object
                return
            }

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

            if (Get-WebBinding -Name "Default Web Site" -Protocol "https") {
                $object.WebBinding = "443"
            }
            if (Get-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole) {
                $object.IISInstalled = "Installed"
            }
            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") 
            }
            

            $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 :)

$report | ConvertTo-Json | Out-File "C:\inetpub\wwwroot\dashboard\json\overview.json" # done