Skip to content

Instantly share code, notes, and snippets.

$HyperVServer = "."
$VMName = "Test VM"
 
# Get the management service
$VMMS = gwmi -namespace root\virtualization\v2 Msvm_VirtualSystemManagementService -computername $HyperVServer
 
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization\v2" -computername $HyperVServer
 
# SettingType = 3 ensures that we do not get snapshots
@BenjaminArmstrong
BenjaminArmstrong / VMCXEditor.ps1
Created April 19, 2017 00:18
PowerShell to edit VMCX files
# Editing a virtual machine file
# This PowerShell code takes an unregistered VMCX file
# It change the VM Name, disables Dynamic Memory, and sets the memory to 2GB
# It then saves the changed virtual machine configuration to a new path
# Parameters that will be changed
$VMConfigurationToEdit = "D:\VMs\Virtual Machines\3F99446F-1D9A-4010-8C8B-4E554E845181.vmcx"
$pathToSaveNewConfigTo = "D:\"
$newVMName= "NewVMName"
@BenjaminArmstrong
BenjaminArmstrong / ICMFunctionPassing.ps1
Created September 16, 2016 17:38
PowerShell Direct Tips - Functions
function Logger {
# Do pretty logging of information
}
function cleanupFile {
# Do some repetitive file manipulation
}
function PrepVM {
# Setup things inside a VM
@BenjaminArmstrong
BenjaminArmstrong / 1-ICMParameter.ps1
Last active September 16, 2016 17:46
PowerShell Direct Tips - Variables
$foo = "7"
Invoke-Command -VMName "Test" {
param($localfoo)
write-host $localfoo
} -ArgumentList $foo
@BenjaminArmstrong
BenjaminArmstrong / VMGuestOSInventory-V2.ps1
Created September 6, 2016 18:56
VM Guest OS Inventory - v2
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Get all virtual machine objects on the server in question
$VMs = Get-CimInstance -namespace root\virtualization\v2 Msvm_ComputerSystem -computername $HyperVServer -filter "Caption = 'Virtual Machine'"
# Create an empty hashtable
$table = @{}
# Go over each of the virtual machines
@BenjaminArmstrong
BenjaminArmstrong / VMScreenReader.ps1
Last active May 26, 2024 16:09
PowerShell script that will read a virtual machine screen to you
$VMName = "New Virtual Machine"
# Rectangle format == top left x, top left y, width, height
# $cropRectangle = New-Object System.Drawing.Rectangle 35,485,218,147
$cropRectangle = $null
$speakItToMe = $true
# Load System.Drawing
Add-Type -AssemblyName "System.Drawing"
# Load Tesseract - using code from https://github.com/jourdant/powershell-paperless
@BenjaminArmstrong
BenjaminArmstrong / LookForBitmap.ps1
Created June 16, 2016 20:35
How to look for an image on a VM screen
$VMName = "Windows"
# Load System.Drawing
Add-Type -AssemblyName "System.Drawing"
# Load AForge libraries - download from http://www.aforgenet.com/framework/
Add-Type -Path "D:\aForge\Release\AForge.dll"
Add-Type -Path "D:\aForge\Release\AForge.Math.dll"
Add-Type -Path "D:\aForge\Release\AForge.Imaging.dll"
# Number parameter is percentage threshold for acuracy. 0.9 = 90% match
@BenjaminArmstrong
BenjaminArmstrong / CheckPixel.ps1
Created June 15, 2016 18:00
Sample PowerShell script to check the pixel value of a virtual machine screen
$VMName = "VM 1"
Add-Type -AssemblyName "System.Drawing"
$VMCS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$($VMName)'"
# Get the resolution of the screen at the moment
$video = $VMCS.GetRelated("Msvm_VideoHead")
$xResolution = $video.CurrentHorizontalResolution[0]
$yResolution = $video.CurrentVerticalResolution[0]
@BenjaminArmstrong
BenjaminArmstrong / SaveVMBitmap.ps1
Last active June 18, 2024 02:35
Sample PowerShell script that shows you how to create a .BMP file of the display of a Hyper-V virtual machine.
$VMName = "VM 1"
$BMPName = "C:\Users\benja\Desktop\test.bmp"
Add-Type -AssemblyName "System.Drawing"
$VMCS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$($VMName)'"
# Get the resolution of the screen at the moment
$video = $VMCS.GetRelated("Msvm_VideoHead")
$xResolution = $video.CurrentHorizontalResolution[0]