0

Attempting to download a .csv file from the net automatically each day.

I've so far setup a string $date, to use in the URL (if thats right).

Changed the TLS from 1.0 to 1.2

Make webrequest to the server, save output to file.

I just now need the $date to automatically change the URL.

Anyone know of an easy way to do this? Or do I completely need to redo the script?

Cheers,

$date = Get-Date
    [System.Net.ServicePointManager]::SecurityProtocol -bor 
[System.Net.SecurityProtocolType]::Tls12
$params = @{
    'Uri'     = @"https://website.net/v1/exportstation=32&buffer=daily&type=data&fileType=csv&grap hType=degreeDays&$(
    'start={0:yyyy-MM-dd}&end={1:yyyy-MM-dd}' -f @($date.AddDays(-1), $date)
)&from=0|7&to=30|2&yearRanges=$($date.Year)
"@
    'OutFile' = "$Env:USERPROFILE\Desktop\File.csv"
}
1
  • 1
    I'd suggest using some form of string concatenation to make your script more readable. Commented Oct 19, 2018 at 12:35

3 Answers 3

2

You can use string expands to call the $date in different formats. So below I call it 3 times, once to get yesterday's date $($Date.AddDays(-1).ToString('yyyy-MM-dd')), once to get today's date $($Date.ToString('yyyy-MM-dd')), and finally to get yesterday's year $($Date.AddDays(-1).ToString('yyyy')).

$date = Get-Date
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest ` 
    -Uri "https://website.net/v1/exportstation=32&buffer=daily&type=data&fileType=csv&graphType=degreeDays&start=$($Date.AddDays(-1).ToString('yyyy-MM-dd'))&end=$($Date.ToString('yyyy-MM-dd'))&from=0|7&to=30|2&yearRanges=$($Date.AddDays(-1).ToString('yyyy'))" `
    -Outfile C:\USERS\PC\DESKTOP\FILE.CSV

I think this should be able to get a report of yesterday if I understood what your Uri meant.

Sign up to request clarification or add additional context in comments.

Comments

1

I suggest to use a combination of


## Q:\Test\2018\10\19\SO_52891734.ps1
$Date  = (Get-Date).Date
$Start = "&start={0:yyyy-MM-dd}" -f $Date.AddDays(-1)
$End   = "&end={0:yyyy-MM-dd}"   -f $Date
$Year  = "&yearRanges={0:yyyy}"  -f $Date.AddDays(-1)
$Other = "&buffer=daily&type=data&fileType=csv&graphType=degreeDays&from=0|7&to=30|2"

$params = @{
    Uri     = "https://website.net/v1/exportstation=32$Other$Start$End$Year" 
    OutFile = "C:\USERS\PC\DESKTOP\FILE.CSV"
}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Invoke-WebRequest @params

2 Comments

You should only need to get the date once, and in your $Year example, you can simplify it to {0}
@TheIncorrigible1 If I store the date in a sep. var, yes. DONE
0

To complement the other answers and to promote a more readable solution, here is the method I would use to tackle this problem to dynamically add the date to the URL:

$date = Get-Date
[System.Net.ServicePointManager]::SecurityProtocol =
    [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
$params = @{
    'Uri'     = @"
https://website.net/v1/exportstation=32&buffer=daily&type=data&fileType=csv&graphType=degreeDays&$(
    'start={0:yyyy-MM-dd}&end={1:yyyy-MM-dd}' -f @($date.AddDays(-1), $date)
)&from=0|7&to=30|2&yearRanges=$($date.Year)
"@
    'OutFile' = "$Env:USERPROFILE\Desktop\File.csv"
}
Invoke-WebRequest @params

It is not wise to only support one protocol, so I use the bitwise-or to add TLS1.2 into the support stack.

5 Comments

This works great for me. I can understand only a little bit of it though (never coded before yesterday). Could you please explain it a bit?
Is it possible to edit the OutFile to reflect certain variables? Ideally, I'd like the "File.csv" to be called "Station#date.csv".
@Marshy I would recommend reading this article about_Splatting for explanation about the $params hashtable I define and @params syntax. about_Operators explains the -f (format) operator.
@Marshy Yes, you can change the outfile to use a variable or use a different name of your choosing. I'm not 100% if # is a valid windows path character.
'OutFile' = "$Env:USERPROFILE\Desktop\Station32 $(get-date -f yyyy-MM-dd).csv" is what I used for changing OutFile to have current date on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.