Solved

build variables from values on txt file

Posted on 2013-05-29
11
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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
Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

 
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 70

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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
A brief introduction to what I consider to be the best editor for PowerShell.
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

626 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