Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active December 1, 2017 17:41
Show Gist options
  • Select an option

  • Save gitfvb/cd0c7cfa0c7e28f4e92e55cd04e846c9 to your computer and use it in GitHub Desktop.

Select an option

Save gitfvb/cd0c7cfa0c7e28f4e92e55cd04e846c9 to your computer and use it in GitHub Desktop.
powershell json restapi csv
# several commands via Invoke-RestMethod
$request = 'https://piwikdomain/index.php?module=API&method=Live.getLastVisitsDetails&idSite=1&period=day&date=today&format=JSON&token_auth=<token>'
$RestCall = Invoke-RestMethod -Uri $request
$RestCall | Export-Csv -Delimiter "`t" -Encoding "UTF8" -path whatever1.csv -NoTypeInformation
# single line with pipes
$request = 'https://piwikdomain/index.php?module=API&method=Live.getLastVisitsDetails&idSite=1&period=day&date=today&format=JSON&token_auth=<token>'
( Invoke-RestMethod -Uri $request ) | Select * | Export-Csv -Delimiter "`t" -Encoding "UTF8" -path whatever2.csv -NoTypeInformation
# alternative calls via Invoke-WebRequest
$request = 'https://piwikdomain/index.php?module=API&method=Live.getLastVisitsDetails&idSite=1&period=day&date=today&format=JSON&token_auth=<token>'
$result = Invoke-WebRequest $request
$visits = $result.Content | ConvertFrom-Json
$visits | Select *
$visits | Export-Csv -Delimiter "`t" -Encoding "UTF8" -path whatever3.csv -NoTypeInformation
# alternative as one-liner
( Invoke-WebRequest $request | ConvertFrom-Json ) | Select * | Export-Csv -Delimiter "`t" -Encoding "UTF8" -path whatever4.csv -NoTypeInformation
( Invoke-RestMethod -Uri $request ) | Select idVisit -expand customVariables | Select *, @{Name='Orga';Expression={ ($_.1).customVariableValue1 }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment