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
Solved

Powershell if statement question

Posted on 2016-08-02
4
86 Views
Last Modified: 2016-08-02
Whats the best way to write an if statement for this please. I think I might be doing something wrong here with the code.


$mailstats = 5000
IF ($mailstats -GT  6000) {

code injected here

}

Else

IF ($mailstats -GT  4000 -and $mailstats -lt 5999 ) {

code injected here

}

else

if ($mailstats -GT  3000 -and $mailstats -lt 3999 ) {

code injected here

}
0
Comment
Question by:techdrive
  • 3
4 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 41739058
If you go down from largest to smallest, you only need to check the -gt part, because the -lt part will have been handled in the if statement before.
$mailstats = 5000
if ($mailstats -GT  6000) {
	# code injected here
} ElseIf ($mailstats -GT  4000) {
	# code injected here
} ElseIf ($mailstats -GT  3000) {
	# code injected here
} Else { ## -le 3000
	# code injected here
}

Open in new window

0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 250 total points
ID: 41739081
Hi techdrive
The 'Switch' statement is also another approach that you could use - please see sample below:
$mailstats = 5000

switch ($mailstats)
{
    {$mailstats -gt 6000}{Write-Host "Mail Stats > 6000"}
    {$mailstats -gt 4000 -and $mailstats -lt 5999}{Write-Host "Mail Stats > 4000"}
    {$mailstats -gt 3000 -and $mailstats -lt 3999}{Write-Host "Mail Stats > 3000"}

}

Open in new window

0
 
LVL 84

Expert Comment

by:oBdA
ID: 41739110
@ techdrive and Barry:
Both of your scripts will fail for mailstats with sizes of 3999, 4000, 5999, 6000, which is the danger of specifying full ranges.
The correct comparisons would be
if ($mailstats -GT  4000 -and $mailstats -le 6000) {
if ($mailstats -GT  3000 -and $mailstats -le 4000) {
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41739169
Maybe some more notes on this.
To do this in a switch without ranges, the "break" statement is required; in a Switch, Powershell continues to check expressions even after it finds match (except for 'default', which will only be processed if no other expression matched).
$mailstats = 5000
Switch ($mailstats) {
	{$_ -gt 6000} {
		"$($_) is -gt 6000"
		Break
	}
	{$_ -gt 4000} {
		"$($_) is -gt 4000"
		Break
	}
	{$_ -gt 3000} {
		"$($_) is -gt 3000"
		Break
	}
	default {	## -le 3000
		"$($_) is -le 3000"
	}
}

Open in new window

And integer ranges can be specified as <Start>..<End> as well, so if ranges are a must, something like this is easier readable:
$mailstats = 5000
If ($mailstats -gt 6000) {
	"$($mailstats) is -gt 6000"
} ElseIf ($mailstats -in 4001..6000) {
	"$($mailstats) is -in 4001..6000"
} ElseIf ($mailstats -in 3001..4000) {
	"$($mailstats) is -in 3001..4000"
} Else {
	"$($mailstats) is -le 3000"
}

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
An article on effective troubleshooting
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

837 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