I need to unregister and register a list of specific vm's that are spread across multiple datastores.
I have found these functions, but they process against all vm's in the datastore. Is there a way to modify the functions to process againsts a list of vm's?
I'm guessing you would need your input file to specify the vm and datastore path.
Function unregisterallvm([STRING]$datacentername,[STRING]$clustername) { $vms = Get-DataCenter $datacentername | Get-Cluster $clustername | get-vm Foreach ($vm in $vms) { $vmname = $vm.name Write-Host "Unregistering VM: $vmname from Cluster $clustername" Remove-VM -VM $vmname -DeleteFromDisk:$false -Confirm:$false -RunAsync } } Function registerallvm([STRING]$vCenterHost,[STRING]$datacentername,[STRING]$clustername,[STRING]$datastorename) { $datastore = Get-Datacenter $datacentername | Get-Datastore | ? {$_.name -match $datastorename} $datastoreshortname = $datastore.Name $ResourcePool = Get-Cluster -Name $clustername | Get-ResourcePool | Get-View $vmFolder = Get-View (Get-Datacenter -Name $datacentername | Get-Folder -Name "vm").id $vmdirs = (dir "vmstores:\$vCenterHost@443\$datacentername\$datastoreshortname\") Foreach ($f in $vmdirs) { $vmname = $f.Name $checkreg = (Get-Cluster $clustername | Get-VM | ? { $_.name -match $vmname}) If (!$checkreg) { "Registering VM: $vmname on: $clustername \ $datastoreshortname" $vmFolder.RegisterVM_Task("[$datastoreshortname]/$vmname/$vmname.vmx", $vmname, $false, $ResourcePool.MoRef, $null) } Else { "VM: $vmname is already registered with the same name. Skipping...." } } }
To unregister a bunch of VMs, you could do
Get-VM -Name vm1,vm2,vm3 | Remove-VM -DeleteFromDisk:$false -Confirm:$false
There are other ways to provide the list of VM names (from a TXT or CSV file for example).
How do you want to register the VMs ?
Are they on 1 or more specific datastores ?
If yes, you could use the script in VMX Raiders Revisited
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
How do you want to register the VMs ? Sorry, what do you mean?
Are they on 1 or more specific datastores ? Yes, they are on multiple datastores with vm's that are not being touched.
I think I see what you are saying.
use the Remove-VM cmdlet to remove the VM's that I need to remove.
Then use the VMX Raiders Revisted script and let it search for all unregistered VMs and register them.
Yes, that's what I ment
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
When the machine is unregistered and I run the VMX Raiders Revisited script I get a error that the specified key, name, or identifier already exists. It produces that error twice.
But it appears that the scripts succeeds and adds the vm to the correct folder.
I got it figured out. Thanks again Luc!
Hey Luc. Sorry to bother you, but I am still having an issue with the script. It appears that the script is not bypassing the registered vm's and attempting to register them. This appears to be why I am getting the before mentioned error.
The only modifcation I made was that I removed the $datastores variable so the script would search all datastores in the cluster.
$Cluster = "EngLab"
$VMFolder = "Test Sessions"
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1
foreach($Datastore in Get-Datastore ) {
# Collect .vmx paths of registered VMs on the datastore
$registered = @{}
Get-VM -Datastore $Datastore | %{$_.Extensiondata.LayoutEx.File | where {$_.Name -like "*.vmx"} | %{$registered.Add($_.Name,$true)}}
# Set up Search for .VMX Files in Datastore
New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null
$unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | `
where {$_.FolderPath -notmatch ".snapshot" -and $_.Name -like "*.vmx" -and !$registered.ContainsKey($_.Name)})
Remove-PSDrive -Name TgtDS
#Register all .vmx Files as VMs on the datastore
foreach($VMXFile in $unregistered) {
New-VM -VMFilePath $VMXFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync
}
}
Could you have multiple connections open ?
That way the same datastore would appear multiple times in the resulting objects from the Get-Datastore cmdlet.
Do a
$defaultViServers
If there is more than 1 entry, that most probably explains the phenomena you're seeing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No, sir. verified that I only have one connection.
Strange, can't see any obvious error.
Can you run the script in a debugger or wite some intermediate values to screen.
Is $registered filed in correctly in the first loop ?
Are the unregistered iVMs n the $unregistered array ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference