Gaprofitt17
Enthusiast
Enthusiast

Register VM script not working

Jump to solution

I've been using this script for about a month, been working fine, then all of a sudden it stops working.

$Cluster = "Cluster-1"

$Datastores = "CBE_TO_QTS_VMMigration_OS_01"

$VMFolder = "TEST"

$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1

foreach($Datastore in Get-Datastore $Datastores) {

# Set up 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) {

New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFolder -RunAsync

}

}

I get this everytime.  The datastore is mounted to all the host in the cluster.

Exception calling "SearchDatastoreSubFolders" with "2" argument(s): "Invalid datastore path '[CBE_TO_QTS_VMMigration_OS_01 CBE_TO_QTS_VMMigration_OS_01 CBE_TO_QTS_VMMigration_OS_01

     | CBE_TO_QTS_VMMigration_OS_01]'."

Any guesses or suggestions?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

It looks like that datastorename is in there 4 times.

Do you have multiple vCenter connections open?

Check what is in $global:defaultviservers


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

It looks like that datastorename is in there 4 times.

Do you have multiple vCenter connections open?

Check what is in $global:defaultviservers


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

View solution in original post

0 Kudos
Gaprofitt17
Enthusiast
Enthusiast

That was it, I was connected to the same vCenter twice using seperate credentials.. 

Thanks so much, as always..

0 Kudos