Last active
September 18, 2016 16:28
-
-
Save jtuttas/6c7f34cb6b057b084d1ac966c18b38fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module "C:\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin\psModules\virtualmachinemanager" | |
<# | |
.Synopsis | |
Löscht alle Snapshorts und erstellt einen Snapshot "base" | |
.DESCRIPTION | |
Löscht alle Snapshorts und erstellt einen Snapshot "base" | |
.EXAMPLE | |
Init Block for the named VM | |
init-block -vmname "linux" | |
.EXAMPLE | |
Init Block for a List of VM's | |
Import-Csv C:\Users\jtutt_000\Temp\test.csv | init-block | |
#> | |
function init-block | |
{ | |
Param | |
( | |
# Name der Virtuellen Maschine | |
[Parameter(Mandatory=$true, | |
ValueFromPipelineByPropertyName=$true, | |
ValueFromPipeline=$true, | |
Position=0)] | |
[Alias('Name')] | |
$vmname, | |
$basename="base" | |
) | |
Begin | |
{ | |
} | |
Process | |
{ | |
try { | |
Write-Host "Löschen Snapshots für $vmname" | |
$sn = get-SCVMCheckpoint -VM $vmname | |
foreach ($v in $sn) { | |
$r=Remove-SCVMCheckpoint -VMCheckpoint $v -ErrorAction Stop -ErrorVariable $e | |
} | |
Write-Host "Erstelle base Snapshot" | |
$sn=new-SCVMCheckpoint -VM $vmname -Name $basename -ErrorAction Stop | |
} | |
catch { | |
Write-Host "Fehler beim initialisieren des VM $vmname : $_" -BackgroundColor red | |
} | |
} | |
End | |
{ | |
} | |
} | |
<# | |
.Synopsis | |
Erstellt einen Snapshot unter base mit dem namen block | |
.DESCRIPTION | |
Erstellt einen Snapshot unter base mit dem namen block | |
.EXAMPLE | |
create-block -vmname "linux" -block "rot" | |
.EXAMPLE | |
Create Block for a List of VM's | |
Import-Csv C:\Users\jtutt_000\Temp\test.csv | create-block -block "rot" | |
#> | |
function create-block | |
{ | |
Param | |
( | |
# Name der Virtuellen Maschine | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
[Alias('Name')] | |
$vmname, | |
# Name des Blocks | |
[Parameter(Mandatory=$true)] | |
$block, | |
$basename="base" | |
) | |
Begin | |
{ | |
} | |
Process | |
{ | |
try { | |
$sn = Get-SCVMCheckpoint -VM $vmname -ErrorAction SilentlyContinue | |
$found=$false | |
$basecp | |
foreach ($v in $sn) { | |
if ($v.Name -eq $block) { | |
$found=$true; | |
} | |
if ($v.Name -eq $basename) { | |
$basecp=$v; | |
} | |
} | |
if ($found) { | |
Write-Host "Fehler beim Erzeugen des Block $block für die VM"$vmname": Der Block existiert schon!" -BackgroundColor red | |
} | |
else { | |
Write-Host "aktiviere Snapshot 'base' für "$vmname | |
if ($basecp) { | |
$sn=Restore-SCVMCheckpoint -VMCheckpoint $basecp | |
Write-Host "Erstelle Snapshot $block" | |
$sn=New-SCVMCheckpoint -VM $vmname -Name $block | |
} | |
else { | |
Write-Error "Keinen Snapshop base für die vm $vmname gefunden!" | |
} | |
} | |
} | |
catch { | |
Write-Host "Fehler beim Erzeugen des Block $block für die VM $vmname : $_" -BackgroundColor red | |
} | |
} | |
End | |
{ | |
} | |
} | |
<# | |
.Synopsis | |
Schaltet zwischen zwei Snapshots um | |
.DESCRIPTION | |
Schaltet zwischen zwei Snapshots um | |
.EXAMPLE | |
Schaltet die VM "linux" zwischen den Blöcken "rot" und "gelb" um! | |
switch-block -vmname "linux" -fromblock "rot" -toblock "gelb" | |
.EXAMPLE | |
Switch Blocks for a List of VM's | |
Import-Csv C:\Users\jtutt_000\Temp\test.csv | switch-block -fromblock "rot" -toblock "gelb" | |
#> | |
function switch-block | |
{ | |
Param | |
( | |
# Name der Virtuellen Maschine | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
ValueFromPipelineByPropertyName=$true, | |
Position=0)] | |
[Alias('Name')] | |
$vmname, | |
# Name des from Blocks | |
[Parameter(Mandatory=$true)] | |
$fromblock, | |
# Name des from Blocks | |
[Parameter(Mandatory=$true)] | |
$toblock, | |
$basename="base" | |
) | |
Begin | |
{ | |
} | |
Process | |
{ | |
try { | |
Write-Host "Bearbeite Vm $vmname" | |
$sn = Get-SCVMCheckpoint -VM $vmname -ErrorAction SilentlyContinue | |
$found=$false | |
$basecp="" | |
$fromcp="" | |
$tocp="" | |
foreach ($v in $sn) { | |
if ($v.Name -eq $fromblock) { | |
$fromcp=$v; | |
} | |
if ($v.Name -eq $toblock) { | |
$tocp=$v; | |
} | |
if ($v.Name -eq $basename) { | |
$basecp=$v; | |
} | |
} | |
#$sn = Get-VMSnapshot -VMName $vmname.VM -Name $fromblock -ErrorAction Stop | |
$machine = Get-SCVirtualMachine -Name $vmname | |
$fromPermission=Get-SCUserRole -Name $fromblock | |
$toPermission=Get-SCUserRole -Name $toblock | |
if ($machine.LastRestoredVMCheckpoint.CheckpointID -ne $fromcp.CheckpointID) { | |
Write-Host "$fromblock is not the current block! Current block is"$machine.LastRestoredVMCheckpoint.Name -BackgroundColor Red | |
} | |
elseif ($fromcp -eq "") { | |
Write-Host "From Snapshot $fromblock kann nicht gefunden werden in der VM $vmname" -BackgroundColor Red | |
} | |
elseif ($tocp -eq "") { | |
Write-Host "To Snapshot $toblock kann nicht gefunden werden in der VM $vmname" -BackgroundColor Red | |
} | |
else { | |
Write-Host "Lösche Snapshot '$fromblock' (Merge) für "$vmname | |
$sn=Remove-SCVMCheckpoint -VMCheckpoint $fromcp -ErrorAction Stop | |
Write-Host "Erstelle Snapshot (Merge) '$fromblock' für "$vmname | |
$sn=new-SCVMCheckpoint -VM $vmname -Name $fromblock | |
Write-Host "Aktiviere Snapshot $toblock für "$vmname | |
#Restore-VMSnapshot -VMName $vmname.VM -Name $toblock –confirm:$False -ErrorAction Stop | |
$sn=Restore-SCVMCheckpoint -VMCheckpoint $tocp | |
$fp=Revoke-SCResource -Resource $machine -UserRoleID $fromPermission.ID | |
$fp=Grant-SCResource -Resource $machine -UserRoleID $toPermission.ID | |
} | |
} | |
catch { | |
Write-Host "Fehler beim Switchen des Blockes von $fromblock nach $toblock für die VM "$vmname": $_" -BackgroundColor red | |
} | |
} | |
End | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment