Avatar of SteelMike
SteelMike

asked on 

Monitoring Server Hard space

I am a complete newbie when it comes to scripting so please bare with me!!
I have around 300+ servers to include Virtual Machines that I need to monitor their physical Hard Drive space. Most of the servers and VM's will have two physical Hard drives (C: and D: respectfully).
First part of the script:
I need a script that will search active directory for computers that have either Win2000 or Win2003 and than export that list to a .csv file. I have other processes that could use this file.
Second Part:
Use the .csv file to find general information about the servers (CPU, Memory, # of HD's, HD Space, HD Free Space).
Thrid Part:
If any of the physical Hard Drive on a single server falls below lets say 5% they are added to a list of other servers meeting the same requirements.
Fourth Part:
Sends an email with the list of servers that fall below the 5%.

I don't have a lot of time to implement this script, so I need something that I can just copy from here, make a few minor changes and than push it out.  I would prefer the script to be in Powershell, but at this point I really don't care. I just need some help!!
Microsoft Legacy OSMicrosoft DevelopmentMicrosoft Hardware

Avatar of undefined
Last Comment
SteelMike
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Check out the free Quest cmdlets www.quest.com/powershell

I will try to post something later

FYI: Powershell Zone https://www.experts-exchange.com/Programming/Languages/Scripting/MSH-Monad/
Avatar of SteelMike
SteelMike

ASKER

I will run through the first examples you gave now!  Thank you.
Avatar of SteelMike
SteelMike

ASKER

So I am pulling different items from the examples you provided and this is where I am so far. I'm learning so please be patient with me.
So I am able to pull a list of servers that have WIN2k and WIN2003, but I don't know what i need to do to pipe the results to a .csv file. I will add more points for the added help!
#Clear-Content "E:\Scripts\Powershell\TEST\Output\ServerNames_all.txt
 
#OS Level search in AD
$strCategory = "computer"
$strOS1 = "Windows Server 2003"
$strOS = "Windows Server 2000"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = "(&(objectCategory=$strCategory)(|(operatingSystem=$strOS1)(operatingSystem=$strOS)))"
 
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
 
$colResults = $objSearcher.FindAll()
 
foreach ($objResult in $colResults)
    {$objComputer = $objResult.Properties; $objComputer.name}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of SteelMike
SteelMike

ASKER

Thanks for the help... I am working on getting the other parts up and running. Give me a day with testing and I should have something for you to look at.
Avatar of SteelMike
SteelMike

ASKER

So I was able to search for the servers using the script above. I ran through the second part of the where it finds all logical disk=3. This is what I want the script to do. But the script was reading the servers from a text file instead of the .csv file. So as you can see I am having issues make the below script to read from the .csv file instead of a text file. Can you check through it for me and let me know where I went wrong!! Thank you



Function Get-Utilization {
    Param([string]$computername=$env:computername,
          [string]$ID="C:"
          )
          
     #suppress errors messages    
    $errorActionPreference="silentlycontinue"      
          
    $drive=Get-WmiObject Win32_Logicaldisk -filter "DeviceID='$ID'" `
    -computername $computername -errorAction "silentlycontinue"
    
    if ($drive.size) {
    
        #divide size and freespace by 1MB because they are UInt64
        #and I can't do anything with them in there native type
        
        $size=$drive.size/1MB
        $free=$drive.freespace/1MB
          
        $utilization=($size-$free)/$size
        
        $u=$utilization
    }
    else {
    #there as an error so return a value that can't be mistaken
    #for drive utilization
        $u=-1
    }
    #Format $u as a percentage to 2 decimal points
 
$percent="{0:P2}" -f $u
 
if ($u -eq -1) { 
    $msg="WARNING: Failed to get drive {0} on {1}" -f $drive,$computer
    $color="RED"
}
 
elseif ($u -ge .85){ 
    $msg="WARNING: Drive {0} on {1} is at {2} utilization." -f $drive,$computer,$percent
    $color="RED"
}
 
else {
    $msg="WARNING: Drive {0} on {1} is at {2} utilization." -f $drive,$computer,$percent
    $color="GREEN"
}
 
Write-Host $msg -foregroundcolor $color 
 
}
 
#Server List
$ServerList = "E:\Scripts\PowerShell\Test\Output\ServerNames_all.csv"
Import-csv -path $ServerList |
foreach-object `
{
#Loops through all drives
$drives = get-wmiobject -class "Win32_LogicalDisk" -filter "DriveType= 3" `
-namespace "root\CIMV2" -computername $_.FQDN
foreach($drive in $drives){
Get-Utilization $computer $drive.DeviceID 
}
 
}

Open in new window

Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Open the CSV and see what the header row is. I bet it is not FQDN
Avatar of SteelMike
SteelMike

ASKER

Thank you for the help.  Even though it's a partial solution I am satisfied with the help you have provided.
Microsoft Legacy OS
Microsoft Legacy OS

The Microsoft Legacy Operating System topic includes legacy versions of Microsoft operating systems prior to Windows 2000: All versions of MS-DOS and other versions developed for specific manufacturers and Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions, and Windows Mobile.

55K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo