user1467163
user1467163

Reputation: 169

Powershell script- Filenaming, datetime and FTP

I'm new to Powershell and trying to write a script that:

  1. Prompts for an invoice number
  2. Prompts for the day, then month, then year for the invoice
  3. If any of the values in 2 are null, then it uses a value for the day the script is run for that value.
  4. Concatenates that information with some seperator characters with the invoice number into a filename and renames a given file ("test.txt" in this case)
  5. Creates a quick one-line report that indicates the filename.

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

Answers (2)

Shay Levy
Shay Levy

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

EBGreen
EBGreen

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

Related Questions