 |
|
| IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012PowerShellCommunity.org is moving! This community software, and the hardware that it sits on, are no longer serving the purposes of this community. As a result, we have decided to move this community to a new home at PowerShell.org. PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site. We've already started getting some great questions from members of the community over there so please, come on over and join us!
While we are going through this transition, this site will remain up for the short term. New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.
[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.
If you have any questions, please let us know on the PowerShell.org site.
Thank you,
Kirk "Poshoholic" Munro |
|
|
|
|
Compare percentages and values
Last Post 23 Dec 2011 06:38 AM by Fiona. 6 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
| Author |
Messages |
 |
Fiona
 New Member Posts:6

 |
| 23 Dec 2011 06:04 AM |
|
Hello, I need to compare a percentage with a number. Since I'm really a beginner in powershell I don't know how I can do this. When I write : if (pid.value -ge 20) it doesn't return the expected response. Since pid.value returns a percentage (3.56% for example) I don't know how to compare it with a number If I write pid.value ge 20% Will it work? Or how can I do it otherwise? Thanks in advance Fiona |
|
|
|
|
Marco Shaw
 Veteran Member Posts:1684

 |
| 23 Dec 2011 06:10 AM |
|
Do you have an example that anyone can run? PowerShell will not properly interpret percentages usually. You will have to transform the value into a regular number. |
|
|
|
|
Fiona
 New Member Posts:6

 |
| 23 Dec 2011 06:21 AM |
|
The example is the one I'm talking about above: $value=$mypid.Value if($mypid.Value -ge 20){ Write-Output "User $baduser is using $badprocess, PID: $key, with $value of resources!" | Out-File -filePath "$file_name.txt" -encoding UTF8 -append -noClobber } Like this,I don't get any error but my file contains values like 3.6 (but no 0.5%) I thought about using a cast like this: [float] $value -ge 20 But I don't know if it will work [Edit] Just tried the float, doesn't work |
|
|
|
|
Marco Shaw
 Veteran Member Posts:1684

 |
| 23 Dec 2011 06:23 AM |
|
Where does $mypid come from? Can you provide a complete example? |
|
|
|
|
Fiona
 New Member Posts:6

 |
| 23 Dec 2011 06:26 AM |
|
Sorry, here it is $a=Get-WmiObject Win32_Processor | select LoadPercentage # Snapshot des process en cours $procBefore=Get-Process # On attend un peu... sleep -Seconds 1 # On reprend un snapshot $procAfter=Get-Process #Création du nom de fichier (log+date) $date= Get-Date $file_name = "{0}_{1:d2}_{2:d2}" -f $date.year,$date.month,$date.day $dict = @{} foreach ($p in $procBefore) { # Correspondance des process avant et après # stockée dans $z $procAfter | where {$_.id -eq $p.id} -OutVariable z | Out-Null $usage=(1-($p.WorkingSet64)/($z[0].WorkingSet64)) $usagepercent="{0:P2}" -f $usage if ($usage -gt 0) { $dict.Add($p.id, $usagepercent) } } #Affichage de l'heure d'execution $dict = $dict.GetEnumerator() | Sort-Object -Descending Value foreach ($mypid in $dict) { $baduser = (Get-WmiObject win32_process | where {$_.ProcessId -eq $mypid.Key}).getowner().User $badprocess = (Get-WmiObject win32_process | where {$_.ProcessId -eq $mypid.Key}).Name $key=$mypid.Key $value=$mypid.Value if($mypid.Value -ge "20%"){ Get-Date -displayhint time | Out-File -filePath "$file_name.txt" -encoding UTF8 -append -noClobber Write-Output "User $baduser is using $badprocess, PID: $key, with $value of resources!" | Out-File -filePath "$file_name.txt" -encoding UTF8 -append -noClobber } } exit(1)
|
|
|
|
|
Marco Shaw
 Veteran Member Posts:1684

 |
| 23 Dec 2011 06:34 AM |
|
Sorry, that's a bit too long for me to cycle through, so here's an example: $perc=Get-WmiObject Win32_Processor | select -exp LoadPercentage if($perc -gt 1){"Over 1"} It all depends on what you're comparing, but likely you are comparing integer values. To try this: ... if($mypid.Value -ge "20%"){ ... You need to look at $mypid.value in detail. It could be an integer or a string. For example, it might be: ... if($mypid.Value -ge 20){ ... Or maybe: ... if([int]$mypid.Value -ge 20){ ... |
|
|
|
|
Fiona
 New Member Posts:6

 |
| 23 Dec 2011 06:38 AM |
|
Ok thank you i'll try it Since if($mypid.Value -ge 20) doesn't work I'll try the other one [Edit] doesn't work either, it returns an error |
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |