VMware {code} Community
Footyfoot
Contributor
Contributor

ESXI PoweCLI Creating new NTFS partition with newly created VM

Hi!

Im looking for someone who can help me with my problem.

I am using ESXI and PowerCLI to automate a Windows 10 creation and installation.

I made a unatended ISO so that all the setup questions are ingored. But my ISO wants to look for a NTFS hard disk to install on.

So now in my script im trying to setup a new partition before it loads the ISO into the new VM. But I cant seem to get it right. It says: Error: A positional parameter cannot be found that accepts argument 'New-Partition'.

Im doing this for a school project and i have little experience with Powershell but so far it has been very interesting.

My code: (ive used the main part that was created by David Rodrigeuz and added the commands i needed to it)

<#
===========================================================================
Script Created by: David Rodriguez
Blog: www.sysadmintutorials.com
Twitter: @systutorials
Youtube: https://www.youtube.com/user/sysadmintutorials
===========================================================================
.DESCRIPTION
This script will automate the creation of virtual machine within ESXi
#>

$vmhostipaddress = Read-Host "Enter the IP of your VMware ESXi Host"
$vmhostcreds = Get-Credential -Message "Please enter the root username and password"

Write-Host -ForegroundColor Yellow "`n---- Connecting to VMware ESXi Host ----"
try{
Connect-VIServer $vmhostipaddress -User "$($vmhostcreds.UserName)" -Password ([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($vmhostcreds.Password))) -ErrorAction Stop | Out-Null
Write-Host -ForegroundColor Green "Successfully connected to VMware ESXi Host"
}
catch
{
Write-Warning -Message $("Something went wrong connecting to the ESXi host. Please read the error message and re-run the script. Error: "+ $_.Exception.Message)
Break;
}

Write-Host "Operating system options" -ForegroundColor Yellow
Write-Host "1. Windows10"
Write-Host "2. Linux"

$operatingsystem = Read-Host "Select the Operating System you wish to install by typing in either 1 or 2?"

IF ($operatingsystem -eq 1)
{
$guestid = "windows9_64Guest"
}
IF ($operatingsystem -eq 2)
{
$guestid = "ubuntu64Guest"
}

$vmname = Read-Host "Please enter the name of the virtual machine?"
$cpuamount = Read-Host "How many CPU's would you like to provision?"
$ramamount = Read-Host "How much RAM do you wish to provision in GB's?"
$hddamount = Read-Host "How much Disk storage do you wish to provision in GB's?"

try
{
New-VM -Name $vmname -Datastore datastore1 -NumCpu $cpuamount -MemoryGB $ramamount -DiskGB $hddamount -DiskStorageFormat Thin -NetworkName "VM Network" -CD -GuestId $guestid -ErrorAction Stop | Out-Null
Get-VM $vmname New-Partition -DiskNumber 0 -size $hddamount
Get-VM $vmname | Get-NetworkAdapter | Set-NetworkAdapter -Type VMXNet3 -Confirm:$False -ErrorAction Stop | Out-Null
Start-VM $vmname -confirm
get-vm -name $vmname | get-cddrive| Set-CDDrive -Connected $true -IsoPath '[datastore1] ISO2\en_windows_10_multiple_editions_x64_dvd_6846432.iso' -startconnected $true
Write-Host "Successfully created $($vmname)" -ForegroundColor Green
}
catch
{
Write-Warning -Message $("Something went wrong in creating the virtual machine: $($vmname). Please read the error message and re-run the script. Error: "+ $_.Exception.Message)
Break;
}

 

0 Kudos
0 Replies