How to proportionate reimpartirea
Problem, idea and solution
Hello my dear detishechki. We hasten to inform you that in my mind came one idea that resulted in this post. The idea actually came from the problem that threw me hotly favourite and respected company Microsoft and their new Windows Server 2012 R2. And here I am not being sarcastic, I really like them. But first things first.
First of all I should mention that I, among other things also the coach for all kinds of Microsoft products, and therefore have access to certain buns in the form of ready virtual machines to prepare for the courses at the training center. And, in fact, I decided to try to drive the new server, and, as usual, to deploy virtuallock from the same course. Pumped those cars, all prepared, unpacked. And then I was in for a terrible. They flatly refused to import.

In General, it appears that the machines are exported on Windows Server 2008 and Windows 2012 R2 will not be imported. Not supported this for certain technical reasons.
What to do, how could they, you ask, and rightly so. In my case I did not have at hand the Windows Server 2008 and I began to look for an alternative. In General it is simple. In one of the subdirectories of the exported machine was a file named {GUID}.exp. It represents the configuration of the exported VM. From-for it it is not imported, and we are going to change. I decided to just take my desired settings from the file, bring them to a better mind and simply create a new virtual machine with the same settings as the original. To long to bother, I decided to choose from the file the machine name, path to VHD files, memory configuration, and the name of the virtual network to which these machines should connect. But do not do it by hand, right. Especially if you open this file and look at its contents, then the hair on my head stand on end and lost the desire to search for something in it manually. And if more than one. In the General solution, write the script
Script
On what you write? Of course, the good old powershell 4, which comes bundled with the new server and WIndows 8.1. Where to start? And we'll start right in the forehead, as well as otherwise. Open the file, have the benefit of type [xml] which simplifies the picking in the guts and the wilds of the exported configuration. Briefly, this file contains a bunch of WMI classes with property values. The content of these classes is downloaded in XML and written to a file. Since I'm not too familiar with these WMI classes, the one with XLM, too, had to struggle, getting these settings in the forehead. Here's what happened:
the
cls
$tmp = dir "C:\Program Files\Microsoft Learning\20413\*\*.exp" -Recurse
$tmp | % {
# read file
[xml]$vm = gc $_.fullname
# parsing of the various XML of different internal structures using "properties" notation
# CLASSNAME Msvm_VirtualSystemGlobalSettingData
$disks = ($vm.DECLARATIONS.DECLGROUP.'VALUE.OBJECT'.instance | where classname -like "*resource*") |
where {$_.property | where name -like "*units*" |
where value-eq "disks"}
$newVM = @{}
# CLASSNAME Msvm_VirtualSystemGlobalSettingData
$newVM.Global = $vm.DECLARATIONS.DECLGROUP.'VALUE.OBJECT'.instance |
where classname -like "*Msvm_VirtualSystemGlobalSettingData*" |
select-ExpandProperty property |
# below passage is most exciting
% {$obj=@{}} {$obj["$($_.name)"]=$_.value} {new-object psobject -prop $obj}
# disks configuration contains some internal nodes, extractiong them to get the paths to VHDs
$newVM.Disks = $disks | % { $prop = @{}; $disk = $_; $disk | select-ExpandProperty property |
% {$obj=@{}} {$obj["$($_.name)"]=$_.value};
$obj."Path" = ($disk | select -expand property.array)."value.array".value;
New-object psobject -prop $obj}
# CLASSNAME Msvm_MemorySettingData
$newVM.Memory = $vm.DECLARATIONS.DECLGROUP.'VALUE.OBJECT'.instance |
where classname -like "*memory*" | select-ExpandProperty property |
% {$obj=@{}} {$obj["$($_.name)"]=$_.value} {new-object psobject -prop $obj}
# CLASSNAME Msvm_SwitchPort
$newVM.Network = $vm.DECLARATIONS.DECLGROUP.'VALUE.OBJECT'.instance |
where classname -like "*switch*" | select-ExpandProperty property |
# as far as $newVM is a hashtable, making an object from it
$vmObj = New-object psobject -prop $newVM
# variables, just to see what we've got
$vmName = $vmObj.Global.ElementName
#$vmObj.Disks.Path
[int64]$vmMemoryReservation = [int64]$vmObj.Memory.Reservation * 1MB
[int64]$vmMemoryLimit = [int64]$vmObj.Memory.Limit * 1MB
$vmNetwork = $vmObj.Network.ElementName
$vmName
$vmObj.Disks.Path
$vmMemoryReservation
$vmMemoryLimit
$vmNetwork
#actual import
New-VM -Name $vmName-MemoryStartupBytes $vmMemoryLimit #-VHDPath $vmObj.Disks.Path[0]
$vmObj.Disks.Path | % {Add-VMHardDiskDrive -VMName $vmName -Path $_}
Set-VMMemory -VMName $vmName-MaximumBytes $vmMemoryLimit -DynamicMemoryEnabled $true
Get-vm-Name $vmName | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName $vmNetwork
checkpoint-vm-Name $vmName
"========== $vmName =========="
}
And it worked. But looking at all this, and remembering the hours I spent searching for the right parts of the text, I realized that it is awful. I immediately remembered the comment of comrade Pinsky about the Paralympic games programming. And that, I'm not a programmer, yet. In fact still working. But I wanted something more, more concise, beautiful and concise. In General, then I remembered a familiar word XPATH. Honestly, up to this point, about the technology itself except for the word, I knew nothing. I suspect that this stuff is supposed to do but not necessary. I thought that would be worth a try. How this happiness works with powershell and work. A couple of hours were spent in searching on Google and tests. And here it is, almost happiness:
the
[xml]$vm = gc $path
#class 'Msvm_VirtualSystemGlobalSettingData'
$vmName = ($vm.SelectNodes("//INSTANCE[@CLASSNAME='Msvm_VirtualSystemGlobalSettingData']/PROPERTY") |
% {$obj=@{}} {$obj["$($_.name)"]=$_.value} {new-object psobject -prop $obj}).elementname
#class 'Msvm_ResourceAllocationSettingData'
$etc = $vm.SelectNodes("(//INSTANCE[@CLASSNAME='Msvm_ResourceAllocationSettingData'])/PROPERTY.ARRAY[@NAME='Connection']/VALUE.ARRAY").value
#class 'Msvm_MemorySettingData'
$memory = $vm.SelectNodes("//INSTANCE[@CLASSNAME='Msvm_MemorySettingData']/PROPERTY") |
% {$obj=@{}} {$obj["$($_.name)"]=$_.value} {new-object psobject -prop $obj} | select Limit,Reservation
#class 'Msvm_SwitchPort'
$network = ($vm.SelectNodes("//INSTANCE[@CLASSNAME='Msvm_SwitchPort']/PROPERTY") |
% {$obj=@{}} {$obj["$($_.name)"]=$_.value} {new-object psobject -prop $obj}).ElementName
$vmName
$etc
$memory
$network
Here's the thing. Much shorter, nicer to read, more understandable. And still up and running.
PS. By the way it should be noted that the configurations of the machine were incorrectly specified the path to the VHD file. That is a self-extracting file puts the files into the directory [..]\1234In, XX, YY1\[..]\file.vhd and configuration, they were very different [..]\1234And, XX, YY1\[..]\file.vhd. To see this difference instead of banging your head against the wall in poiskah error in the script took about an hour with penny.
Комментарии
Отправить комментарий