我是Powershell的新手,并尝试编写一个脚本:
我被困在2和3上。 我这样做:
$InvoiceNumber = read-host "Enter test invoice number: "
$InvDay = $(Read-Host "Enter day of month: "
if ($InvDay){""} Else {Get-Date -format dd})
$InvMonth = $(Read-Host "Enter Month of Year: "
if ($InvMonth){""} Else {Get-Date -format MM})
$InvYear = $(Read-Host "Enter day of month: "
if ($InvYear){""} Else {Get-Date -format yy})
$InvDate = "$InvMonth + "-" + $InvDay + "-" + $InvYear"
Write-Host "InvDate is $InvDate"`
但输出显示$InvMonth
,$InvDay
和$InvYear
为System.Object[]
。如何正确地对这些进行类型转换才能使用?我搜索了几个小时,却找不到合适的答案。
答案 0 :(得分:1)
正如我所指出的,您发布的代码存在很多语法问题。这是代码,语法清理完毕,并按照我认为您希望的方式运行:
$InvoiceNumber = read-host "Enter test invoice number"
$InvDay = $(Read-Host "Enter day of month")
if ($InvDay -eq ''){$InvDay = Get-Date -format dd}
$InvMonth = $(Read-Host "Enter Month of Year")
if ($InvMonth -eq ""){$InvMonth = Get-Date -format MM}
$InvYear = $(Read-Host "Enter Year")
if ($InvYear -eq "") {$InvYear = Get-Date -format yy}
$InvDate = $InvMonth + "-" + $InvDay + "-" + $InvYear
Write-Host "InvDate is $InvDate"
答案 1 :(得分:0)
尝试一下,对于日期和月份,它会提示,直到获得有效值:
$InvoiceNumber = read-host "Enter test invoice number"
do {[int]$InvDay = Read-Host "Enter day of month (1-31)"}
while ((1..31) -notcontains $InvDay)
do {[int]$InvMonth = Read-Host "Enter Month of month (1-12)"}
while ((1..12) -notcontains $InvMonth)
[int]$InvYear = Read-Host "Enter Year"
if (!$InvYear) {$InvYear = Get-Date -format yy}
$InvDate = $InvMonth,$InvDay,$InvYear -join '-'
Write-Host "InvDate is $InvDate"