Skip to content

Commit

Permalink
change VMName parameter to optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchCodeMonkey committed Sep 16, 2019
1 parent 9e87590 commit b737139
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions update-hosts.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<#
.SYNOPSIS
This script updates the hosts file with the current IP address of a Hyper-V virtual machine
This script updates the hosts file with the current IP address of running Hyper-V virtual machines
.PARAMETER VMName
The name of the Hyper-V virtual machine
The name of a Hyper-V virtual machine to start (optional)
.NOTES
The Hyper-V Default Switch is recreated every time the host machine is restarted.
Expand All @@ -20,7 +20,7 @@ using namespace Microsoft.HyperV.PowerShell
using namespace System.Net.Sockets


param ([Parameter(Mandatory=$true)][string]$VMName)
param ([string]$VMName)


# The location of the hosts file
Expand All @@ -30,30 +30,41 @@ $HOSTS_FILE = "C:\Windows\System32\drivers\etc\hosts"
$HOSTS_TEMPLATE = "C:\Windows\System32\drivers\etc\hosts.template"


$TargetVM = Get-VM -Name $VMName

If ($TargetVM.State -eq [VMState]::Off -or $TargetVM.State -eq [VMState]::Saved)
{
Write-Host "Starting virtual machine '$VMName'"
Start-VM -Name $VMName
}
ElseIf ($TargetVM.State -eq [VMState]::Paused)
If ( -not [string]::IsNullOrEmpty($VMName))
{
Write-Host "Resuming paused virtual machine '$VMName'"
Resume-VM -Name $VMName
}

$Heartbeat = $TargetVM.HeartBeat
Try
{
$TargetVM = Get-VM -Name $VMName -ErrorAction Stop
}
Catch [VirtualizationException]
{
Write-Host "Problem locating virtual machine '$VMName'"
}

While (($Heartbeat -ne [VMHeartbeatStatus]::OkApplicationsHealthy) -and ($Heartbeat -ne [VMHeartbeatStatus]::OkApplicationsUnknown))
{
Write-Host "Waiting for the virtual machine to finish booting..."
Start-Sleep -s 1
$Heartbeat = $TargetVM.HeartBeat
If ($TargetVM -ne $null)
{
If ($TargetVM.State -eq [VMState]::Off -or $TargetVM.State -eq [VMState]::Saved)
{
Write-Host "Starting virtual machine '$VMName'"
Start-VM -Name $VMName
}
ElseIf ($TargetVM.State -eq [VMState]::Paused)
{
Write-Host "Resuming paused virtual machine '$VMName'"
Resume-VM -Name $VMName
}

$Heartbeat = $TargetVM.HeartBeat

While (($Heartbeat -ne [VMHeartbeatStatus]::OkApplicationsHealthy) -and ($Heartbeat -ne [VMHeartbeatStatus]::OkApplicationsUnknown))
{
Write-Host "Waiting for the virtual machine to finish booting..."
Start-Sleep -s 1
$Heartbeat = $TargetVM.HeartBeat
}
}
}

Start-Sleep -s 1

$TemplateData = Get-Content -Path $HOSTS_TEMPLATE

Get-VM | Where-Object { $_.State -eq [VMState]::Running } | ForEach-Object {
Expand Down

0 comments on commit b737139

Please sign in to comment.