bdumont
Contributor
Contributor

Help required - multiple VM build script

Hi all

Could really use some help here please?

This is the script I am trying to get working in PCLI 5.1 against a eSXI 6.5 vCenter.

$vms = Import-CSV "C:\pshome\NewVMs.csv"

foreach ($vm in $vms){
#Assign Variables
$Template = Get-Template -Name $vm.Template
$Cluster = $vm.Cluster
$Datastore = Get-Datastore -Name $vm.Datastore
$Custom = Get-OSCustomizationSpec -Name $vm.Customization
$vCPU = $vm.vCPU
$Memory = $vm.Memory
$Network = $vm.Network
$Location = $vm.Location
$VMName = $vm.Name

#Where the VM gets built
New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -Location $Location -DiskStorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom
Start-Sleep -Seconds 10

#Where the vCPU, memory, and network gets set
$NewVM = Get-VM -Name $VMName
$NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
$NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false
}

CSV File:
Name,Template,Cluster,Datastore,Customization,vCPU,Memory,Network,HardDrive,HardDrive2,DiskStorageFormat,Location
ScriptTest,T-Win2016STD,Production,0283-L130-P-F-1000GB,T-Win2016STD,1,2,192.168.1.0/24,40,10,Thin,JPI

 

The VM does build but none of the settings are as specified (2 CPU's instead of 1, a single 50Gb disk etc..) I receive this error in PowerCLI 

PowerCLI C:\pshome> .\NewVMs.ps1
New-VM : 22/01/2021 13:53:44 New-VM Operation is not valid due to the current state of the object.
At C:\pshome\NewVMs.ps1:16 char:1
+ New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

Set-VM : 22/01/2021 13:53:55 Set-VM The object 'vmodl.ManagedObject:' has already been deleted or has not been completely created
At C:\pshome\NewVMs.ps1:21 char:10
+ $NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Set-VM], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVM

And this error in vCenter

A specified parameter was not correct: nicsettings:adapter:ip

Any help would be much appreciated!

Labels (3)
0 Kudos
5 Replies
LucD
Leadership
Leadership

First, why are you using PowerCLI 5.1?
Are you sure?
Isn't that the PowerShell version?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bdumont
Contributor
Contributor

Hi LucD

Its says PowerCHLI 5.1 Release 2 Patch 2 at the top of the cmd window.

0 Kudos
LucD
Leadership
Leadership

That is a very old PowerCLI version.
Can you check if that info is indeed correct?
Open a PowerShell prompt (that is not the PowerCLI icon, just at the Run command type powershell),  at the PS> prompt run the following

Get-Module -Name VMware* -ListAvailable


If that doesn't return anything, run the following from the PS prompt.

Get-PSSnapin

If that confirms that you are running PowerCLI 5.1, I would strongly suggest your first upgrade your PowerCLI version.

See Welcome PowerCLI to the PowerShell Gallery – Install Process Updates and  Updating PowerCLI through the PowerShell Gallery for detailed instructions.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bdumont
Contributor
Contributor

Hi lucD

I finally have some time to come to this 🙂
Appreciate you help here. I couldn't script my way out of a wet paper bag.

Get-Module -Name VMware* -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 12.1.0.... VMware.VimAutomation.Vds {Add-VDSwitchPhysicalNetworkAdapter, Add-VDSwitchVMHost, E...
Script 12.1.0.... VMware.VimAutomation.Vmc {Add-VmcSddcHost, Connect-Vmc, Disconnect-Vmc, Get-AwsAcco...
Script 12.0.0.... VMware.VimAutomation.vROps {Connect-OMServer, Disconnect-OMServer, Get-OMAlert, Get-O...
Script 12.1.0.... VMware.VimAutomation.WorkloadMan... {Disable-WMCluster, Enable-WMCluster, Get-WMCluster, Get-W...
Script 12.1.0.... VMware.VumAutomation {Add-EntityBaseline, Copy-Patch, Get-Baseline, Get-Complia...

PowerCLI C:\pshome> Get-PSSnapin

Name : Microsoft.PowerShell.Diagnostics
PSVersion : 5.1.17763.1007
Description : This Windows PowerShell snap-in contains Windows Eventing and Performance Counter cmdlets.

 

Name : Microsoft.PowerShell.Host
PSVersion : 5.1.17763.1007
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-Transcript and Stop-Transcript) that are provided for use with the Windows PowerShell
console host.

Name : Microsoft.PowerShell.Core
PSVersion : 5.1.17763.1007
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.

Name : Microsoft.PowerShell.Utility
PSVersion : 5.1.17763.1007
Description : This Windows PowerShell snap-in contains utility cmdlets that are used to view and organize data in different ways.

Name : Microsoft.PowerShell.Management
PSVersion : 5.1.17763.1007
Description : This Windows PowerShell Snap-In contains management cmdlets that are used to manage Windows components.

Name : Microsoft.PowerShell.Security
PSVersion : 5.1.17763.1007
Description : This Windows PowerShell Snap-In contains cmdlets to manage Windows PowerShell security.

Name : Microsoft.WSMan.Management
PSVersion : 5.1.17763.1007
Description : This Windows PowerShell snap-in contains cmdlets (such as Get-WSManInstance and Set-WSManInstance) that are used by the Windows PowerShell host to manage
WSMan operations.

Name : VMware.VimAutomation.Core
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains Windows PowerShell cmdlets for managing vSphere.

Name : VMware.VimAutomation.Vds
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains cmdlets that let you manage vSphere Distributed Switches.

Name : VMware.VimAutomation.License
PSVersion : 2.0
Description : This Windows Powershell snap-in contains cmdlets for managing License components.

Name : VMware.DeployAutomation
PSVersion : 2.0
Description : Cmdlets for Rule-Based-Deployment

Name : VMware.ImageBuilder
PSVersion : 2.0
Description : This Windows PowerShell snap-in contains VMware ESXi Image Builder cmdlets used to generate custom images.

0 Kudos
LucD
Leadership
Leadership

I'm afraid you are in a mixed environment (old and new PowerCLI modules).

The best approach would be to remove everything and start fresh.
Have a look at Welcome PowerCLI to the PowerShell Gallery – Install Process Updates and Updating PowerCLI through the PowerShell Gallery.

Just make sure to remove everything before attempting a fresh PowerCLI installation with Install-Module.
The PSSnapin can be removed via Programs and Features, the modules can be removed by just deleting the folders.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos