| Powershell create Scheduled Tasks » |
When you have a Windows 2008 R2 machine and the HyperV role enabled but no SCVVM or whatever. Cloning is not possible out-of-the-box but you can create an export.
As soon as you want to import this export again you will get a message about the machine already exists. When you rename the vhd and replace all the references in the config files of the export you ARE able to import the machine again.
That's why I created a .ps1 script to list all the exports in the current folder and rename the choosen one.
Follow up:
$a = Get-ChildItem . -Exclude *.*
$i = -1
foreach ($aitem in $a) {
$i++
Write-Host $i"." $aitem.name
}
$image = Read-Host "Please select the Image by typing the number in front of the os"
do {$answer = Read-Host "The selected image is '"$a[$image].name"' Is this correct? [Y/N]" } Until ($answer -eq "Y" -or $answer -eq "y" -or $answer -eq "N" -or $answer -eq "n")
If ($answer -eq "Y")
{
Write-Host "This is the selected image" $a[$image].name
$newimage = Read-Host "Please type new machine name"
do {$answer = Read-Host "The HyperV systemname will be '$newimage'. Is this correct? [Y/N]" } Until ($answer -eq "Y" -or $answer -eq "y" -or $answer -eq "N" -or $answer -eq "n")
If ($answer -eq "Y" -or $answer -eq "y")
{
Write-Host "This will be the name of the new system '$newimage'"
$b = Get-ChildItem $a[$image] -Recurse -Include *.vhd
$oldname = $b.BaseName
$c = Get-ChildItem $a[$image] -Recurse -Exclude *.vhd -Include *.*
foreach ($citem in $c) {
Write-Host $citem.name
$content = Get-Content -Encoding Unicode $citem
$content -replace $oldname, $newimage | Set-Content -Encoding Unicode $citem | Out-Null
}
$newimagename = $newimage + ".vhd"
Get-ChildItem $a[$image] -Recurse -Include *.vhd | Rename-Item -NewName $newimagename
}
}
Attachments:
renamevm.ps1 (1.3 KB)
No feedback yet
Comments are closed for this post.