cat file.csv

Name,Template,ResourcePool,Datastore,Network,IP,Netmask,Gateway,DNS
VM1,Template1,ResourcePool1,Datastore1,Network1,192.168.1.2,255.255.255.0,192.168.1.1,8.8.8.8
VM2,Template2,ResourcePool2,Datastore2,Network2,192.168.1.3,255.255.255.0,192.168.1.1,8.8.8.8


在模版中安装 VMware Tools

# Import the CSV file
$csv = Import-Csv -Path 'C:\path\to\your\file.csv'

# Connect to the vCenter Server
$vcServer = 'vcenterservername'
$vcUsername = 'username'
$vcPassword = 'password'
Connect-VIServer -Server $vcServer -User $vcUsername -Password $vcPassword

# Loop through each row in the CSV
foreach ($row in $csv) {
    # Clone the template to create the new VM
    $vm = New-VM -Name $row.Name -Template $row.Template -ResourcePool $row.ResourcePool -Datastore $row.Datastore -Location (Get-Folder -NoRecursion)

    # Get the network adapter of the new VM
    $networkAdapter = Get-NetworkAdapter -VM $vm

    # Set the network name of the network adapter
    Set-NetworkAdapter -NetworkAdapter $networkAdapter -NetworkName $row.Network -Confirm:$false

    # Get the OS customization spec
    $spec = Get-OSCustomizationSpec -Name 'your_spec_name'

    # Set the IP address of the new VM
    $spec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $row.IP -SubnetMask $row.Netmask -DefaultGateway $row.Gateway -Dns $row.DNS

    # Apply the OS customization spec to the new VM
    $vm | Set-VM -OSCustomizationSpec $spec -Confirm:$false
}

# Disconnect from the vCenter Server
Disconnect-VIServer -Confirm:$false