これは、VMテンプレート(.VMTX)に格納されたデータをスキャンし、リストに追加するスクリプトです。これは、「ある 仮想マシン(.VMXが)のリストに追加され、」 スクリプト修正版。
あなたはインストールする必要がありますVMwareのPowerCLIのを必要と実行PowerShellスクリプトコマンドレットを持っています。
vmtxテーブルリストに追加するデータストレージから(仮想マシンテンプレート):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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」データストレージ、仮想マシンテンプレートをして見つけるために、追加名「ラボクラスタ01」クラスタの「_Templates」 フォルダを 。
私はそれがより多くの制御を提供することができると思うので、PowerShellのISEからスクリプトを実行することを好みます。私は、変数更新 (黄色で強調表示)にし、それを実行します。
あなたが失敗したタスクが表示された場合はvSphere Clientで、「タスク」ペインでは、スクリプトが既に存在しているテンプレートのリストを見つけるでしょう。それは「_Templates」フォルダに配置されているテンプレートの私のリストにありません。
データストレージクラスタからVMX(仮想マシン)をリストに追加されます。
では $データ格納 変数ではなく、特定のよりも、スキャンデータストレージクラスタを変更することができます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
}
}
|
我更新了突出显示的变量:
我希望如果您遇到需要扫描并将多个VM模板添加到清单的情况,这将有助于加快该过程!