Untitled

 avatar
unknown
plain_text
2 years ago
3.1 kB
5
Indexable

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


$ErrorActionPreference = 'Stop'

#Load 
$machines = @($csv)

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

        $object = [pscustomobject]@{
            Machine        = $machine
            #ICMP          = $icmp
            ICMP           = 'online'
            WebBinding     = ''
            IISInstalled   = ''
            NetIsInstalled = ''
            NotBefore      = ''
            NotAfter       = ''
            ErrorMessage   = ''
            lastUpdate     = ''
        }

        $capture = Invoke-Command -ComputerName $machine {
            Import-Module webadministration;

            $object = [pscustomobject]@{
                WebBinding     = $false
                IISInstalled   = $false
                NetIsInstalled = $false
                NotBefore      = 'No Cert'
                NotAfter       = 'No Cert'
                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
Editor is loading...