$XMLFiles=@()
$XMLs=@()
$XMLCSV = $PSScriptRoot + "$(Get-Date -f 'yyyyMMdd').csv"
$i=0
$j=0
OutputToLog "#INFO#:开始读取虚拟机配置文件" green
if(!(Test-Path $XMLCSV))
{
foreach($NewCSVPath in $NewCSVPaths){
$XMLFiles+=Get-ChildItem -Path $NewCSVPath -Recurse|? Extension -eq .xml
}
foreach($XMLFile in $XMLFiles){
$i++
$item=""|select VMName,XMLPath
[xml]$XMLDocuments = Get-Content $($XMLFile.FullName)
$item.VMName=$VMNameinXML=$XMLDocuments.ChildNodes.properties.name.('#text')
$item.XMLPath=$($XMLFile.FullName)
OutputToLog "#INFO#:$i,正在读取虚拟机$($VMNameinXML)的配置文件" green
$XMLs+=$item
}
$XMLs | Export-Csv -Path $XMLCSV -Append -NoTypeInformation -Encoding UTF8
}
else
{
$XMLs = Import-Csv -Path $XMLCSV
}
foreach($customVM in $customVMs){
$j++
$ImportedVM=""|select VMName,ImportStatus,FullPath,Comments
echo $ImportedVM.FullPath
$ImportedVM.VMName=$VMName=$customVM
foreach($XML in $XMLs){
#比较虚拟机名称
$ImportedVM.FullPath=$XML.XMLPath
if($($XML.VMName) -eq $customVM){
try{
Import-VM -Path $($XML.XMLPath) -ea stop
$ImportedVM.ImportStatus="sucess"
$ImportedVM.Comments=""
$VMImported++
OutputToLog "#INFO#:虚拟机$($VMName)导入成功" green
break
}
catch{
$ImportedVM.ImportStatus="failed"
$ImportedVM.Comments=$($_.exception.tostring())
}
}
else{
$ImportedVM.ImportStatus="N/A"
$ImportedVM.Comments="NotFound"
}
}
$ImportedVM|Out-File $LogPath -Encoding utf8 -Append -Force
#输出导入结果
$ImportedVM|Export-Csv -Path $ImportedVMLog -NoTypeInformation -Encoding UTF8 -Append
}
Windows Server 2012 R2 读取Hyper-V VM配置文件
于 2023-07-14 16:20:34 首次发布