I'm having trouble with trying to output my powershell ping results into a file that has a portion of the path as a variable. How is this supposed to be done? It's bombing out at the part where I try concatenating $DATA_FOLDER with _hal and the rest. I've tried removing the and the space to no avail. If I relplace $DATA_FOLDER with C:\temp\ it works fine, it only has an issue when I try to reference it as a variable.
$IP_TARGET = Read-Host -Prompt 'Enter an IP address to ping'
$DATA_FOLDER = 'C:\temp\'
ping.exe -t $IP_TARGET | ForEach {"{0} - {1}" -f (Get-Date),$_} > $DATA_FOLDER
hal_$(Get-Date -Format "MM-dd-yyyy hh-mm").txt
CodePudding user response:
hal_$(Get-Date -Format "MM-dd-yyyy hh-mm").txt
would need to be quoted since it doesn't understand what hal_
is. It would also have to be a in a grouping expression for it to evaluate it as 1 token instead of 2.
$IP_TARGET = Read-Host -Prompt 'Enter an IP address to ping'
$DATA_FOLDER = 'C:\temp\'
ping.exe -t $IP_TARGET | ForEach {"{0} - {1}" -f (Get-Date),$_} > ($DATA_FOLDER "hal_$(Get-Date -Format 'MM-dd-yyyy hh-mm').txt")
and since the sub expression needs to be evaluated, it has to be placed in double quotes. So you have to replace the double quotes inside with single quotes if you're not going to double them up.