Solved

build variables from values on txt file

Posted on 2013-05-29
11
248 Views
Last Modified: 2013-05-30
A txt file monitoring.env contains a list of disk with specifical thresold for monitoring

k:\\data\\sql\\:FS:80%:90%
k:\\data1\\sql\\:FS:80%:90%

How can I get the values 80% and 90% and put them in two variables thres_warning and thres_critical?

Thanks
0
Comment
Question by:bibi92
  • 5
  • 3
  • 2
11 Comments
 
LVL 39

Expert Comment

by:footech
ID: 39206251
This will do that.
Get-Content monitoring.env |ForEach `
{
  $_ -match ":(\d{2})%:(\d{2})%"
  $thres_warning = $matches[1]
  $thres_critical = $matches[2]
}

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39206256
There are several methods. We could split by colon, and take the 4th and 5th field:
get-content monitoring.txt | % {
 $vals = $_ -split ':'
 # other stuff
 $thres_warning = $vals[3] -replace '%'
 $thres_critical = $vals[4] -replace '%'
}

Open in new window

0
 

Author Comment

by:bibi92
ID: 39206967
Hello,

I try the code suggested by qlemo and footech, it doesn't work.
Thanks
0
 
LVL 39

Expert Comment

by:footech
ID: 39207006
I've verified that both work.  If you're seeing a problem my best guess is it would be because of how you're trying to integrate the code with whatever else you have.
0
 

Author Comment

by:bibi92
ID: 39207116
Hello,

I have modified like this :
$MONITORING = "d:\monitoring.txt"
$SPEC_FS=$MONITORING | %{ gc $_| where-object {$_ -like("*:FS:*%")}
$array_str = $spec_fs.split(":")
$fs_name = $array_str[0]
$warn_thresold = $array_str[2]
$critical_thresold  = $array_str[3]
0
IT, Stop Being Called Into Every Meeting

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!

 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39207174
This can't work for several reasons.
With
  $SPEC_FS=$MONITORING | %{ gc $_| where-object {$_ -like("*:FS:*%")}
you are creating an array of strings, one element per line.
Then you split that array - you get an array which is split at each line break and on colons. The array is mixed up, and you can't reliably tell which element corresponds to which field anymore (ok, you can: since you know there are 5 colon-separated fields, you can add a offset of 5 per line, but that is really, really too much).
In addition to that, you need to keep the name and thresholds for each line, while you only ask for the first one in your recent code.
The thresholds got with -split / .Split() will still have their trailing percent, so we have to remove that.
And last you used the wrong index for the thresholds. Note that
   k:\\data\\sql\\:
are two fields, "k" and "\\data\\sql\\".

I recommend to store the data in a custom object for easy handling:
$MONITORING = "d:\monitoring.txt"
$thresholds = @()
get-content $MONITORING | ? { $_ -like '*:FS:*%' } | % {
  $val = $_ -split ':' -replace '%'
  $thresholds += New-Object PsObject -Property @{
    fs       = $val[0]+':'+$val[1]
    warning  = $val[3]
    critical = $val[4]
  }
}
$thresholds

Open in new window

That will also work if you provide multiple files in $MONITORING.
0
 
LVL 39

Expert Comment

by:footech
ID: 39207224
@bibi92 - I have to make a side comment.  In my opinion it's very poor practice to say that a proposed solution doesn't work, when what you have is barely like what was proposed.  Please try what was proposed with as few modifications as possible to see if it works before changing everything.  Once you've verified the concept works then you can adapt it to any existing script you have.  This helps to cut down on wasted effort by those contributing.  Thanks.
0
 

Author Comment

by:bibi92
ID: 39207246
Exactly you are right. My bad english doesn't help me to explain correctly.
Thanks a lot Regards
0
 

Author Closing Comment

by:bibi92
ID: 39207366
$MONITORING = "d:\monitoring.txt"
$thresholds = @()

get-content $MONITORING| ? { $_ -like '*:FS:*%' } | % {
  $val = $_ -split ':' -replace '%'
  $thresholds += New-Object PsObject -Property @{
    fs       = $val[0]+':'+$val[1]
    warning  = $val[2]
    critical = $val[3]
  }
}
$thresholds

and I obtain the result

warning                                                     fs                                                          critical
-------                                                     --                                                          --------
80                                                          k:\sql\data\:FS                                                90
0
 

Author Comment

by:bibi92
ID: 39207827
Exactly thanks a lot. Regards
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

16 Experts available now in Live!

Get 1:1 Help Now