$ErrorActionPreference= 'silentlycontinue'

$Date=Get-date -format s
$Path = Split-Path -Parent $MyInvocation.MyCommand.Path #Check directory script run
$Path = $Path + "\"

$FileSrv=$Path + "Servers_Import.txt"

if(!(Test-Path -Path $FileSrv)){write-host "Warning !!!! File server not found" -f red;exit}

$Servers=get-content $FileSrv 
$XmlFile="Zabbix_Import_Srv.xml"

#Var groupe Zabbix
$ZabbixGrp="GRP_Citrix_XenApp_6.5"
#Var Template Zabbix
$ZabbixTpl="TPL_Citrix_XenApp_6.5"
$AgentConnect="0" #0 = Use DNS ; 1 = Use IP

$CountSrvIpError=0
$CountSrv=0

#Header XML File export
$XmlfileHeader=@"
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>2.0</version>
    <date>$Date</date>
    <groups>
        <group>
            <name>$ZabbixGrp</name>
        </group>
    </groups>
    <hosts>
"@

#Footer XML File export
$XmlFileFooter=@"
</hosts>
</zabbix_export>
"@


Foreach ($Server in $Servers)
{
$ServerDisplay=$Server.split('.')[0].split(' ')
$SrvIp=$Null
$SrvIp=[System.Net.Dns]::GetHostAddresses($Server)|%{$_.IPAddressToString}

	if(!$SrvIp) {Write-host "No IP for : $Server" -f yellow;$CountSrvIpError++}
	else
	{
	$CountSrv++
	$XmlFileBody=@"

<host>
            <host>$ServerDisplay</host>
            <name>$ServerDisplay</name>
            <description/>
            <proxy/>
            <status>0</status>
            <ipmi_authtype>-1</ipmi_authtype>
            <ipmi_privilege>2</ipmi_privilege>
            <ipmi_username/>
            <ipmi_password/>
            <templates>
                <template>
                    <name>$ZabbixTpl</name>
                </template>
            </templates>
            <groups>
                <group>
                    <name>$ZabbixGrp</name>
                </group>
            </groups>
            <interfaces>
                <interface>
                    <default>1</default>
                    <type>1</type>
                    <useip>$AgentConnect</useip>
                    <ip>$SrvIp</ip>
                    <dns>$Server</dns>
                    <port>10050</port>
                    <bulk>1</bulk>
                    <interface_ref>if1</interface_ref>
                </interface>
            </interfaces>
            <applications/>
            <items/>
            <discovery_rules/>
            <macros/>
            <inventory/>
        </host>
                               
"@
	$XmlFileBodyFull+=$XmlFileBody
	}
}

$ZabbixXmlFile+=$XmlfileHeader
$ZabbixXmlFile+=$XmlFileBodyFull
$ZabbixXmlFile+=$XmlFileFooter


#Create XML file
$ZabbixXmlFile|out-file -encoding ASCII $Path$XmlFile

Write-host @"

-------------------------------------------------
Number of Host written in XML file : $CountSrv

Number of Host with no IP : $CountSrvIpError
-------------------------------------------------

XML file path : $Path$XmlFile

"@



