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."
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?
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.
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 }
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?
Line 35 is
$VMCount | % { New-VM -Name newvm$_ -VM $vm -Datastore $datastore -VMHost $vmhost }
