PoshCode Archive  Artifact Content

Artifact 8a75b477963e4cfc02f337f467edb67f5279ccbba804e0d37bbd59447948134f:

  • File This-is-a-very-quick-.ps1 — part of check-in [80c6da1c4c] at 2018-06-10 13:32:20 on branch trunk — This is a very quick and dirty script to import non-exported Machines into hyper-v. For example if you have a data disk with vm’s and your system disk fails. This can “reimport” the machines without need to recreate the virtual machine config with all the work that implies. Switches needs to be recreated in addition to this so network connections will be broken by default. If you have snapshots you will need to add in links and ACLs for that also. Not supported and not recommended for production. (user: unknown size: 1363)

# encoding: ascii
# api: powershell
# title: 
# description: This is a very quick and dirty script to import non-exported Machines into hyper-v. For example if you have a data disk with vm’s and your system disk fails. This can “reimport” the machines without need to recreate the virtual machine config with all the work that implies. Switches needs to be recreated in addition to this so network connections will be broken by default. If you have snapshots you will need to add in links and ACLs for that also. Not supported and not recommended for production.
# version: 0.1
# type: script
# license: CC0
# x-poshcode-id: 3860
# x-archived: 2013-01-09T07:27:57
#
#
PARAM(
	$location=$(throw "Make sure to specify a location for old machines to be imported")
)
$oldVM = Get-ChildItem "$location\*.xml"

foreach($vm in $oldVM)
{
	$vmGuid = [System.IO.Path]::GetFileNameWithoutExtension($vm.Name)
	Invoke-Command -ScriptBlock {cmd /c "Mklink `"C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\$vmGuid.xml`" `"D:\VMConfig\Virtual Machines\$vmGuid.xml`""}
	Invoke-Command -ScriptBlock {cmd /c "Icacls `"C:\ProgramData\Microsoft\Windows\Hyper-V\Virtual Machines\$vmGuid.xml`" /grant `"NT VIRTUAL MACHINE\$vmGuid`":(F) /L"}
	Invoke-Command -ScriptBlock {cmd /c "Icacls $location /T /grant `"NT VIRTUAL MACHINE\$vmGuid`":(F)"}
}