Solved

build variables from values on txt file

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

 
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

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

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

22 Experts available now in Live!

Get 1:1 Help Now