PowerShell-参数[Int32] $ Month在应有前导0时显示为单个整数

时间:2018-06-25 15:35:29

标签: powershell

我正在尝试将参数传递到脚本中:

# Get current Year and Month
param ([Int32]$Year, [Int32]$Month, [String]$Environment, [String]$Report, [String]$Username, [String]$Password)

function Get-DaysOfMonth {
    Param ([Int32]$Year, [Int32]$Month)
    $MonthDays = New-Object System.Collections.Generic.List[System.Object]
    1..([datetime]::DaysInMonth($Year, $Month)) |
        ForEach-Object {
           $Day = ([datetime]::ParseExact("$_/$Month/$Year", "d/M/yyyy", [System.Globalization.CultureInfo]::InvariantCulture)).ToString("dd")
           $MonthDays.Add($Day)
        }
    return $MonthDays
}

Write-Host ("The Year Selected is: {0}" -f $Year)
Write-Host ("The Month Selected is: {0}" -f $Month)
Write-Host ("The Environment Selected is: {0}" -f $Environment)
Write-Host ("The Report Generated is: {0}" -f $Report)
Write-Host ("Export Generating as User: {0}" -f $Username)

问题是从命令行运行以下内容:

myscript.ps1 -Year 2018 -Month 02 -Environment PROD -Report event -Username myname -Password mypass

它正在将02解析为2

1 个答案:

答案 0 :(得分:4)

02不是整数。分配给int的数字将删除0。

entities %>% 
         filter(grepl(paste(input$select_state, collapse="|"), IndividualName))

将显示2

您可以将其重新格式化为字符串。

[int]$int = 02
$int

将显示02