我正在尝试将参数传递到我的脚本中:
# 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/