Powershell 从zoho的 OPM 里导出数据

发布时间 2023-04-18 11:04:12作者: NagaResst

公司使用zoho的OPM监控客户的设备,需要给客户提供所有网络设备的网络接口 95% 利用率的表格。

卓豪的 OPM 居然不支持批量导出,非常残疾。所以只能自己写个脚本去跑。考虑到各种问题,最后决定用 powershell。

写完之后神清气爽,Powershell YES!!

$interfacedata = (Get-Content "interfaceList.json" | ConvertFrom-Json)
#读取要抓取的接口列表dd 读取json格式的数据作为对象储存在一个变量里
#powershell的变量可以作为对象  对数据的使用有良好的支持性
#输出表头  会覆盖上次抓取的文件
Write-Output `t deviceName | Out-File  -NoNewline  "interface.xls"
Write-Output `t interfaceName | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t  Rx-Min | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t Rx-Max | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t Rx-Avg | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t Rx-95th | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t Tx-Min | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t Tx-Max | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t Tx-Avg | Out-File -Append -NoNewline  "interface.xls"
Write-Output `t Tx-95th | Out-File -Append -NoNewline  "interface.xls"
Write-Host Starting work!
#XLS文件是一个简单的excel表格 本质上也可以用记事本打开 但是不支持xlsx


#遍历接口列表  向表格输出数据  同时显示接口抓取进度
foreach ($interf IN $interfacedata.data){
    $data = Invoke-WebRequest -Uri "$($interfacedata.httpadd)/api/json/device/getInterfaceGraphs?interfaceName=$($interf.interfaceID)&graphName=utilization&isFluidic=true&period=Last_Month&apiKey=$($interfacedata.apikey)"  | ConvertFrom-Json 
    <#
    foreach是powershell提供的遍历函数  可以自动遍历括号内的列表
    ($interf IN $interfacedata.data) 此条将接口列表中的 data 部分单独提取出来作为 interf 对象使用 会遍历interf (即$interfacedata.data)中的所有内容  此函数中使用的时候使用 interf
    #>
    Write-Output `n $interf.sequence | Out-File -Append -NoNewLine "interface.xls"
    #Write-Progress 提供一个可视化的进度条用于查看现在任务执行的进度  PercentComplete 负责显示进度条 CurrentOperation 负责提供文字说明
    Write-Progress -Activity "Starting..." -PercentComplete $($interf.sequence / $interfacedata.data.Length * 100)  -CurrentOperation "$($interf.sequence) / $($interfacedata.data.Length )  Finished" -Status "Loading..."
    Write-Output `t $interf.device | Out-File -Append -NoNewLine "interface.xls"
    Write-Output `t $interf.interfaceName | Out-File -Append  -NoNewline "interface.xls"
    Write-Output `t $data.consolidatedValues.'Rx Utilization'.'minVal' | Out-File -Append -NoNewline  "interface.xls"
    Write-Output `t $data.consolidatedValues.'Rx Utilization'.'maxVal' | Out-File -Append -NoNewline  "interface.xls"
    Write-Output `t $data.consolidatedValues.'Rx Utilization'.'avgVal' | Out-File -Append -NoNewline  "interface.xls"
    Write-Output `t $data.consolidatedValues.'Rx Utilization'.'95thpercentileValue' | Out-File -Append -NoNewline "interface.xls"
    Write-Output `t $data.consolidatedValues.'Tx Utilization'.'minVal' | Out-File -Append -NoNewline "interface.xls"
    Write-Output `t $data.consolidatedValues.'Tx Utilization'.'maxVal' | Out-File -Append -NoNewline "interface.xls"
    Write-Output `t $data.consolidatedValues.'Tx Utilization'.'avgVal' | Out-File -Append -NoNewline "interface.xls"
    Write-Output `t $data.consolidatedValues.'Tx Utilization'.'95thpercentileValue' | Out-File -Append -NoNewline "interface.xls"
    }

Write-Host "Report is complete!"

下面放一段 interfaceList.json 的代码

{
	"httpadd": "http://localhost:8088",
	"apikey": "",
	"data": [
		{
			"sequence": "1",
			"device": "pekco-r3",
			"interfaceName": "GigabitEthernet0/1-SDH 10M Connect to NAH core swith #2 by CU (10.227.6.94)",
			"interfaceID": "IF-199.40.10.229.140000000001-140000010225"
		},
		{
			"sequence": "2",
			"device": "pekco-r4",
			"interfaceName": "Serial0/1/0:15-Signaling-Se0/1/0:15",
			"interfaceID": "IF-199.40.10.16.140000000001-140000009801"
		}
    ]
}

powershell 其实挺复杂的,但是入门并没有那么难。个人感觉是非常好用。