Solved

Powershell if statement question

Posted on 2016-08-02
4
76 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 83

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 83

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 83

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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
Troubleshooting common task sequence error codes
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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