Mizex
Mizex

Reputation: 65

Append variable name and date to PowerShell file output

I am trying to put together a quick little script to help when I am using OpenSSL, for some reason I can't seem to get PowerShell to append the hostname to the front of my CSR file. It is basically giving me things like _09_26_2017.txt instead of something like hostname_09_26_2017.txt.

I have the following

$date = Get-Date -format MM_dd_yyyy
$csrName = $siteName + "_" + $date

$siteName = Read-Host "Enter the site name"

.\openssl.exe req -newkey rsa:4096 -sha256 -nodes -keyout "$siteName.pem" -out "$csrName.txt"

Any help would be greatly appreciated. Thank you very much in advance.

Upvotes: 0

Views: 4725

Answers (1)

BenH
BenH

Reputation: 10044

You define $sitename after you define $csrname. So

$csrName = $siteName + "_" + $date 

is behaving as

$csrName = $null + "_" + $date

Simply define $sitename before you try to use by moving the line up:

$date = Get-Date -format MM_dd_yyyy
$siteName = Read-Host "Enter the site name"
$csrName = $siteName + "_" + $date

.\openssl.exe req -newkey rsa:4096 -sha256 -nodes -keyout "$siteName.pem" -out "$csrName.txt"

Upvotes: 2

Related Questions