Solved

build variables from values on txt file

Posted on 2013-05-29
11
281 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 40

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 69

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
Problems using Powershell and Active Directory?

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

 
LVL 40

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
 
LVL 69

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 40

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Matching variables and Compare-Object 24 51
How to use Powershell data from SQL 151 57
WPF Select Items 3 24
Query inactive computers in AD. 5 18
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article will help you understand what HashTables are and how to use them in PowerShell.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
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 antispam), the admini…

679 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