Solved

problem with several loops on script

Posted on 2013-06-04
2
228 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
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 "%") {

   
  $_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
ID: 39288503
Thanks
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

789 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