VMware Cloud Community
LSWS
Contributor
Contributor

Clone virtual machine script.

I'm working on a script to take the name of one VM and clone it to a set amount of additional VMs. I am getting the below error. I am pretty new to PowerCLI so any help would be great. Thanks

 

New-VM : Cannot convert 'System.Object[]' to the type
'VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.StorageResource'
required by parameter 'Datastore'. Specified method is not supported.
At C:\Scripts\CloneVM.ps1:34 char:56

 

# Variables
$vCenter = "vcenterhost"
$vCenterUserName = "loginhere"
$vCenterPassword = "passwordhere"

 

# Loading PowerCLI Modules

. “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”

# Building Credential
$Password = $vCenterPassword | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($vCenterUserName, $Password)

# Connecting to vCenter
Connect-VIServer -Server $vCenter -Credential $Credential | Out-Null

Write-Host ""
Write-Host "************************************************************************"
Write-Host "***   Clone VM                                                       ***"
Write-Host "************************************************************************"
Write-Host ""

$VMName =  Read-Host -Prompt "Enter machine to clone"
$VMCount = Read-Host -Prompt "Enter how many clones to create"

$vm = Get-VM $VMName

$vmhost = Get-VMHost

$datastore = Get-datastore datastore1

$VMCount | % { New-VM -Name newvm$_ -VM $vm -Datastore $datastore -VMHost $vmhost }

Write-Host ""
Read-Host "Script finished...  Press any key to quit."

Reply
0 Kudos
6 Replies
RvdNieuwendijk
Leadership
Leadership

It looks like you are trying to use an array as the value of the -Datastore parameter. Do you have more than one datastore called datastore1?

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LSWS
Contributor
Contributor

No just one data store, I think the original script I took that piece from used one.
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can try to modify the line:

$datastore = Get-datastore datastore1

into:

$datastore = Get-datastore datastore1 -RelatedObject $vmhost

This will return only the datastore called datastore1 on the host in variable $vmhost.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LSWS
Contributor
Contributor

Still getting an error,

 

New-VM : Cannot convert 'System.Object[]' to the type
'VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.StorageResource'
required by parameter 'Datastore'. Specified method is not supported.
At C:\Scripts\CloneVM.ps1:35 char:56

 

I tried

$datastore = Get-datastore datastore1 -RelatedObject $vmhost

$VMCount | % { New-VM -Name newvm$_ -VM $vm -Datastore $datastore -VMHost $vmhost }

 

and also

$datastore = Get-datastore datastore1 -RelatedObject $vmhost

$VMCount | % { New-VM -Name newvm$_ -VM $vm -Datastore datastore1 -VMHost $vmhost }

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

The error is at line 35 in your script. In your code, I count only 25 lines. Which line do think generates the error message?

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LSWS
Contributor
Contributor

Line 35 is

$VMCount | % { New-VM -Name newvm$_ -VM $vm -Datastore $datastore -VMHost $vmhost }

Reply
0 Kudos