1、從SDK中使用到了兩個方法
a. SearchDatastoreSubFloders_Task:在一個特定的datastore(數據存儲)中查找所有的.VMX文件。
b.RegisterVM_Task:注冊一個VMX文件
2、在常見的FAQ中RegisterVM_Task方法解答中:resourcepool是必須的。
3、客戶機會被注冊到一個特定的數據中心(datacenter)或者是集群中的一個數據中心。
4、客戶機注冊的文件夾將會隱藏一個文件夾"VM"
5、客戶機注冊的資源池也是隱藏的資源池"Resources"
6、被注冊的名稱與現有的重名,RegisterVM_Task方法將會失敗。
7、*.VMX文件就是要注冊的文件名,例如:PC1.vmx,就是客戶機的PC1
$folder = Get-View (Get-Datacenter -Name <datacenter-name> | Get-Folder -Name "vm").ID $pool = Get-View (Get-Cluster -Name <cluster-name> | Get-ResourcePool -Name "Resources").ID $guestname = [regex]"^([\w]+).vmx" $esxImpl = Get-VMHost -Name <VMHost-name> $esx = Get-View $esxImpl.ID $dsBrowser = Get-View $esx.DatastoreBrowser foreach($dsImpl in $dsBrowser.Datastore){ $ds = Get-View $dsImpl $vms = @() foreach($vmImpl in $ds.Vm){ $vm = Get-View $vmImpl $vms += $vm.Config.Files.VmPathName } $datastorepath = "[http://" + $ds.Summary.Name + "|http://" + $ds.Summary.Name + "]" $searchspec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec $searchSpec.matchpattern = "*.vmx" $taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($datastorePath, $searchSpec) $task = Get-View $taskMoRef while ($task.Info.State -eq "running"){$task = Get-View $taskMoRef} foreach ($file in $task.info.Result){ $found = $FALSE foreach($vmx in $vms){ if(($file.FolderPath + $file.File[0].Path) -eq $vmx){ $found = $TRUE } } if (-not $found){ $vmx = $file.FolderPath + $file.File[0].Path $res = $file.File[0].Path -match $guestname $folder.RegisterVM_Task($vmx,$matches[1],$FALSE,$pool.MoRef,$null) } } }