使用PowerShell将虚拟机模板(.VMTX)添加到清单

这是一个脚本,它将扫描数据存储中的VM模板(.VMTX),并将其添加到清单中。这是“ 将虚拟机(.VMX)添加到清单”  脚本的修改版本

您必须安装VMware PowerCLI才能拥有PowerShell运行脚本所需的cmdlet。

将VMTX(虚拟机模板)从数据存储添加到清单:

1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
三十
31
32
33
34
35
36
37
38
39
40
#####################################################################
#          Load VMware Plugins and vCenter Connect                  #
#####################################################################
 
Add-PSSnapin vmware.vimautomation.core
 
connect-viserver -server ENTER VCENTER FQDN HERE
#####################################################################
#       Add .VMTX (VM Templates) to Inventory from Datastore        #
#####################################################################
 
# Variables: Update these to the match the environment
$Cluster = "ENTER CLUSTER NAME HERE"
$Datastore = "ENTER DATASTORE NAME HERE"
$VMFolder = "ENTER FOLDER NAME HERE"
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1
 
foreach ( $Datastore in get-datastore $Datastore )
{
  # Collect .vmtx paths of registered VMs on the datastore
     $registered = @{}
     Get-Template -Datastore $Datastore | ForEach-Object -Process {
     $registered .Add( $_ .Name+ '.vmtx' , $true )
  }
 
  # Set up Search for .VMTX Files in Datastore(s)
     $null = New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\'
     $unregistered = @( Get-ChildItem -Path TgtDS: -Recurse | `
     Where-Object -FilterScript {
     $_ .FolderPath -notmatch '.snapshot' -and $_ .Name -like '*.vmtx' -and ! $registered .ContainsKey( $_ .Name)
  }
  )
  Remove-PSDrive -Name TgtDS
 
  #Register all .vmtx Files as VMs on the datastore
  foreach ( $vmtxFile in $unregistered )
  {
  New-Template -Name $vmtxFile .Name -TemplateFilePath $vmtxFile .DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync
  }
}

让我们来看看它的作用!

我想扫描“ Synology LUN”数据存储,并将其找到的任何虚拟机模板添加到名为“ Lab Cluster 01”的集群“ _Templates” 文件夹中  

VMTX Powershell脚本-环境变量

我更喜欢从PowerShell ISE中运行脚本,因为我认为它可以提供更多控制权。我更新了变量  (以黄色突出显示)并运行了它:

VMTX Powershell脚本-来自ICE的Ran

在vSphere Client的“任务”窗格中,如果看到失败的任务,则脚本会找到清单中已存在的模板。现在不在我的清单中的模板位于“ _Templates”文件夹中:

VMTX-脚本运行后的vCenter

从数据存储群集将VMX(虚拟机)添加到清单:

$数据存储  变量是可以改变的扫描数据存储群集,而不是具体的:

1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
三十
31
32
33
34
35
36
37
38
39
40
#####################################################################
#          Load VMware Plugins and vCenter Connect                  #
#####################################################################
 
Add-PSSnapin vmware.vimautomation.core
 
connect-viserver -server ENTER VCENTER FQDN HERE
###############################################################
#    Add .VMTX (Templates) to Inventory from Storage Cluster  #
###############################################################
 
# Variables: Update these to the match the environment
$Cluster = "ENTER CLUSTER NAME HERE"
$Datastore = get-datastorecluster "ENTER DATASTORE CLUSTER NAME HERE" | get-datastore
$VMFolder = "ENTER FOLDER NAME HERE"
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1
 
foreach ( $Datastore in get-datastore $Datastore )
{
  # Collect .vmtx paths of registered VMs on the datastore
     $registered = @{}
     Get-Template -Datastore $Datastore | ForEach-Object -Process {
     $registered .Add( $_ .Name+ '.vmtx' , $true )
  }
 
  # Set up Search for .VMTX Files in Datastore(s)
     $null = New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\'
     $unregistered = @( Get-ChildItem -Path TgtDS: -Recurse | `
     Where-Object -FilterScript {
     $_ .FolderPath -notmatch '.snapshot' -and $_ .Name -like '*.vmtx' -and ! $registered .ContainsKey( $_ .Name)
  }
  )
  Remove-PSDrive -Name TgtDS
 
  #Register all .vmtx Files as VMs on the datastore
  foreach ( $vmtxFile in $unregistered )
  {
  New-Template -Name $vmtxFile .Name -TemplateFilePath $vmtxFile .DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync
  }
}

我更新了突出显示的变量:

VMTX Powershell脚本-从ICE运用于存储群集

我希望如果您遇到需要扫描并将多个VM模板添加到清单的情况,这将有助于加快该过程!

猜你喜欢

转载自www.cnblogs.com/donray888/p/11589592.html