Hi,
I would like to optimize this script as it seems that there is some room for improvement.
What would you do?
ipmo ‘virtualmachinemanager\virtualmachinemanager.psd1’
$const_SCVMM_server_long = “xxxxxxxx-001.cds.xxxx.net”
$const_SCVMM_server_short = “xxxxxxxx-001”
$const_SCVMM_environment = “MS cloud v1.0”
$const_outputfilename = $const_SCVMM_server_short + “.xml”
[XML]$SCVMM_xml = New-Object system.Xml.XmlDocument
$SCVMM_xml.LoadXml( @"
<?xml version=“1.0” encoding=“utf-8”?>
<SCVMM>
<globalinformation>
<devicename>$env:computername</devicename>
<SCVMMserver>$const_SCVMM_server_long</SCVMMserver>
<inventorydate>$(get-date -Format “yyyyMMdd_HHmmss”)</inventorydate>
<scriptversion>1.0</scriptversion>
</globalinformation>
</SCVMM>
"@ )
##############################################################################
read guest information
##############################################################################
$VMs = Get-VM -VMMServer $const_SCVMM_server_long
foreach ($VM in $VMs) {
“Processing $($vm.name)”
$hostgroup = $($vm.hostgrouppath).Replace(“All Hosts",”“)
$hostgroup = $hostgroup.Replace($(”" + $vm.name),“”)
$hostname = $($vm.HostName).split(‘.’,2)[0]
$hostdomain = $($vm.HostName).split(‘.’,2)[1]
$SCVMM_guest_data = @"
<guestname>$($vm.name)</guestname>
<SCVMMName>$const_SCVMM_environment</SCVMMName>
<hostname>$HostName</hostname>
<hostdomain>$HostDomain</hostdomain>
<Generation>$($VM.Generation)</Generation>
<VMAddition>$($VM.VMAddition)</VMAddition>
<operatingsystem>$($vm.OperatingSystem)</operatingsystem>
<state>$($vm.virtualmachinestate)</state>
<location>$($VM.location)</location>
<Clustername></Clustername>
<Hostgroup>$hostgroup</Hostgroup>
<cloud>$($vm.Cloud.Name)</cloud>
<scvmmserver>$const_SCVMM_server_long</scvmmserver>
<creationdate>$($vm.creationTime)</creationdate>
"@
$element = $SCVMM_xml.CreateElement("SCVMM_guest")
$element.InnerXml = $SCVMM_guest_data
$SCVMM_xml.SCVMM.AppendChild($element) | Out-Null
}
$SCVMM_xml.save($const_outputfilename)
##############################################################################
upload that data to the CMDB database
##############################################################################
# Database initialisation
$dbconn = New-Object System.Data.SqlClient.SqlConnection
$dbconn.connectionstring = "Data Source=xx.xxxx.xx.com; Initial Catalog=xxx;User ID=Xmlimort;Password=Mis`xxxxxx@;"
$dbconn.Open()
#delete all the previous records first
$query_delete = "DELETE FROM [xmlimport].[XML_IMPORT] where datasource ='SCVMM' and datasourcefilter = '$const_SCVMM_server_short'"
$dbwrite = $dbconn.CreateCommand()
$dbwrite.CommandText = $query_delete
$query_result = $dbwrite.ExecuteNonQuery()
#read basic information and store it in the XML_Import table
$query_insert = "INSERT INTO [xmlimport].[XML_IMPORT] ([ImportDate],[Filename],[XMLimport],[DataSource],[DataSourceFilter]) VALUES "
$query_insert += "(getdate(),'$const_outputfilename','$($SCVMM_xml.outerxml)','SCVMM','$const_SCVMM_server_short')"
$dbwrite = $dbconn.CreateCommand()
$dbwrite.CommandText = $query_insert
$query_result = $dbwrite.ExecuteNonQuery()
Any help is appreciated!