我已经编写了一个从restful api获取系统日志的基本脚本,并将其写入到一个json文件中。它可以工作,但我看到了巨大的RAM问题-我认为这是因为脚本获取了系统日志条目的全部有效负载,并在将其写入文件之前将其存储在内存中。
下面脚本中的do循环用于分页- api一次只返回一定数量的事件,因此需要遍历。
这里的性能可以提高吗?我猜,当内存中的有效负载达到一定大小时,它会被写入文件,然后循环吗?或者,是否可以采取其他措施来减少系统资源?
谢谢你的帮忙!
#Name: getLogs.ps1
#Purpose: Script for exporting logs to a json file
#variables
$org = "<api url here>"
$token="<api token here>"
$filePath = "D:\Downloads\logs\test2.json"
#format the date and append 00:00:00
$fromTime = Get-Date -Format s
$fromTime = $fromTime.Substring(0,10) + "00%3A00%3A00Z"
#format the date and append 23:59.59
$toTime = Get-Date -Format s
$toTime = $fromTime.Substring(0,10) + "T23%3A59%3A59Z"
### Set $uri as the API URI for use in the loop
$uri = "$org/api/v1/logs?until=$toTime&limit=20&sortOrder=DESCENDING&q=&since=$fromTimeT00"
### Define $allLogsas empty array
$allLogs = @()
### Use a while loop and get all users from Okta API
DO
{
$webrequest = Invoke-WebRequest -Headers @{"Authorization" = "SSWS $token"} -Method Get -Uri $uri
$link = $webrequest.Headers.Link.Split("<").Split(">") 
$uri = $link[3]
$json = $webrequest | ConvertFrom-Json
$allLogs += $json
#slow it down to avoid rate limits
Start-Sleep -m 1001
} while ($webrequest.Headers.Link.EndsWith('rel="next"'))
# get results, switch to json, save
$allLogs | ConvertTo-Json | Out-File $filePath发布于 2018-01-08 18:36:45
您可以尝试更改:
$json = $webrequest | ConvertFrom-Json至:
$webrequest | Out-File $filePath -Append然后完全抛弃了$alllogs。
发布于 2018-01-09 11:20:34
还有另一篇关于性能和使用PoSH Invoke-* cmdlet的文章。正如在上一篇文章中所指出的,在PoSHv6之前,一般都是很慢的。请看这篇文章,了解关于这个主题的信息。Choppy File Download using Powershell on Azure VM
https://stackoverflow.com/questions/48148378
复制相似问题