Thanks LucD, the logic makes complete sense now that I see it like that.
In regards to your question about downloading the .vmx file, I literally cannot do anything with the .vmx file on VMs which are running, this includes both VMs which are WORKING using the script and the examples which are erroring out. I can't even cat the contents of the .vmx file by logging directly into one of the esxi hosts and browsing the directory of the VM. Here is an example of a working VM:
I'm testing using a VM which is powered on, the name is mismatched to the .vmx name on disk <-- these are the VMs I'm particularly interested in catching to find the running name in the .vmx file under 'displayname'.
Can you download the file in $mismatch.fullname via the Web client?
Download the .vmx via web client - ERROR - this page is not working right now.
Download another fie to test the browser can download files - SUCCESS - I can download a .log file, but not the .vmx file.
Log directly into one of the esxi hosts via putty - browse to the .vmx fie location.
cat unregistered_IOMEGA1.vmx
cat: can't open 'unregistered_IOMEGA1.vmx': Device or resource busy
[root@extreme:/vmfs/volumes/5feba04b-9a0e67e4-7b48-000c29de29f0/unregistered_IOMEGA1
vi unregistered_IOMEGA1.vmx
(file comes up blank in the editor)
cp unregistered_IOMEGA1.vmx Test_unregistered_IOMEGA1.vmx
cp: can't open 'unregistered_IOMEGA1.vmx': Device or resource busy
[root@extreme:/vmfs/volumes/5feba04b-9a0e67e4-7b48-000c29de29f0/unregistered_IOMEGA1
Do you use an account to connect to the vCenter that has the required privileges (role) to access the file?
I have used administrator@vsphere.local for all of the tests above.
Important Note - In the example above this is working when I use the script, however I have tried all the above tests with a VM which isn't working and I see the exact same behavior. It appears this is working as designed for any running VM, the .vmx file is protected from being downloaded, modified, or copied, however in most cases the script is able to copy it to my $tempfile (if anything, I don't understand why this is working).
This seems to be more of a VMware questions than a scripting question, but I'm curious if you have an insights.
Original Message:
Sent: Dec 25, 2024 02:44 PM
From: LucD
Subject: Create a list of all VMs with mismatched names
Can you download the file in $mismatch.fullname via the Web client?
Do you use an account to connect to the vCenter that has the required privileges (role) to access the file?
I think you misplaced the Catch block in your code.
It is now sitting inside the the Try block, but it should follow that Try block.
Something like this
$mismatches = Import-Csv -Path C:\output\UnregisteredVms4.csv$tempFile = New-TemporaryFileForEach ($mismatch in $mismatches) { Write-Host "Looking at $($mismatch.fullname)" try { Copy-DatastoreItem -Item $mismatch.fullname -Destination $tempFile -ErrorAction:Stop | Out-Null $VCVMName = Get-VM -Name (Get-Content -Path $tempFile | where { $_ -match 'displayName' }).Split('"')[1] $VCname = (Get-Content -Path $tempFile | where { $_ -match 'displayName' }).Split('"')[1] $RegisteredVCenter = $VCVMName.Uid.Substring($VCVMName.Uid.IndexOf('@') + 1).Split(":")[0] $DSVMName = $mismatch.name $DSpath = $mismatch.datastorefullpath $result = "$VCVMName was found on vCenter $RegisteredVCenter however it is named $DSVMName on disk." } catch { $error[0] $result = "ERROR" } Write-Host "$result"}
------------------------------
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Original Message:
Sent: Dec 24, 2024 11:33 AM
From: dbutch1976
Subject: Create a list of all VMs with mismatched names
Hello,
I have created a list of VMs which I suspect are running under different names. I have created a simple script to import that list, then copy the .vmx file to a temp file in order to find the 'displayname' which is the actual name the VM is running under. This works in some cases, but in other cases I'm getting this error:
Download of file 'https://MYVCETNER.com/folder/lun01&enc=std' failed. Error message: Response status code does not indicate success: 500 (Internal Server Error).
Even though I have confirmed that the working VMs and non-working VMs are online and running I cannot find any reason that I get this error in some cases and others I don't.
While I investigate why it's not working I'd like to add a catch which will simply add "ERROR" to the results variable so that I can flag which results I need to follow up on, but the logic for catch statements is eluding me. If there's an error I want to it be marked as an error, if not I just want the script to continue on:
$mismatches = Import-Csv -Path C:\output\UnregisteredVms4.csv$tempFile = New-TemporaryFileForEach ($mismatch in $mismatches) { try { Copy-DatastoreItem -Item $mismatch.fullname -Destination $tempFile -ErrorAction:Stop | Out-Null } catch { $result = "ERROR" } $VCVMName = Get-VM -Name (Get-Content -Path $tempFile | where{$_ -match 'displayName'}).Split('"')[1] $VCname = (Get-Content -Path $tempFile | where{$_ -match 'displayName'}).Split('"')[1] $RegisteredVCenter = $VCVMName.Uid.Substring($VCVMName.Uid.IndexOf('@')+1).Split(":")[0] $DSVMName = $mismatch.name $DSpath = $mismatch.datastorefullpath $result = "$VCVMName was found on vCenter $RegisteredVCenter however it is named $DSVMName on disk." }write-host "$result"Remove-Item -Path $tempFile$result