Solved

problem with several loops on script

Posted on 2013-06-04
2
223 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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