Reference:
#檔案輸出指定為ascii
$PSDefaultParameterValues['*:Encoding'] = 'Ascii'
#暫存檔主檔名
$tmpfile = "E:\Tool\tmp"
#7z位置
$7z_path = 'C:\Program Files\7-Zip\7z.exe'
$PSDefaultParameterValues['*:Encoding'] = 'Ascii'
#暫存檔主檔名
$tmpfile = "E:\Tool\tmp"
#7z位置
$7z_path = 'C:\Program Files\7-Zip\7z.exe'
#用postman測試可以抓到資料後,postman可以產生對應的powershell code
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Token XXXXXXor8mWYh259gqa1ZcMa")
$response = Invoke-RestMethod 'https://backend.com.tw/blue/raw_data/?index=blue-04&time=2022-09-11T20:00:00,2022-09-11T21:59:59' -Method 'GET' -Headers $headers
#webapi的回應為 result: value,取出 value (base64格式)
$result_base64 = $response[0].result
$out_b64 = $tmpfile + ".b64"
$result_base64 | Out-File -FilePath $out_b64 #$out_b54 for debug
#decode base64 得到 .gz檔
$result_gzip = [Convert]::FromBase64String($result_base64)
$out_gz = $tmpfile + ".gz"
[IO.File]::WriteAllBytes($out_gz, $result_gzip) #$out_gz for debug
#解壓縮 .gz
$argumentlist="x -y $($out_gz)" # x 解壓縮,-y 回答 yes
start-process $7z_path -argumentlist $argumentlist #解壓縮後的檔案為 $tmpfile
$out_csv = $tmpfile + ".csv"
(Get-Content $tmpfile | ConvertFrom-Json) | ForEach-Object {
#$_.mac + "," + $_.vendor + "," + $_.datetime
$_.address + "," + $_.company + "," + $_.upload_time
} | Out-File -FilePath $out_csv
$headers.Add("Authorization", "Token XXXXXXor8mWYh259gqa1ZcMa")
$response = Invoke-RestMethod 'https://backend.com.tw/blue/raw_data/?index=blue-04&time=2022-09-11T20:00:00,2022-09-11T21:59:59' -Method 'GET' -Headers $headers
#webapi的回應為 result: value,取出 value (base64格式)
$result_base64 = $response[0].result
$out_b64 = $tmpfile + ".b64"
$result_base64 | Out-File -FilePath $out_b64 #$out_b54 for debug
#decode base64 得到 .gz檔
$result_gzip = [Convert]::FromBase64String($result_base64)
$out_gz = $tmpfile + ".gz"
[IO.File]::WriteAllBytes($out_gz, $result_gzip) #$out_gz for debug
#解壓縮 .gz
$argumentlist="x -y $($out_gz)" # x 解壓縮,-y 回答 yes
start-process $7z_path -argumentlist $argumentlist #解壓縮後的檔案為 $tmpfile
$out_csv = $tmpfile + ".csv"
(Get-Content $tmpfile | ConvertFrom-Json) | ForEach-Object {
#$_.mac + "," + $_.vendor + "," + $_.datetime
$_.address + "," + $_.company + "," + $_.upload_time
} | Out-File -FilePath $out_csv