Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

problem with several loops on script

Posted on 2013-06-04
2
Medium Priority
?
250 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 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 "%") {

   
  $_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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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 anti-spam), the admin…
Loops Section Overview

885 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