Solved

problem with several loops on script

Posted on 2013-06-04
2
208 Views
Last Modified: 2013-06-30
In a txt file $MONITORING_ENV, there are the following lines :
k:\msql\:DISK:5%:8%
I:\backup:DISK:20%:40%

Hello

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	  

ForEach($DISK in $LIST_SPEC_DISK) {

  [double] $free_sz=$DISK.FreeSpace
  $FREE_SZ=[int]($free_sz/(1024*1024))


  $MOUNT_POINT=$DISK.NAME
  
  $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 "%") {

   
  $_SPEC_WARN_THRES = $SPEC_DISK.WARNING.REPLACE("%","")
	

  $_SPEC_PANIC_THRES = $SPEC_DISK.CRITICAL.REPLACE("%","")
	

  $_UNIT = "%"

  }
    
  elseif ($SPEC_DISK.CRITICAL -match "M") {
   
  $_SPEC_WARN_THRES = $SPEC_DISK.WARNING.REPLACE("M","")

  $_SPEC_PANIC_THRES = $SPEC_DISK.CRITICAL.REPLACE("M","")

  $_UNIT = "M"
	
   }
    $SPEC_MOUNT_POINT=$SPEC_DISK.DISK

  }
  
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) { 
	
$DISK_STATUS="DBMS[DISASTER]:: Freespace in $MOUNT_POINT $FREE_PCT % < PANIC level $_SPEC_PANIC_THRES %"

 }
}

Open in new window


How can I resolve ?

Thanks
0
Comment
Question by:bibi92
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
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 "%") {

   
  $_SPEC_WARN_THRES = $SPEC_D.WARNING.REPLACE("%","")
	

  $_SPEC_PANIC_THRES = $SPEC_D.CRITICAL.REPLACE("%","")
	

  $_UNIT = "%"

  }
    
  elseif ($SPEC_D.CRITICAL -match "M") {
   
  $_SPEC_WARN_THRES = $SPEC_D.WARNING.REPLACE("M","")

  $_SPEC_PANIC_THRES = $SPEC_D.CRITICAL.REPLACE("M","")

  $_UNIT = "M"
	
  }
    $SPEC_MOUNT_POINT=$SPEC_D.DISK

  }

Open in new window


In line 37...

$SPEC_DISK.CRITICAL -match "M"
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 "%"
0
 

Author Closing Comment

by:bibi92
Comment Utility
Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article will help you understand what HashTables are and how to use them in PowerShell.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

10 Experts available now in Live!

Get 1:1 Help Now