Skip to content

Commit

Permalink
benarm-wmi-v2 - code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Dragan authored and Mateusz Dragan committed May 15, 2016
1 parent a264a8b commit c98d327
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
38 changes: 19 additions & 19 deletions hyperv-samples/benarm-wmi-v2/AddHVAdmin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@ $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
Clear-Host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
{
# We are not running "as Administrator" - so relaunch as administrator

# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;

# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Indicate that the process should be elevated
$newProcess.Verb = "runas";

# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);

# Exit from the current, unelevated, process
exit
}
# Exit from the current, unelevated, process
exit
}

# Prompt for the virtual machine to use
$Domain = Read-Host "Specify the domain of the user to add to Hyper-V Administrators (use $($env:ComputerName) for this computer)"
Expand Down
13 changes: 8 additions & 5 deletions hyperv-samples/benarm-wmi-v2/EventNotification.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ $Query = "Select * from __InstanceModificationEvent within 3 where TargetInstanc
$VMName = $Event.SourceEventArgs.NewEvent.TargetInstance.ElementName

switch ($Event.SourceEventArgs.NewEvent.TargetInstance.EnabledState)
{
{
2 {$vmState = "running"}
3 {$vmState = "turned off"}
9 {$vmState = "paused"}
6 {$vmState = "in a saved state"}
10 {$vmState = "starting"}
4 {$vmState = "stopping"}
default {$vmState = "in an unknown state..."}
}
}

if ($Event.SourceEventArgs.NewEvent.TargetInstance.EnabledState -eq 1)
{$vmState = $Event.SourceEventArgs.NewEvent.TargetInstance.OtherEnabledState}
if ($Event.SourceEventArgs.NewEvent.TargetInstance.EnabledState -eq 1)
{
$vmState = $Event.SourceEventArgs.NewEvent.TargetInstance.OtherEnabledState
}

write-host "The virtual machine '$($vmName)' is now $($vmState)."}
Write-Host "The virtual machine '$($vmName)' is now $($vmState)."
}

# Register for the events
Register-WMIEvent -Query $Query -Action $Action -Namespace root\virtualization\v2
Expand Down
46 changes: 29 additions & 17 deletions hyperv-samples/benarm-wmi-v2/New-VM.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,48 @@ while ($newVSSD.Properties -eq $null) {}
$newVSSD.Properties.Item("ElementName").value = $VMName

# Get the VirtualSystemManagementService object
$VMMS = gwmi MSVM_VirtualSystemManagementService -namespace "root\virtualization\v2" -computername $HyperVServer
$VMMS = Get-WmiObject MSVM_VirtualSystemManagementService -Namespace "root\virtualization\v2" -ComputerName $HyperVServer

# Create the VM
$result = $VMMS.DefineSystem($newVSSD.GetText(1))

#Return success if the return value is "0"
if ($Result.ReturnValue -eq 0)
{write-host "Virtual machine created."}
{
Write-Host "Virtual machine created."
}

#If the return value is not "0" or "4096" then the operation failed
ElseIf ($Result.ReturnValue -ne 4096)
{write-host "Failed to create virtual machine"}

Else
{#Get the job object
elseif ($Result.ReturnValue -ne 4096)
{
Write-Host "Failed to create virtual machine"
}

else
{

#Get the job object
$job=[WMI]$Result.job

#Provide updates if the jobstate is "3" (starting) or "4" (running)
while ($job.JobState -eq 3 -or $job.JobState -eq 4)
{write-host $job.PercentComplete
start-sleep 1
{
Write-Host $job.PercentComplete
Start-Sleep 1

#Refresh the job object
$job=[WMI]$Result.job}
$job=[WMI]$Result.job
}

#A jobstate of "7" means success
#A jobstate of "7" means success
if ($job.JobState -eq 7)
{write-host "Virtual machine created."}
Else
{write-host "Failed to create virtual machine"
write-host "ErrorCode:" $job.ErrorCode
write-host "ErrorDescription" $job.ErrorDescription}
}
{
Write-Host "Virtual machine created."
}
else
{
Write-Host "Failed to create virtual machine"
Write-Host "ErrorCode:" $job.ErrorCode
Write-Host "ErrorDescription" $job.ErrorDescription
}
}

0 comments on commit c98d327

Please sign in to comment.