Reputation: 169
I'm new to Powershell and trying to write a script that:
I've gotten stuck on 2 and 3. I am doing this:
$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"`
But the output shows $InvMonth
, $InvDay
, and $InvYear
as System.Object[]
. How do I properly typecast these to be usable? I've searched for a few hours and not find an answer that works.
Upvotes: 2
Views: 232
Reputation: 126752
Give this a try, for day and month it will prompt until it gets a valid value :
$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"
Upvotes: 0
Reputation: 37730
As I noted you have a lot of syntax issues with the code that you posted. Here is the code with the syntax cleaned up and functioning the way that I think you wanted it to:
$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"
Upvotes: 1