Untitled

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

####################################################### DASHBOARD BACKEND #####################################################################

############  Überprüft EPIC WEBSERVER auf / IST ONLINE / HAT GÜLTIGES HTTPS BINDING IN IIS / HAT IIS INSTALLIERT / HAT .NET INSTALLIER / 
############  HAT ZERTIFIKAT  /  WANN WURDE ZERTIFIKAT INSTALLIERT / WANN LÄUFT ZERTIFIKAT AB / LETZTES WINDOWS SERVER UPDATE / ERROR

############  Version 0.1
############  Alexander Geuze



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


$serverlist = $csv
$serverlist


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


$playground = $machines | where-object CNAME -like 'ply' 
$playground
break

$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       = ''
            ErrorMessage   = ''
            lastUpdate     = ''
        }

        $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'
            }

            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')
            }
            # 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
        
        # 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