Let me ask how to increment date correctly in PowerShell. We'd like to get date list in format 'yyyy-mm-dd' in designated period, to pass them to other programs.
For example, when DateStart='2018-01-05' and DateEnd='2018-01-08', then the date list should be;
2018-01-05
2018-01-06
2018-01-07
2018-01-08
Currently, my script can work as long as the designated period in the same month, like above case, but can't work in different month.
#Get_DateRange.ps1
param(
[String] $DateStart,
[String] $DateEnd
)
# Variables Set
$StartDT = [Datetime]::ParseExact($DateStart,"yyyy-mm-dd",$null).AddDays(-1)
$EndDT = [Datetime]::ParseExact($DateEnd,"yyyy-mm-dd",$null)
# Return Date List during designated period
do
{
$StartDT = $StartDT.AddDays(1)
$StartDT.toString("yyyy-mm-dd")
} While ($StartDT -lt $EndDT)
As wrong case, when DateStart='2018-01-31' and DateEnd='2018-02-05', it only returned '2018-01-31'.
I understand obviously something wrong with my script, but so far I have no idea. Any advice would be appreciated again.
Thank you.
Additional Info: We'd like to use above script in other batch like this;
FOR /F %%d IN ('%PS_EXE% -ExecutionPolicy ByPass -File "Get_DateRange.ps1" -DateStart %EXT_START_DT% -DateEnd %EXT_END_DT%') DO (
@ECHO %FILE_ID%,%%d,%TARGET_DB%,%TARGET_SCHEMA%,%TARGET_TABLE%,'%%d',gz,%TARGET_DB%,%TARGET_TABLE%>>%EXT_FILE_NAME%
)