From ce5601464d1b0b810c533b5edb1de79a6bad6478 Mon Sep 17 00:00:00 2001 From: James Stringer <38541878+jamesstringerparsec@users.noreply.github.com> Date: Mon, 3 Jan 2022 17:25:58 +0100 Subject: [PATCH] Check VM doesn't already exist --- CopyFilesToVM.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CopyFilesToVM.ps1 b/CopyFilesToVM.ps1 index 3aa692a..70363a5 100644 --- a/CopyFilesToVM.ps1 +++ b/CopyFilesToVM.ps1 @@ -4253,7 +4253,11 @@ param( [string]$autologon ) - if (test-path $vhdPath) { + if ($(Get-VM -Name $VMName -ErrorAction SilentlyContinue) -ne $NULL) { + "Virtual Machine already exists with name $VMName, please delete existing VM or change VMName" + Exit + } + if (Test-Path $vhdPath) { Write-Host "Virtual Machine Disk already exists at $vhdPath, please delete existing VHDX or change VMName" Exit } @@ -4283,7 +4287,6 @@ param( Write-Host "Failed to create VHDX, stopping script" Exit } - } Check-Params @params