mercredi 22 mai 2019

Is there an If-Else function to make a heatmap criteria for a list of servers

I've created a heatmap piechart in excel and am writing a script in powershell to return results to that excel with 3 different criteria's. Looking to if-else powershell statements to create this criteria and export the results to the excel document.

Right now I've used all Get-WmiObject commands as well as if else statements, running from my local machine and reaching out across my company's network.

Finds Heatmap Criteria: Gets Last Boot up Time. Gets Install Date and OS Version for list of servers provided in text file serverlist.txt and publishes results in CSV

$machines = Get-Content C:\Users\manderson\Desktop\serverlist.txt 
$report = @() 
$object = @() 
foreach($machine in $machines) 
{ 
$machine   
 #Test if computer is online 

if (test-Connection -ComputerName $machine -Count 3 -Quiet )  
{  
$object = gwmi win32_operatingsystem -ComputerName $machine |     select   csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime  ($_.lastbootuptime)}} 
$report += $object
    $PingResult = 'Server IS Pinging' 

    # Connect to WMI on remote machine to get the required information 

    $gwmios = Get-WmiObject -Class Win32_OperatingSystem     -ComputerName     $machine 

    # Get the Install Date and Convert readable date & time format 

    $osidate = $gwmios.ConvertToDateTime($gwmios.InstallDate) 
    $osname = $gwmios.Caption  

    $tempreport = New-Object PSObject  
    $tempreport | Add-Member NoteProperty 'Server Name' $machine 
    $tempreport | Add-Member NoteProperty 'Ping Result' $PingResult 
    $tempreport | Add-Member NoteProperty 'OS Version' $osname  
    $tempreport | Add-Member NoteProperty 'OS Install Date' $osidate 
    $report += $tempreport  
}   
else   
    {   
    $PingResult = 'Server NOT Pinging'  
    $tempreport = New-Object PSObject  
    $tempreport | Add-Member NoteProperty 'Server Name' $machine
    $tempreport | Add-Member NoteProperty 'Ping Result' $PingResult 
    $report += $tempreport  
    }  
} 
$report | Export-csv C:\Users\manderson\Desktop\Reboot.csv

What I want to do is this; Make a powershell script that will read from my list of servers. Read thru this criteria in these 3 criterias below.

Critical Risk = OS version is more than 8 years old, reboots haven't happened for more than 1 year, hardware is more than 5 years old.

Medium Risk = OS version is more than 4 years old, reboots haven't happened for more than 3 months, and hardware is more than 3 years old.

Low Risk = OS version is more than 1 year old, reboots haven't happened for more than 2 months, and hardware is more than 2 years old.

Then export it to an excel doc in a certain column with the results of "C" for Critical, "M" for Medium, or "L" for Low risk levels. I already have the excel portion built with the formulas creating a piechart. But it is manual and the results aren't strictly from the server.

Now i'm just trying to automate it since the servers change over time as more servers are implemented within the company.

Please help me create this criteria in the script so that I can put it on all of our servers across with a GPO and then it would automatically return the results back to my excel.

Aucun commentaire:

Enregistrer un commentaire