Zabbix批量导入XML文件生成小脚本(by Powershell)
作者:互联网
最近升级6.0后发现手动批量导入Zabbix主机群组需要一个新的uuid参数,于是用powershell写了一个脚本。
代码如下:
<# TIME:2022/04/30 DESCRIPTION:生成zabbix批量导入的XML文件 AUTHOR:Monbro #> # # $zbxiface设置为161为生成SNMP的XML文件,设置10050为ZabbixAgent的XML文件 $zbxiface = 10050 $ppath = Split-Path -Parent $PSCommandPath $xlsxfile = $ppath+"\"+(Get-ChildItem -Path "$ppath\*.xlsx").Name $excel = New-Object -ComObject Excel.Application $excel.Visible = $false $wb=$excel.Workbooks.Open($xlsxfile) $ws=$wb.Sheets.Item("Sheet1") $slist=$iplist=$dplist=$cnlist=@() for ($n = 1; $n -le $ws.UsedRange.Rows.Count; $n++) { #01_Names $slist+=$ws.Cells.Item($n,1).Text #02_IPv4Address $iplist+=$ws.Cells.Item($n,2).Text #03_Description $dplist+=$ws.Cells.Item($n,3).Text #04_CanonicalName $cnlist+=$ws.Cells.Item($n,4).Text } $excel.Quit() $excel = $null [GC]::Collect() $cretedate = Get-Date -Format "yyyy-MM-ddThh:mm:ssZ" $groupname=$cretedate $groupuuid=[System.Guid]::NewGuid().toString('N') $filename = $ppath+"\"+$cretedate.Replace(":", "")+".xml" function ZBXhosts_imxml{ $xmlhead="<?xml version=`"1.0`" encoding=`"UTF-8`"?> <zabbix_export> <version>6.0</version> <date>$cretedate</date> <groups> <group> <uuid>$groupuuid</uuid> <name>$groupname</name> </group> </groups> <hosts>" $xmltail=" </hosts> </zabbix_export>" $xmlhead | Out-File -Encoding utf8 $filename if ($zbxiface.Equals(10050)) { $i=0 foreach($ServerName in $slist ){ $ip=$iplist[$i] $desp=$dplist[$i] $cnname=$cnlist[$i] $xmlmid10050=" <host> <host>$ServerName</host> <name>$ServerName</name> <description>$desp</description> <groups> <group> <name>$groupname</name> </group> </groups> <interfaces> <interface> <ip>$ip</ip> <dns>$ServerName</dns> <interface_ref>if1</interface_ref> </interface> </interfaces> <inventory> <name>$cnname</name> </inventory> </host>" $xmlmid10050 | Out-File -Encoding utf8 -Append $filename $i++ } } if ($zbxiface.Equals(161)) { $i=0 foreach($ServerName in $slist ){ $ip=$iplist[$i] $desp=$dplist[$i] $cnname=$cnlist[$i] $xmlmid161=" <host> <host>$ServerName</host> <name>$ServerName</name> <description>$desp</description> <groups> <group> <name>$groupname</name> </group> </groups> <interfaces> <interface> <type>SNMP</type> <ip>$ip</ip> <dns>$ServerName</dns> <port>161</port> <details> <community>{`$SNMP_COMMUNITY}</community> </details> <interface_ref>if1</interface_ref> </interface> </interfaces> <inventory> <name>$cnname</name> </inventory> </host>" $xmlmid161 | Out-File -Encoding utf8 -Append $filename $i++ } } $xmltail | Out-File -Encoding utf8 -Append $filename } ZBXhosts_imxml
标签:XML,ServerName,excel,zbxiface,filename,Item,Zabbix,ws,Powershell 来源: https://www.cnblogs.com/gocd/p/16210954.html