我有一个PowerShell脚本正在作为作业的一部分运行Web请求。使用作业运行的脚本块,我正在记录Endpoint Uri和响应时间。然后在所有作业完成之后但在我删除作业之前,我试图将结果导出到excel。
PS版本:
Major Minor Build Revision
----- ----- ----- --------
5 1 17763 503
代码:
$Code = {
Param(
[array]$Domain,
[array]$Services,
[array]$TestData,
[string]$Path
)
$Log = @()
## Get Random School ##
$Random = Get-Random -InputObject $TestData -Count 1
## Get Random Service ##
$RandService = Get-Random -InputObject $Services
## Get Random Endpoint ##
$ServiceEndpoints = Get-Content "$Path\Service.Endpoints.json" | Out-String | ConvertFrom-Json
$GatewayEndpoints = $ServiceEndpoints.Services.$RandService.Gateway
$RandomEndpoint = Get-Random -InputObject $GatewayEndpoints
$Headers = @{
"Authorization" = "Bearer" + ' ' + $Random.Token
}
$Uri = 'https://' + $Domain + $RandomEndpoint
Try {
$TimeTaken = Measure-Command -Expression {
$JsonResponse = Invoke-WebRequest -Uri $Uri -Headers $Headers -ContentType 'application/json' -Method Get -UseBasicParsing
}
}
Catch {
}
$ResponseTime = [Math]::Round($TimeTaken.TotalMilliseconds, 1)
$LogItem = New-Object PSObject
$LogItem | Add-Member -type NoteProperty -Name 'Endpoint' -Value $Uri
$LogItem | Add-Member -type NoteProperty -Name 'Time' -Value $ResponseTime
$Log += $LogItem
Write-Host $Log
}
#Remove all jobs
Get-Job | Remove-Job
#Start the jobs. Max 4 jobs running simultaneously.
foreach($Row in $TestData){
While ($(Get-Job -state running).count -ge $MaxThreads){
Start-Sleep -Milliseconds 3
}
Start-Job -Scriptblock $Code -ArgumentList $Domain, $Services, $TestData, $Path
}
#Wait for all jobs to finish.
While ($(Get-Job -State Running).count -gt 0) {
start-sleep 1
}
$Log | Export-XLSX -Path .\Test.Results\Performance\Performance.Test.Log.xlsx -ClearSheet
#Get information from each job.
foreach($Job in Get-Job) {
$Info = Receive-Job -Id ($Job.Id)
}
#Remove all jobs created.
Get-Job | Remove-Job
我似乎无法从脚本块中获取端点uri和响应时间。当我尝试导出$Log
时,所有发生的事情是它创建了一个空的excel文件。
写入主机$ Log
@{Endpoint=https://domain/customer/v1/years/2019/marks; Time=1233.3}
@{Endpoint=https://domain/customer/v1/years/2019/marks; Time=2131.7}
答案 0 :(得分:1)
作业内部的全局变量在主脚本中不可见,因为作业在具有自己的全局空间的新会话中运行。
答案 1 :(得分:1)
您必须在脚本块中返回$Log
,因为$Log
位于另一个作用域中。您可以在$Log
脚本块中返回$Code
,最后通过Receive-Job
获取它。
将代码更改为:
$Code = {
...
$Log += $LogItem
Write-Host $Log
$Log # return via pipeline to the caller
}
正如Niraj Gajjar的答案所建议的那样,您可以使用Export-Csv
cmdlet创建一个Excel文件:
#Get information from each job.
foreach($Job in Get-Job) {
Receive-Job -Id ($Job.Id) | Export-CSV -Path .\Test.Results\Performance\Performance.Test.Log.xlsx -Append -NoTypeInformation
}
答案 2 :(得分:0)
您可以使用Export-Csv
在excel中打开文件。
具有多个作业的示例代码为CSV格式:
$jobs = @() # INITILIZING ARRAY
$jobs += Start-Job { appwiz.cpl } # START JOB 1 AND ADDING TO ARRAY
$jobs += Start-Job { compmgmt.msc } # START JOB 2 AND ADDING TO ARRAY
$jobs += Start-Job { notepad.exe } # START JOB 3 AND ADDING TO ARRAY
foreach ( $job in $jobs) # INTERATION OF JOBS
{
Export-Csv -InputObject $job "C:\result.csv" -Append # SAVING TO FILE
}
注意:由于深度为1,有些作业的内部属性无法通过CSV转换,但是一些基本属性可用。