-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
182 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<# | ||
.SYNOPSIS | ||
This script creates a snapshot of a HyperV VM in a remote hyperV server | ||
#> | ||
Param( | ||
[String]$snapshotName, | ||
[Parameter(Mandatory)] | ||
[String]$virtualMachineName, | ||
[Parameter(Mandatory)] | ||
[String]$overWrite | ||
) | ||
|
||
Set-Location $PSScriptRoot | ||
|
||
$createSnapshotScriptBlock = { | ||
if ($overWrite -eq "yes") { | ||
$error.clear() | ||
try { | ||
Get-VMSnapshot -VMName $virtualMachineName | ` | ||
Where-Object { $_.Name -eq $snapshotName } | ` | ||
Remove-VMSnapshot -ErrorAction Stop | ||
} catch { | ||
Write-Output $error | ||
Write-Output "CRITICAL_ERROR" | ||
Exit 1 | ||
} | ||
} | ||
|
||
$error.clear() | ||
try { | ||
Checkpoint-VM ` | ||
-Name $virtualMachineName ` | ||
-SnapshotName $snapshotName ` | ||
-Confirm:$false ` | ||
-ErrorAction Stop | ||
} catch { | ||
Write-Output $error | ||
Write-Output "CRITICAL_ERROR" | ||
Exit 1 | ||
} | ||
} | ||
Write-Output "Creating snapshot to $snapshotName" | ||
$job = Invoke-Command -ScriptBlock $createSnapshotScriptBlock -AsJob | ||
$jobOutput = "" | ||
Write-Host "`r`nJob Name: $($job.name) State: $($job.State)" | ||
$Timer = 0 | ||
$TimerLimit = 600 | ||
$TimerIncrement = 15 | ||
while ($job.State -eq "Running" -And $Timer -lt $TimerLimit) { | ||
$jobOutputIncremental = Receive-Job -Job $job | ||
if ($jobOutputIncremental) { | ||
Write-Host $jobOutputIncremental | ||
$jobOutput += $jobOutputIncremental | ||
} | ||
Start-Sleep $TimerIncrement | ||
$Timer += $TimerIncrement | ||
} | ||
$jobOutputIncremental = Receive-Job -Job $job | ||
if ($jobOutputIncremental) { | ||
Write-Host $jobOutputIncremental | ||
$jobOutput += $jobOutputIncremental | ||
} | ||
Write-Host "`r`nJob Name: $($job.name) State: $($job.State)" | ||
if ($job.State -ne "Completed") { | ||
Write-Host "Job State not Completed" | ||
$STATUS = 1 | ||
} elseif ($([string]$jobOutput).contains("CRITICAL_ERROR")) { | ||
Write-Host "ERROR FOUND" | ||
$STATUS = 1 | ||
} elseif ($Timer -ge $TimerLimit) { | ||
Write-Host "TIMED OUT" | ||
$STATUS = 1 | ||
} else { | ||
$STATUS = 0 | ||
} | ||
|
||
Exit $STATUS |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<# | ||
.SYNOPSIS | ||
Wrapper script to create a snapshot of a VM. | ||
Checks for a snapshot with the same name and gives you the option to "overwrite" it. | ||
#> | ||
Param( | ||
[String]$vmwareHost, | ||
[Parameter(Mandatory)] | ||
[String]$snapshotName, | ||
[Parameter(Mandatory)] | ||
[String]$vmName, | ||
[Parameter()] | ||
[switch]$overWrite | ||
) | ||
|
||
Set-Location $PSScriptRoot | ||
|
||
if ($overWrite) { | ||
$snapshot = Get-Snapshot -VM $vmName -Name $snapshotName -ErrorAction Ignore | ||
if ($snapshot) { | ||
try { | ||
Write-Output "Deleting existing snapshot $snapshotName" | ||
$snapshot | Remove-Snapshot -confirm:$false | ||
} catch { | ||
Write-Output $error | ||
Write-Output "CRITICAL_ERROR" | ||
Exit 1 | ||
} | ||
|
||
} | ||
} | ||
|
||
Write-Output "Creating snapshot $snapshotName" | ||
try { | ||
New-Snapshot -VM $vmName -Name $snapshotName -confirm:$false | ||
} catch { | ||
Write-Output $error | ||
Write-Output "CRITICAL_ERROR" | ||
Exit 1 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<# | ||
.SYNOPSIS | ||
This script logs you in to vSphere | ||
#> | ||
Param( | ||
[Parameter(Mandatory)] | ||
[string]$vmwareUserName, | ||
[Parameter(Mandatory)] | ||
[String]$vmwarePassword, | ||
[Parameter(Mandatory)] | ||
[String]$vmwareHost | ||
) | ||
|
||
Set-Location $PSScriptRoot | ||
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | ||
|
||
Write-Output "`r`nConnecting to vSphere API on: $vmwareHost as $vmwareUserName" | ||
$Error.clear() | ||
try { | ||
Connect-VIServer ` | ||
-Server $vmwareHost ` | ||
-User $vmwareUserName ` | ||
-Password $vmwarePassword ` | ||
-ErrorAction Stop | Out-Null | ||
} catch { | ||
Write-Output "Error connecting to vSphere API on: $vmwareHost as $vmwareUserName" | ||
Write-Output $Error | ||
$retries = 0 | ||
While ($Error -and $retries -lt 10) { | ||
$retries += 1 | ||
Start-Sleep 10 | ||
$Error.clear() | ||
try { | ||
Connect-VIServer ` | ||
-Server $vmwareHost ` | ||
-User $vmwareUserName ` | ||
-Password $vmwarePassword ` | ||
-ErrorAction Stop | Out-Null | ||
} | ||
catch { | ||
Write-Output "Error connecting to vSphere API on: $vmwareHost as $vmwareUserName" | ||
Write-Output $Error | ||
} | ||
} | ||
if ($retries -ge 10) { | ||
Write-Output "Timed out when trying to connect to vSphere API on: $vmwareHost as $vmwareUserName" | ||
Exit 1 | ||
} | ||
} |
File renamed without changes.
File renamed without changes.