Welcome › Forums › General PowerShell Q&A › Hyper-v VM’s hostname in Registry is no longer accurate
- This topic has 3 replies, 2 voices, and was last updated 1 month, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
November 23, 2020 at 7:06 pm #273591
On my Windows 10 Pro Hyper-V, I have a Server 2019 VM whose hostname is the default WIN-MUMBOJUMBO. On searching the Intertubes for a possible solution to find this hostname ‘scriptually’, I came across this solution, which unfortunately no longer works:
(Get-Item “HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters”).GetValue(“Hostname”)
When I run the above line, I get a different value for ‘Hostname’ than the WIN-MUMB…
So, is there another way to determine PowerShell scriptually the hostname of the VM(s). I need this so that I can then run Configurations on the different VMs I intend to create on Hyper-V.
Any help is appreciated.
-
November 24, 2020 at 9:11 am #273693PowerShell12# Get remote vm name from environment variables listInvoke-Command -ComputerName "WIN-MUMBOJUMBO" -ScriptBlock {$env:COMPUTERNAME}
-
November 24, 2020 at 12:03 pm #273783
Thanks for the reply.
Your solution will work if I am able to know WIN-MUMBO… ahead of time. For example, I have a farm of new VMs I have deployed whose default “WIN-Mumbo..” default computernames I don’t know yet. I want to be able to get these default computernames so that I can do what you suggested to change their names. Conceptually this is what I am looking for:
$oldComputerName = Some-FunctionToGetVMsComputerName
$oldComputerName | Rename-Computer -NewName "Kangaroo"
Hope this helps clarify my request. Thank you.
-
November 24, 2020 at 12:46 pm #273798
Well I think I may have found the solution after all.
After reading ‘random commandline’s suggestion on Invoke-Command, I looked it up and apparently there is a VMName parameter for Invoke-Command, so I was able to accomplish what I wanted:
Invoke-Command -VMname $vmName -ScriptBlock { Rename-Computer -NewName 'Kangaroo' -Restart }
Thank you.
-
-
AuthorPosts
- You must be logged in to reply to this topic.