Solved

Powershell if statement question

Posted on 2016-08-02
4
56 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 82

Accepted Solution

by:
oBdA earned 250 total points
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
@ 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 82

Expert Comment

by:oBdA
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

9 Experts available now in Live!

Get 1:1 Help Now