2012-04-10 110 views
1

我寫了一個函數將輸入的月份參數轉換爲某種格式。例如如果我將04傳遞給該函數並且該函數將返回「_APR _」。我寫的函數是象下面這樣:如何正確調用powershell函數?

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; 
} 

然後我用下面的命令來執行的函數來得到結果:

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

好,結果始終是「_No_Result_」,爲什麼呢?下面是例外:

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

有人能給我這個答案嗎? 我已經搜索了很多Google,但沒有找到有用的解決方案。

+0

如何給今日$的價值? – 2012-04-10 10:01:52

+0

該參數是手動操作的。我們通過之前的日期,截至20120409。 – CharlieShi 2012-04-10 10:02:59

回答

2

你的switch敘述是錯誤試試這個:

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; 
} 
+0

嗯,請你告訴我$ _是什麼意思? – CharlieShi 2012-04-10 10:45:17

+0

這是管道中當前值的變量:es:1..10 | Foreach-object {write-host $ _}在屏幕上打印1到10之間的數字。 $ _也是執行當前腳本塊的當前參數 – 2012-04-10 10:49:30

+0

認爲它就像使用每個函數時的jquery中的$(this),對嗎? – CharlieShi 2012-04-10 10:51:36

3

這應該工作:

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; 
} 
+0

thx,正確的答案,但我只選擇一個。對不起.... – CharlieShi 2012-04-10 10:57:26

+1

+1你回答2分鐘前;) – 2012-04-10 11:05:25

2

這不是一個答案,但另一種方式做同在一行,並與文化param:

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

這是我對此的看法。我將參數類型更改爲Int,使用這個參數可以獲得諸如「03」或3的過程。我還添加了break語句以使交換機工作更快。

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_"} 
    } 
} 
+0

+1用於刪除'$ result'變量。 – Rynant 2012-04-10 13:16:09