Solved

Powershell if statement question

Posted on 2016-08-02
4
114 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
[X]
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
  • 3
4 Comments
 
LVL 85

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 85

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 85

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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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