Sanity Check | Creating a VM in Powershell?

Hey All,

I’m creating a script for practice and had some issues near the end. I’m creating this script to ask for information and create a VM on a remote hyper v server with sed information. However at the end when it attempts to create the VM it has an error saying:

Invoke-Command : A positional parameter cannot be found that accepts argument ‘vm01’. At E:MiscPowerShell-Script-TempRemoteHV-VM-Creation.ps1:47 char:1

What could be causing that? I’ve been tearing my hair out trying to find the answer.

Here’s the code below:

This is to remotely create VMs via powershell CLI

This can be used to remotely create vms in other Hyper-V hosts and can be linked / changed to other scripts to automate the deployment of VMs

clear

This asks for the VM host you want to create the vm on

$HVRemoteHost = Read-Host -Prompt 'What system do you want to create VMs on?' 

VM name variable taken from the input

$VMName = Read-Host -Prompt 'What do you want the name to be?' 

Asking for VM memory variable

$VMMemory = Read-Host -Prompt 'How much Memory should the vm have?(example: 4GB )' 

Asking for VM memory variable

$VMVHDX = Read-Host -Prompt 'How large will the VHDX be?' 

Listing remote hosts virtual networks

$InvokeVMNICs = Invoke-Command -ComputerName $HVRemoteHost -ScriptBlock {

Get-VMSwitch -SwitchType External | Sort-Object Name

} #This will then print out what was above ^ Write-Host $InvokeVMNICs

#Asking what VMNIC you'd like to add $VMNIC = Read-Host -Prompt 'What network would you like the VM to be a part of?' 

This will list the disks

Invoke-Command -ComputerName $HVRemoteHost -ScriptBlock {

Get-PSDrive

}

#Asking the for the VM location after listing the disks on the remote host machines $VMlocation = Read-Host -Prompt 'Where do you want to store this VM' 

This is to invoke the command on the remote machine

Invoke-Command -ComputerName $HVRemoteHost -ScriptBlock {

This will create the vm on the remote machine

New-VM -Name $VMName -MemoryStartupBytes $VMMemory -BootDevice VHDX -NewVHDPath $VMLocation$VMName.vhdx -Path $VMLocation -NewVHDSizeBytes $VMVHDX -Generation 2 -Switch $VMNIC

 #This allows the passing through of the variables 

} -ArgumentList $HVRemoteHost $VMName $VMMemory $InvokeVMNICs $VMNIC $VMlocation $VMVHDX

Then the VM will start

Start-VM -Name $VMName

Connect-VM $VMName

submitted by /u/zveroboy152
[link] [comments]

Leave a Reply