VMware Cloud Community
Shweyaz
Contributor
Contributor

Add VM to inventory of esxi server directly without vcenter using powershell

Hi,

I have below code working if I have existing vmfolder input, when want to add VM to inventory of cluster consisting data stores.

But, I want VM to be added to single esxihost  directly and not to any existing vmfolder.

What I should pass in Location argument? Please help.

$esxihost = "10.xxx.xx.xx"

Connect-VIServer $esxihost

#$VMFolder = "Snap-Mirror"

$vm = @("Auto1.xxx.xxx.local")


$ESXHost = Get-VMHost $esxihost

#Write-Output $ESXHost


$Datastores = $ESXHost | Get-Datastore

#Write-Output $Datastores


# Set up Search for .VMX Files in Datastore

foreach($Datastore in $Datastores) {

   $ds = Get-Datastore -Name $Datastore | %{Get-View $_.Id}

   $SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

   $SearchSpec.matchpattern = "*.vmx"

   $dsBrowser = Get-View $ds.browser

   $DatastorePath = "[" + $ds.Summary.Name + "]"

   # Find all .VMX file paths in Datastore, filtering out ones with .snapshot (Useful for NetApp NFS)

   $SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec) | where {$_.FolderPath -notmatch ".snapshot"} | %{$_.FolderPath + ($_.File | select Path).Path}

  

  

   #Register all .vmx Files as VMs on the datastore

   foreach($VMXFile in $SearchResult) {

  

     New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $ESXhost.Id  -RunAsync

      #New-VM -Confirm:$false -Verbose:$true -Name $vm -VMHost $ESXHost -VMFilePath "[$dsv1] $vm/$vm.vmx"

    } }

} }


Thanks 

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

I suspect you might be mixing two things here.

You can register the VM on a specific ESXi node, which you did with the VMHost parameter.

The Location parameter allows you to define a Folder of type VMs and Templates.

These folders are created under the Datacenter, there is no ESXi node in the path.

Or do you mean something else?
Perhaps a screenshot might help in that case?


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

Reply
0 Kudos
LucD
Leadership
Leadership

When you are only connected to the ESXi node, the root folder for VMs is the hidden folder named 'vm'.

You can do

New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location (Get-Folder -Name vm) -RunAsync


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

Reply
0 Kudos