You could try running something like this in a PowerCLI session connected to your vCenter:
foreach ($VM in Get-VM)
{
try { $VMName = $VM | Get-AdvancedSetting -Name "GuestInfo.VMName" }
catch { $VMName = "" }
if ($VMName) { $VMName | Set-AdvancedSetting -Value "$VM.Name" -Confirm:$false }
else { $VM | New-AdvancedSetting -Name "GuestInfo.VMName" -Value "$VM.Name" -Confirm:$false }
}
I haven't been able to try this on an actual system yet, but hope that this would create or set an Advanced Setting called "GuestInfo.VMName" to be the name of each VM used in vCenter for each VM respectively.
Then with PowerShell inside the guest you can use something like:
$VMName = $(& 'C:\Program Files\VMware\VMware Tools\vmtoolsd.exe' --cmd "info-get guestinfo.VMName" | Out-String) -replace "`t|`n|`r",""
If $VMName comes back as empty string, it may be because the GuestInfo.VMName key/variable does not exist. You can check the variable $LASTEXITCODE, which stores the exit code of the last run command. If this is "1" then the variable didn't exist, if it is "0", then the variable did exist and the value in $VMName is valid.
This should set $VMName to the value of GuestInfo.VMName, which will for all your VMs be the name of the VM known in vCenter.
You would have to remember to re-run the first script if anything is done in vSphere that could alter the name of a VM, or a VM is re-deployed or replaced in some way.