'Powershell remove prompt, but run 7 days back to yesterday

Currently I have a powershell that was written by someone else. It Connects to SQL and based on a date it will export the results against a Stored procedure...This Works fine Right now.

$FromDate = Read-Host "Enter Date of to Start (YYYY-MM-DD)" 
$QueryText = "exec UDO_VW_ExportView_HISTORY @FromDate"
 
$SqlConnection = new-object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $connString
$SqlCommand = $SqlConnection.CreateCommand()
$SqlCommand.CommandText = "EXEC UDO_VW_ExportView_HISTORY @FromDate"

# Add parameters to pass values to the stored procedure
$SqlCommand.Parameters.AddWithValue("@FromDate", $FromDate) | Out-Null

$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $SqlCommand
$dataset = new-object System.Data.Dataset

Write-Host $DataAdapter.Fill($dataset) ' records have been exported.'

$dataset.Tables[0] | Export-CSV D:\_#Scripts\Daily_Export_HISTORY\Daily_Report_Txt\MyReport.csv  -NoTypeInformation -Force
 
Write-Host 'New report D:\_#Scripts\Daily_Export_HISTORY\Daily_Report_Txt\MyReport.csv has been successfully generated'
(gc D:\_#Scripts\Daily_Export_HISTORY\Daily_Report_Txt\MyReport.csv) | % {$_ -replace '"""', '"'} | out-file D:\_#Scripts\Daily_Export_HISTORY\Daily_Report_Txt\Daily_Report_For_$FromDate.csv -Fo -En ascii

Remove-Item D:\_#Scripts\Daily_Export_HISTORY\Daily_Report_Txt\MyReport.csv

As you can see (i hope) the first line is:

$FromDate = Read-Host "Enter Date of to Start (YYYY-MM-DD)" 

I just want it to get all transactions 7,6,5,4,3,2 days, and Yesterday as one export.

I also see in the code there are declared variables that may also be modified. Can you please look at this coed and provide an updated code that will achieve what I'm trying to do. I'm new to Powershell

Thank you so much!



Solution 1:[1]

You can replace :

$FromDate = Read-Host "Enter Date of to Start (YYYY-MM-DD)" 

by

$FromDate = ([datetime]::Now).AddDays(-7).Date.ToString("yyyy-MM-dd") 

It takes today date and time, remove 7 days, remove time and format it.

Solution 2:[2]

Looks like I needed to update the nuget package, and then restart my solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JPBlanc
Solution 2 GHarland