Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


problem with several loops on script

Posted on 2013-06-04
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 40

Accepted Solution

Subsun earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

660 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