r/PowerShell • • Mar 26 '20

Script Sharing Script to aggregate United States reported COVID-19 numbers.

Please note that each state reports differently, so the numbers may not be correct. All data is taken from The COVID Tracking Project, which has an API that outputs JSON. Inspiration taken from this thread on /r/homeassistant.

If someone wants to improve this script, by all means do so. This is my rudimentary way, with my skillset.

Here is a sample output of a single state, I chose New York since it is the highest currently.

state                : NY
positive             : 30811
positiveScore        : 1
negativeScore        : 1
negativeRegularScore : 0
commercialScore      : 1
grade                : B
score                : 3
negative             : 72668
pending              :
hospitalized         : 3805
death                : 285
total                : 103479
lastUpdateEt         : 3/25 11:45
checkTimeEt          : 3/25 13:12
dateModified         : 2020-03-25T15:45:00Z
dateChecked          : 2020-03-25T17:12:00Z

  

And here is the script itself that will loop through all 50 states, pull the data for each one, replace any null values with zeros (personal preference, can be removed), and format it into a table. It can also be output to a CSV.

  

$call = Invoke-RestMethod -uri https://covidtracking.com/api/states
$call | select state,positive,negative,@{n="pending";e={if($_.pending -eq $null){"0"}else{$_.pending}}},@{n="hospitalized";e={if($_.hospitalized -eq $null){"0"}else{$_.hospitalized}}},@{n="death";e={if($_.death -eq $null){"0"}else{$_.death}}},total | ft -autosize

EDIT: Condensed into smaller script, didn't realize making a generic call without specifying state would pull all states.

5 Upvotes

2 comments sorted by

2

u/SMFX Mar 26 '20

rather than iterating through the list of states and doing 50 REST calls, you can make one an outdated through the array the results :

https://covidtracking.com/api/states

4

u/Sunsparc Mar 26 '20

I always manage to over-engineer stuff. Updating.

I did try piping the invocation directly into select, but it would only output a single line with no data so I still put it into a variable and then piped the variable into select.