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

标签 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

最佳答案

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

[int]$int = 02
$int

将显示2

您可以将其重新格式化为字符串。
[int]$int = 02
$int.ToString("00")

将显示02

关于powershell - PowerShell-参数[Int32] $ Month当其应具有前导0时显示为单个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51027160/

相关文章:

Powershell 无法从命令提示符创建 Outlook COM 对象

mysql - 自动 Access DB5到MySQL

windows - 运行 postgres 容器时出错 "Error response from daemon: invalid mode:/var/lib/postgresql/data."

sql-server - 使用Powershell脱机设置数据库

powershell 比较两个数组,创建只有不匹配值的第三个数组

powershell - 管理员组的 SID 不显示在成员帐户中

sql-server - Jenkins 的单元测试

Powershell数组的ForEach不返回System.array?

powershell - Azure AD 模块和 MS Online 模块有什么区别

xml - PowerShell 连接多个 XML 文件