problem with several loops on script

Posted on 2013-06-04
Last Modified: 2013-06-30
In a txt file $MONITORING_ENV, there are the following lines :


I try to generate alerts for disks in $MONITORING_ENV with the following script, but I think there is a loop problem.
$LIST_SPEC_DISK= @( Select-String $MONITORING_ENV -Pattern "^(.+):DISK:" |? {$_ -notmatch "^([A-Z]:\\.+):DISK:OFF"} | ForEach-Object { $_.matches } | ForEach-Object { $_.groups[1].value } | Select -Unique -ErrorAction silentlycontinue)
$LIST_SPEC_DISK = Get-WmiObject Win32_Volume -Filter "Label!='OSDisk'" | Where-Object { $LIST_SPEC_DISK -contains $_.Name} -ErrorAction silentlycontinue	  


  [double] $free_sz=$DISK.FreeSpace

  $SPEC_DISK = @()
  get-content $MONITORING_ENV| ? { $_ -like '*:DISK:*%' } | % {
  $val = $_ -split ':' 
  $SPEC_DISK += New-Object PsObject -Property @{
  DISK       = $val[0]+':'+$val[1]
  warning  = $val[3]
  critical = $val[4]
  foreach ($SPEC_DISK.DISK in $SPEC_DISK ) {
  if ($SPEC_DISK.WARNING -match "%") {



  $_UNIT = "%"

  elseif ($SPEC_DISK.CRITICAL -match "M") {


  $_UNIT = "M"

if ($FREE_SZ -gt 0) {
$FREE_PCT=[int](($FREE_SZ * 100 / $TOTAL_SZ))

if  ($_UNIT = "%" -and $SPEC_MOUNT_POINT -eq $MOUNT_POINT -and $FREE_PCT -lt $_SPEC_PANIC_THRES) { 


Open in new window

How can I resolve ?

Question by:bibi92
LVL 40

Accepted Solution

Subsun earned 500 total points
ID: 39221304
Few observations..

In line 22 you mentioned..
foreach ($SPEC_DISK.DISK in $SPEC_DISK )
 I would change it to foreach ($SPEC_D in $SPEC_DISK )
and the Var's inside the foreach loop need to be changed accordingly..
For example..
foreach ($SPEC_D in $SPEC_DISK) {
  if ($SPEC_D.WARNING -match "%") {



  $_UNIT = "%"

  elseif ($SPEC_D.CRITICAL -match "M") {


  $_UNIT = "M"


Open in new window

In line 37...

As per your input sheet and my understanding the value of the var is always like "number with %" (For example 5% or 20%), so it will never match "M"

I think you should replace "M" with "%"

In line 55..
if  ($_UNIT = "%" -and $SPEC_MOUNT_POINT -eq $MOUNT_POINT -and $FREE_PCT -lt $_SPEC_PANIC_THRES) {

change $_UNIT = "%" to $_UNIT -eq "%"

Author Closing Comment

ID: 39288503

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now