?
Solved

Powershell if statement question

Posted on 2016-08-02
4
Medium Priority
?
145 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 1000 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 1000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

770 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