Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
svzez committed Dec 9, 2019
1 parent 2944d66 commit 678b7ae
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 68 deletions.
79 changes: 79 additions & 0 deletions hyperv/CreateSnapshot.ps1
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
15 changes: 10 additions & 5 deletions hypervnewvm/newvmcopy.ps1 → hyperv/newvmcopy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ Generation for the new Virtual Machine
Log file. If not there, it will write the output to the console
.EXAMPLE
.\NewVMCopy.ps1 -virtualMachineName NewVMCopy -pathToBaseVHD C:\BaseVHDs\WindowsServer2012R2.vhdx -pathToWorkingDir -virtualSwitch Network1 C:\temp -logFile logfile.txt
.\NewVMCopy.ps1 -virtualMachineName NewVMCopy -pathToBaseVHD `
C:\BaseVHDs\WindowsServer2012R2.vhdx -pathToWorkingDir C:\temp `
-virtualSwitch Network1 -logFile logfile.txt
NewVMCopy.ps1 [-pathToBaseVHD] <String> [-pathToWorkingDir] <String> `
[[-virtualMachineName] <String>] [-virtualSwitch] <String> [[-vmCpus] <Int32>] `
[[-vmRam] <Int64>] [[-generation] <Int32>] [[-logFile] <String>] [<CommonParameters>]
#>

[CmdletBinding(DefaultParametersetName='None')]
Expand Down Expand Up @@ -133,7 +138,7 @@ try {
Write-Log -Message "Enabling Guest Service Interface Integration Service on $virtualMachineName" -logfile $logFile
$error.clear()
try {
Enable-VMIntegrationService Name "Guest Service Interface" -VMName $virtualMachineName -ErrorAction Stop
Enable-VMIntegrationService Name "Guest Service Interface" -VMName $virtualMachineName -ErrorAction Stop
Write-Log -Message "Guest Service Interface Integration Service on $virtualMachineName enabled successfully" -logfile $logFile
} Catch {
Write-Log -Message "Could not enable Guest Service Interface Integration Service on $virtualMachineName" -Level ERROR -logfile $logFile
Expand All @@ -148,7 +153,7 @@ try {
$timer = 0
$timerLimit = 600
$timerIncrement = 20
($startResult = Start-VM Name $virtualMachineName -ErrorAction Stop) | out-null
Start-VM -Name $virtualMachineName -ErrorAction Stop | out-null
$vmStatus = Get-VMIntegrationService -VMName $virtualMachineName -Name Heartbeat
while ($vmStatus.PrimaryStatusDescription -ne "OK" -And $timer -lt $timerLimit)
{
Expand All @@ -170,7 +175,7 @@ try {
}

# Check IP Address
($vmIPAddress = (Get-VMNetworkAdapter -VMName $virtualMachineName -Name 'Network Adapter').IPAddresses | where { $_ -match "\." } ) | out-null
($vmIPAddress = (Get-VMNetworkAdapter -VMName $virtualMachineName -Name 'Network Adapter').IPAddresses | Where-Object { $_ -match "\." } ) | out-null
$timer = 0
$timerLimit = 600
$timerIncrement = 20
Expand All @@ -179,7 +184,7 @@ while ( !$vmIPAddress -And $timer -lt $timerLimit)
sleep $timerIncrement
$timer = $timer + $timerIncrement
Write-Log -Message "Waiting for $VirtualMachineName IP Address" -Level INFO -logfile $logFile
($vmIPAddress = (Get-VMNetworkAdapter -VMName $virtualMachineName -Name 'Network Adapter').IPAddresses | where { $_ -match "\." } ) | out-null
($vmIPAddress = (Get-VMNetworkAdapter -VMName $virtualMachineName -Name 'Network Adapter').IPAddresses | Where-Object { $_ -match "\." } ) | out-null
}
If ($timer -ge $timerLimit) {
Write-Log -Message "$virtualMachineName get IP Address timed out" -Level ERROR -logfile $logFile
Expand Down
63 changes: 0 additions & 63 deletions hypervnewvm/README.md

This file was deleted.

42 changes: 42 additions & 0 deletions vmware/CreateSnapshot.ps1
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
}
51 changes: 51 additions & 0 deletions vmware/LoginTovSphere.ps1
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.

0 comments on commit 678b7ae

Please sign in to comment.