header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

quick witch switch statement
Last Post 17 Aug 2008 08:46 AM by Shay. 7 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
16 Aug 2008 01:45 AM  

Subject should have said, "Quick issue with switch statement."  Apparently we can't edit our subjects.

With that said, the issue is with the following code:

$today = Get-Date
switch ($today.day) {
(15) { Write-Host Today is the 15th }
(16) { Write-Host Today is the 16th }
($today.day -eq 15) { Write-Host Greater than 13 }
}

Why isn't the last statement working.  15 is the correct answer here (at least at the current moment it is).  When (15) hits, it successfully displays that Today is the 15th.  But when it gets to ($today.day -eq) it doesn't Write-Host.  I've tried varations like ($today.day -gt 13) and none of that works.  Why won't this work?

Thanks.

Elan Shudnow
http://www.shudnow.net
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
16 Aug 2008 03:29 AM  
While I could edit the subject, I kinda like the typo. :D

The answer to your question is that "$today.day" will be an [int] type, the number indicating the day. That will never equal the results of your 3rd statement which will be either $true or $false depending on the date--but it'll never be "15".

Your logic in the switch statement does not quite make sense to me, so I'm not sure how to re-write the last part.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
16 Aug 2008 04:22 AM  
Ah I see it's being returned as $true. I'm thinking from a perspective if wanting to do an if statement within a swtich statement which really just won't work in my scenario. Thanks again.

And expect many more novice type questions from me. I'm just starting out scripting (no scripting knowledge).
Elan Shudnow
http://www.shudnow.net
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:334
Avatar

--
16 Aug 2008 04:29 AM  
Well I didn't say it could not be done, just that I didn't know what you wanted done so as to give you a working example. :)
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
16 Aug 2008 04:56 AM  

Well, after what you told me, I did get it working by doing:

if ($today.day -eq 15) { $todayReturn = 15 }
$today = Get-Date
switch ($today.day) {
(15) { Write-Host Today is the 15th }
(16) { Write-Host Today is the 16th }
($todayReturn) { Write-Host Greater than 13 }
}

But even doing this is pointless since I already have 15 in there, that would work.  I'm just looking at what things work and what doesn't work with variables and using if statements, switch statements to kind of learn what can be done and what can't. 

Elan Shudnow
http://www.shudnow.net
ShayUser is Offline
Basic Member
Basic Member
Posts:271
Avatar

--
16 Aug 2008 06:49 PM  

It would validate if you put it in {} braces:

{$today.day -eq 15} { Write-Host Greater than 13 }

 

BTW, the switch value (e.g $today.day) is available as $_ inside the switch statement, so this is valid:

{$_ -eq 15}  { Write-Host Greater than 13 }

 

Further more, you can even do regex inside:

{$_ -match '\d{2}'}  { Write-Host 'Greater than or equel to 10'}

 

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
17 Aug 2008 07:25 AM  
Thanks Shay. Surprised to see that a {something} {something} would work. I always thought it would need to be (condition) { code execution }. Guess not!

I did try doing the following though:
($today.day -eq 16) which returned true
{$today.day -eq 16} which returned nothing. Granted your code works, but shouldn't this have returned 16 since it does indeed work with your code?

Elan Shudnow
http://www.shudnow.net
ShayUser is Offline
Basic Member
Basic Member
Posts:271
Avatar

--
17 Aug 2008 08:46 AM  

The result can be whatever you choose it to be as long as the -eq part is evaluated to $true and is enclosed in {} braces. Notice that the expression inside the () parenthesis is not evaluated to $true.


PS > switch ($today.day) {
>>     15 { Write-Host '1. Today is the 15th' }
>>     17 { Write-Host '2. Today is the 17th' }
>>     ($today.day -eq 17) { Write-Host '1. Greater than 13' }
>>     {$_ -gt 13} { Write-Host '2. Greater than 13' }
>>     {$_ -eq 17} { Write-Host $true }
>>     {$_ -gt 16} { Write-Host $false }
>> }
>>
2. Today is the 17th
2. Greater than 13
True
False

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer