Thanks again CRAD14 for your help.
I now have one script that adds vm's to the vCenter inventory with info from a CSV (thank you LucD for the script)
###############################################################################################################################
$targetVMX = Get-Content D:\temp\import-vm.csv
$Cluster = "CLUSTER_01"
$Datastores = "*_DATASTORE"
$VMFolder = "TESTFOLDER"
$ResourcePool = "TestRP"
$ESXHost = Get-Cluster $Cluster | Get-VMHost | Get-Random
foreach($Datastore in Get-Datastore $Datastores) {
# Search for .VMX Files in Datastore
$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) {
if($targetVMX -contains $VMXFile){
New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFolder -ResourcePool $ResourcePool -RunAsync
}
}
}
###############################################################################################################################
import-vm.csv content:
[01_DATASTORE] TESTVM03/TESTVM03.vmx
[02_DATASTORE] TESTVM02/TESTVM02.vmx
[03_DATASTORE] TESTVM01/TESTVM01.vmx
And I have another script that changes the display name of the vm with info from another CSV (thank you CRAD14 for the script)
###############################################################################################################################
$csv=Import-CSV "d:\temp\renamevm.csv"
FOREACH ($row in $csv)
{ Get-VM $($row.oldname) | Set-vm -name $($row.newname) -confirm:$false | Get-NetworkAdapter | Set-NetworkAdapter -Networkname $($row.vlan) -confirm:$false
}
###############################################################################################################################
renamevm.csv content:
oldname,newname,vlan
TESTVM01,sn123-TESTVM01,Testvlan1
TESTVM02,sn124-TESTVM02,Testvlan3
TESTVM03,sn125-TESTVM03,Testvlan5
Is it possible to combine both scripts into one script and using only one CSV-file?
The goal of this script is to register a number of vm's, change the display name and network with info from just one CSV-file.