1

I wrote a function to convert the input month parameter to a certain format. such as if I passed 04 to the function and the function will return the "_APR_". the function I wrote is like below:

function GetEnMonth()
{
param([string] $month)
switch ($month)
{
    ($_ -eq "01"){$result = "_JAN_"}
    ($_ -eq "02"){$result = "_FEB_"}
    ($_ -eq "03"){$result = "_MAR_"}
    ($_ -eq "04"){$result = "_APR_"}
    ($_ -eq "05"){$result = "_MAY_"}
    ($_ -eq "06"){$result = "_JUN_"}
    ($_ -eq "07"){$result = "_JUL_"}
    ($_ -eq "08"){$result = "_AUG_"}
    ($_ -eq "09"){$result = "_SEP_"}
    ($_ -eq "10"){$result = "_OCT_"}
    ($_ -eq "11"){$result = "_NOV_"}
    ($_ -eq "12"){$result = "_DEC_"}
    default {$result ="_No_Result_"}
}
return [string]$result;
} 

Then I use below command to execute the function to get the result:

$mYear = $today.substring(0,4) 
$mMonth =$today.substring(4,2)
$mDate = $today.substring(6,2)
$monthInEn = GetEnMonth $mMonth

well, the result is always "_No_Result_", why? below is the exception:

 **** Exception type : System.Management.Automation.RuntimeException
 **** Exception message : You cannot call a method on a null-valued expression.

Could anyone give me an answer for this? I have searched Google a lot but don't find useful solutions.

2
  • How you give value to $today? Commented Apr 10, 2012 at 10:01
  • this parameter is mannually operated. we pass previous date to it, suce as 20120409. Commented Apr 10, 2012 at 10:02

4 Answers 4

3

This should work:

function GetEnMonth
{
param([string] $month)
switch ($month)
{
    "01"{$result = "_JAN_"}
    "02"{$result = "_FEB_"}
    "03"{$result = "_MAR_"}
    "04"{$result = "_APR_"}
    "05"{$result = "_MAY_"}
    "06"{$result = "_JUN_"}
    "07"{$result = "_JUL_"}
    "08"{$result = "_AUG_"}
    "09"{$result = "_SEP_"}
    "10"{$result = "_OCT_"}
    "11"{$result = "_NOV_"}
    "12"{$result = "_DEC_"}
    default {$result ="_No_Result_"}
}
return [string]$result;
} 
Sign up to request clarification or add additional context in comments.

Comments

3

Here's my take on this. I changed the parameter type to Int, with this you'll be able to get process such as "03" or 3. I also added break statements to make the switch work faster.

function GetEnMonth
{
    param([int] $month)

    switch ($month)
    {
        1 {"_JAN_"; break}
        2 {"_FEB_"; break}
        3 {"_MAR_"; break}
        4 {"_APR_"; break}
        5 {"_MAY_"; break}
        6 {"_JUN_"; break}
        7 {"_JUL_"; break}
        8 {"_AUG_"; break}
        9 {"_SEP_"; break}
        10 {"_OCT_"; break}
        11 {"_NOV_"; break}
        12 {"_DEC_"; break}
        default {"_No_Result_"}
    }
} 

Comments

2

Your switch statment is wrong try this:

function GetEnMonth()
{
param([string] $month)
switch ($month)
{
    "01" {$result = "_JAN_"}
    "02" {$result = "_FEB_"}
    "03" {$result = "_MAR_"}
    "04" {$result = "_APR_"}
    "05" {$result = "_MAY_"}
    "06" {$result = "_JUN_"}
    "07" {$result = "_JUL_"}
    "08" {$result = "_AUG_"}
    "09" {$result = "_SEP_"}
    "10" {$result = "_OCT_"}
    "11" {$result = "_NOV_"}
    "12" {$result = "_DEC_"}
    default {$result ="_No_Result_"}
}
return [string]$result;
} 

5 Comments

well, Could you pls tell me $_ means what?
This is the variable for the current value in the pipe line: es: 1..10 | Foreach-object { write-host $_ } prints numbers from 1 to 10 in the screen. $_ is also the current argument to the current script block executed
think that it's like the $(this) in jquery when using each function, right?
try get-help about_automatic_variables to know more about automatics variables in powershell.
@CharlieShi Your example will work if you change ($_ -eq "XX") to {$_ -eq "XX"} The $_ refers to the current item from the $month variable at the top of the switch statement. Switch statements can take entire arrays as arguments. $_ will refer to each element as one element at a time is passed through the switch.
2

This is not an answer, but another way to do the same in one line and with a culture param:

$month = 3
$smonth=[string]::format([System.Globalization.CultureInfo]"en-US", "_{0:MMM}_",[datetime]("$month/01"))
$smonth
_MAR_

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.