mercredi 8 août 2018

Trying to select a variable from within a ForEach loop, and if that variable is less than a certain number, display another variable as Red

this is my first time asking a question on here so sorry if it doesn't make much sense.

I'm currently trying to a do a project in which I look up OS information, Disk Size, and Processor type and manufacturer of a list of servers, and output all of that information from a single variable. After that, if the free space of the server is less than 5gb, list the disk size of the server in red.

This is what I have so far as my script:

$ServerList = Get-Content $env:USERPROFILE\Documents\Servers.txt

ForEach($Server in ($ServerList))
    {
        "Collecting server information on $Server, please wait..."
        Start-Sleep 3
        "Collecting Operating System..."
        $OSInfo = gwmi Win32_OperatingSystem -ComputerName $Server | select Caption
        "Collecting Storage..."
        $StorageInfo = gwmi Win32_LogicalDisk -ComputerName $Server | Format-Table DeviceId, @{n="Size in GB";e={[math]::Round($_.Size/1GB,2)}},@{n="Free Space in GB";e={[math]::Round($_.FreeSpace/1GB,2)}}
        "Collecting CPU..."
        $CPUInfo = gwmi Win32_Processor -ComputerName $Server | select Name, Manufacturer
        $ServerInfo = @($OSInfo, $StorageInfo, $CPUInfo) 
        Start-Sleep 3
        "All done with $Server. Here's what we got:"
        $ServerInfo
        Start-Sleep 3
        "Continuing on to next server in the list..."
        Start-Sleep 3  
        break      
    }

This is an example of the output:

Collecting server information on *****, please wait...
Collecting Operating System...
Collecting Storage...
Collecting CPU...
All done with *****. Here's what we got:

Caption                                                                                                                                                                                       
-------                                                                                                                                                                                       
Microsoft Windows Server 2008 R2 Enterprise                                                                                                                                                   



DeviceId                                                                                                             Size in GB                                               Free Space in GB
--------                                                                                                             ----------                                               ----------------
A:                                                                                                                            0                                                              0
C:                                                                                                                         51.9                                                           5.35
D:                                                                                                                           15                                                           6.37
E:                                                                                                                            0                                                              0



Name                                                                                            Manufacturer                                                                                  
----                                                                                            ------------                                                                                  
Intel(R) Xeon(R) CPU E7- 2870  @ 2.40GHz                                                        GenuineIntel                                                                                  
Intel(R) Xeon(R) CPU E7- 2870  @ 2.40GHz                                                        GenuineIntel                                                                                  
Intel(R) Xeon(R) CPU E7- 2870  @ 2.40GHz                                                        GenuineIntel                                                                                  
Intel(R) Xeon(R) CPU E7- 2870  @ 2.40GHz                                                        GenuineIntel                                                                                  
Continuing on to next server in the list...

I want to do an If statement where if the number provided in "Free Space in GB" is less than 5, then it will display Size in GB in Red, else, display it in Yellow, but I don't know how to go about actually pulling the number from "Free Space in GB" to use in the If statement. Any help?

Aucun commentaire:

Enregistrer un commentaire