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

x
?
Solved

build variables from values on txt file

Posted on 2013-05-29
11
Medium Priority
?
292 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
10 Comments
 
LVL 41

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 71

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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 41

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 71

Accepted Solution

by:
Qlemo earned 2000 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 41

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
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…

824 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