CaptPete3
Contributor
Contributor

Register Existing VM's from .vmx file with 'known' folders'

Jump to solution

I have this small script that will register a list of existing VM's to a specific host.  I'd like to modify it so that it will place the vm's in known existing folders.  ie:  A csv instead of 'get-content' that has one column for the datastore path and another column folders.  Each VM could be in different folders and the way the script is written now it will just dump them all into one folder.

$VMXFILE = Get-Content D:\vmxlist.txt
$ESXHOST = "Hostname"
$VMFOLDER = "foldername"

#Register all .vmx Files as VMs
foreach($VMXFile in $VMXFILE) {
New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFOLDER -RunAsync
}

0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership

You mean something like this?

$ESXHOST = "Hostname"

#Register all .vmx Files as VMs
# CSV layout
# VMXFile,VMFOLDER
# VM1,Folder1
# VM2,Folder2

Import-Csv -Path .\names.csv -UseCulture |
ForEach-Object -Process {
    New-VM -VMFilePath $_.VMXFile -VMHost $ESXHost -Location $_.VMFOLDER -RunAsync
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
LucD
Leadership
Leadership

The Location parameter accepts a single Folder object.
But thanks to OBN, PowerCLI can also handle a String with the name of a Folder.

What OBN can't do is to convert a FolderPath to a single Folder object.
That ins fact the reason I wrote my Get-FolderByPath function.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
mpeneva
VMware Employee
VMware Employee

Hi,

I'm not pretty sure that I've understood you, however according to me, you could create a "map" that presents the relationship between vm and desired vm folder.

$vmMap = @{

   'vmName' = 'vmFolder'

}

And than in foreach statement, you'll iterate through the keys and then for a given key , you'll get the desired value (that is the desired vm folder). Could this be working for you ?

Regards,

Maya

0 Kudos
LucD
Leadership
Leadership

You mean something like this?

$ESXHOST = "Hostname"

#Register all .vmx Files as VMs
# CSV layout
# VMXFile,VMFOLDER
# VM1,Folder1
# VM2,Folder2

Import-Csv -Path .\names.csv -UseCulture |
ForEach-Object -Process {
    New-VM -VMFilePath $_.VMXFile -VMHost $ESXHost -Location $_.VMFOLDER -RunAsync
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
CaptPete3
Contributor
Contributor

Let me give that a try Luc.  That's exactly what I'm looking for.  I'll let you know.

0 Kudos
CaptPete3
Contributor
Contributor

It worked.  But failed when I used a folder that had a long path.

 

New-VM : 1/25/2023 8:43:33 AM New-VM Could not find Folder with name

/Data Center Name/QA/NextFolder/Nextfolder/Nextfolder

0 Kudos
CaptPete3
Contributor
Contributor

The folder is there.  Maybe the script doesn't like spaces in the folder names in the path?

0 Kudos
LucD
Leadership
Leadership

The Location parameter accepts a single Folder object.
But thanks to OBN, PowerCLI can also handle a String with the name of a Folder.

What OBN can't do is to convert a FolderPath to a single Folder object.
That ins fact the reason I wrote my Get-FolderByPath function.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos