Home A PowerShell script to Create a new VMware VM
Post
Cancel

A PowerShell script to Create a new VMware VM

This script will create a inventory item aka a virtual machine using PowerShell. It connects an ISO image and defines all of the hardware. It does not install the OS.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$ErrorActionPreference = "Stop"

$vCenter = "something"  # IP or FQDN of vCenter
$vCUser = "administrator@vsphere.local"
$vCPW = "password"
$Host_name = "something"  # IP or FQDN of the host to put the VM on
$VM_name = "AdamsVM"
$Date = Get-Date
$CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$NotesString = "Created on $Date by $CurrentUser"
$DS_name = "SSD"

Write-Host "Connecting to vCenter" -ForeGroundColor "Green"
Connect-VIServer $vCenter -User $vCUser -Password $vCPW -Force | Out-Null
Write-Host " Connected to vCenter" -ForeGroundColor "Green"

$VMHost = Get-VMHost -Name $Host_name

Write-Host "Creating the VM..." -ForeGroundColor "Green"

New-VM -Name $VM_name -ResourcePool $VMHost `
-Datastore $DS_name -NumCPU 2 -CoresPerSocket 1 -MemoryGB 4 -DiskGB 40 `
-NetworkName "VM Network" -DiskStorageFormat Thin -Notes "$NotesString" `
-CpuHotAddEnabled 1 -MemoryHotAddEnabled 1 -GuestId ubuntu64Guest `
| Out-Null

Write-Host " VM Created" -ForeGroundColor "Green"

Write-Host "Attaching the CD ISO..." -ForeGroundColor "Green"

New-CDDrive -VM $VM_name -ISOPath "[3TB1] ubuntu-22.04-live-server-amd64.iso" `
-StartConnected | Out-Null

Write-Host " CD attached" -ForeGroundColor "Green"

Disconnect-VIServer -Server $vCenter -Force -Confirm:$false

Write-Host "All done." -ForeGroundColor "Green"
This post is licensed under CC BY 4.0 by the author.