Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Add a column in this script to put an '*' if the PercentFree is below 10%

$
0
0

I was asked to check with this forum to see if I could get help here.    My code is below.  What needs to be done is go out to all the servers in servers.txt and check free drive space.  If free drive space is below 10%, then add a '*' next to that line in the report.  Below the code is the error message I get when running the script.

Previous Thread: http://social.technet.microsoft.com/Forums/scriptcenter/en-US/ce6ba6d0-d5e8-4470-890c-a8c9914770be/add-a-column-in-this-script-to-put-an-if-the-percentfree-is-below-10

Code:

$servers = get-content servers.txt
foreach ($server in $servers)
{
$Size = @{name="Size(GB)";expression={ "{0:N2}" -f ([double]$_.Size/1GB) }}
$FreeSpace = @{name="FreeSpace(GB)";expression={ "{0:N2}" -f ([double]$_.FreeSpace/1GB) }}
$Used=@{name="Used(GB)";expression={ "{0:N2}" -f (([double]$_.Size-[double]$_.FreeSpace)/1GB) }}
$PercentFree=@{name="PercentFree";expression={ "{0:P2}" -f ([double]$_.FreeSpace/[double]$_.Size) }}
$PercentUsed=@{name="PercentUsed";expression={ "{0:P2}" -f (([double]$_.Size-[double]$_.FreeSpace)/[double]$_.Size) }}
$check = Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computerName $server| Select-Object $PercentFree
if(([double]($check.PercentFree).Replace(' %','')) -lt 10){
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computerName $server| Select-Object SystemName,DeviceID,VolumeName,$Size,$FreeSpace,$used,$PercentUsed,$PercentFree,@{l="Attention";e={"*"}} |ft -Property * -autosize | out-string -Width 4096  > "$server.txt"
$server
}
else{
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computerName $server|Select-Object SystemName,DeviceID,VolumeName,$Size,$FreeSpace,$used,$PercentUsed,$PercentFree |ft -Property * -autosize | out-string -Width 4096  > "$server.txt"
$server
}
}

ERROR:

You cannot call a method on a null-valued expression.
At C:\scripts\freespace\freespace2.ps1:10 char:41
+ if(([double]($check.PercentFree).Replace <<<< (' %','')) -lt 10){
    + CategoryInfo          : InvalidOperation: (Replace:String) [], RuntimeEx
   ception
    + FullyQualifiedErrorId : InvokeMethodOnNull


http://techwithmike.com


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>