Untitled

mail@pastecode.io avatar
unknown
powershell
2 years ago
4.4 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        = ''
        }

        $capture = Invoke-Command -ComputerName $row.Machine {
            Import-Module webadministration;

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

            $eventlog = Get-EventLog -LogName Epic -EntryType Error -Newest 1
            if (-not $eventlog::IsNullOrEmpty){
                $object.InstanceID    = $eventlog.InstanceId
                $object.Source        = $eventlog.Source
                $object.TimeGenerated = $eventlog.TimeGenerated.ToString('dd/MM/yyyy')
                $object.Message       = $eventlog.Message
                }
            # 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
        
        # now that the local object is updated, we can output it
        $object
    }
    catch {
        # if `Invoke-Command` fails, we can output that initial object stating everything fail except ICMP
        $errormsg = $object.ErrorMessage = $_.Exception.Message
        $object.ICMP = "offline"
        $object # try now, at least will tell you what failed
        #$errormsg = $object.ErrorMessage = "Invoke-Command"
       
    }
}

# now $report has all the results of all machines :)

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