powershell - Get remote registry value - Stack Overflow<\/a><\/p>","upvoteCount":1,"datePublished":"2018-05-08T15:11:50.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/2","author":{"@type":"Person","name":"alex3031","url":"https://community.spiceworks.com/u/alex3031"}},{"@type":"Answer","text":"Try one of those:<\/p>\n
$ComputerName = 'core'\n\n# Remote Reqistry\n\n([Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$ComputerName)).OpenSubKey('SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters').GetValue('PhysicalHostName')\n\n# WMI\n\n(([WMIClass]\"\\\\$ComputerName\\ROOT\\DEFAULT:StdRegProv\").GetStringValue(2147483650,'SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters','PhysicalHostName')).sValue\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-05-08T15:11:53.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Thank you for the reply however I am finding it hard to understand any of that as I am new to Powershell.<\/p>\n
I understand that basically I need to a remote version of the command below, however have no idea how to make a prompt appear to enter in the computer name and then to generate that command remotely to that computer I specified.<\/p>\n
(get-item “HKLM:\\SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters”).GetValue(“HostName”)<\/p>","upvoteCount":0,"datePublished":"2018-05-08T15:17:42.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/4","author":{"@type":"Person","name":"jamesgrier","url":"https://community.spiceworks.com/u/jamesgrier"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Jimmmz:<\/div>\n
\nThank you for the reply however I am finding it hard to understand any of that as I am new to Powershell.<\/p>\n
I understand that basically I need to a remote version of the command below, however have no idea how to make a prompt appear to enter in the computer name and then to generate that command remotely to that computer I specified.<\/p>\n
(get-item “HKLM:\\SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters”).GetValue(“HostName”)<\/p>\n<\/blockquote>\n<\/aside>\n
What have you tried so far…?<\/p>\n
$vminfo = read-host \"Please enter the VM Name\"\n$hostinfo = ([Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine',$vminfo)).OpenSubKey('SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters').GetValue('PhysicalHostName')\nwrite-output \" VM '$computername' is hosted on '$hostinfo'\"\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-05-08T15:21:49.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"I attempted the above script and when it prompts for computer name I type in the VM Name and get the following error.<\/p>\n
Exception calling “OpenRemoteBaseKey” with “2” argument(s): \"The network path was not found.<\/em><\/p>\n\"<\/em><\/p>\nAt line:2 char:1<\/em><\/p>\n+ $hostinfo = ([Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’,$vm …<\/em><\/p>\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/em><\/p>\n+ CategoryInfo : NotSpecified: ( [], MethodInvocationException<\/em><\/p>\n+ FullyQualifiedErrorId : IOException<\/em><\/p>\nVM ‘’ is hosted on ‘’<\/em><\/p>","upvoteCount":0,"datePublished":"2018-05-08T15:38:40.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/6","author":{"@type":"Person","name":"jamesgrier","url":"https://community.spiceworks.com/u/jamesgrier"}},{"@type":"Answer","text":"Is the Remote Registry service started on the VM?<\/p>","upvoteCount":1,"datePublished":"2018-05-08T15:41:30.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/7","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
That was the problem! Thanks.<\/p>\n
The last part doesn’t seem to work correctly where it displays<\/p>\n
VM ‘’ is hosted on ’ as it only displays the Host and not the VM for example, VM ‘’ is hosted on ‘Host1’<\/p>\n
It doesn’t seem to be finding the ‘$computername’ command but the ‘$hostinfo’\" works<\/p>\n
Also once I have saved the .PS1 and right click and run with powershell I enter in the computer name and it just closes, it only works if I open up Powershell as admin and paste in he whole command.<\/p>","upvoteCount":0,"datePublished":"2018-05-08T16:03:24.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/8","author":{"@type":"Person","name":"jamesgrier","url":"https://community.spiceworks.com/u/jamesgrier"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Jimmmz:<\/div>\n
\nThat was the problem! Thanks.<\/p>\n
The last part doesn’t seem to work correctly where it displays<\/p>\n
VM ‘’ is hosted on ’ as it only displays the Host and not the VM for example, VM ‘’ is hosted on ‘Host1’<\/p>\n
It doesn’t seem to be finding the ‘$computername’ command but the ‘$hostinfo’\" works<\/p>\n<\/blockquote>\n<\/aside>\n
Right, I changed the name to $vminfo.<\/p>\n
write-output \" VM '$vminfo' is hosted on '$hostinfo'\"\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-05-08T16:05:34.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/9","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"change $computername to $vminfo $computername iis an empty variable that’s why it isn’t working.<\/p>","upvoteCount":1,"datePublished":"2018-05-08T16:06:08.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/10","author":{"@type":"Person","name":"alex3031","url":"https://community.spiceworks.com/u/alex3031"}},{"@type":"Answer","text":"
Thanks again, works like a charm now…<\/p>\n
Just running the .ps1 file now when I right click and select run with powershell I enter in the computer name and it just closes, it only works if I open up Powershell as admin and paste in the whole command, is there anything I need to enable to run this easier?<\/p>","upvoteCount":0,"datePublished":"2018-05-08T16:13:57.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/11","author":{"@type":"Person","name":"jamesgrier","url":"https://community.spiceworks.com/u/jamesgrier"}},{"@type":"Answer","text":"
You can append a ‘pause’ to the last line of your script, that will keep the window open.<\/p>","upvoteCount":0,"datePublished":"2018-05-08T16:17:38.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/12","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Is there anyway to run it as Administrator automatically?<\/p>\n
I added a ‘Pause’ command on the last line but it still seems to instantly close down.<\/p>","upvoteCount":0,"datePublished":"2018-05-08T17:14:46.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/13","author":{"@type":"Person","name":"jamesgrier","url":"https://community.spiceworks.com/u/jamesgrier"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Jimmmz:<\/div>\n
\nIs there anyway to run it as Administrator automatically?<\/p>\n
I added a ‘Pause’ command on the last line but it still seems to instantly close down.<\/p>\n<\/blockquote>\n<\/aside>\n
Could call the run as app in powershell and call powershell passing it the command.<\/p>","upvoteCount":1,"datePublished":"2018-05-08T17:22:04.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/14","author":{"@type":"Person","name":"alex3031","url":"https://community.spiceworks.com/u/alex3031"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Jimmmz:<\/div>\n
\nI added a ‘Pause’ command on the last line but it still seems to instantly close down.<\/p>\n<\/blockquote>\n<\/aside>\n
Can you show what you actually have?<\/p>\n\n\n
<\/div>\n
Jimmmz:<\/div>\n
\nIs there anyway to run it as Administrator automatically?<\/p>\n<\/blockquote>\n<\/aside>\n
Something like this:<\/p>\n
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command \"& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File \"\"PS_Script_Path&Name.ps1\"\"' -Verb RunAs}\"\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-05-08T17:34:12.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/15","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"So the full script should be this?<\/p>\n
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command “& {Start-Process PowerShell -ArgumentList ‘-NoProfile -ExecutionPolicy Unrestricted -File \"“PS_Script_Path&Name.ps1\"”’ -Verb RunAs}”<\/p>\n
$vminfo = read-host “Please enter the VM Name”<\/p>\n
$hostinfo = ([Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’,$vminfo)).OpenSubKey(‘SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters’).GetValue(‘PhysicalHostName’)<\/p>\n
write-output \" VM ‘$vminfo’ is hosted on ‘$hostinfo’\"<\/p>\n
‘Pause’<\/p>","upvoteCount":0,"datePublished":"2018-05-08T18:03:27.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/16","author":{"@type":"Person","name":"jamesgrier","url":"https://community.spiceworks.com/u/jamesgrier"}},{"@type":"Answer","text":"
If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n \n
<\/div>\n
\n
PLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> Programming & Development<\/span><\/span><\/a>\n <\/div>\n \n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n <\/p>","upvoteCount":0,"datePublished":"2018-05-08T18:06:55.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/17","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Well, we’re here to help, not do it for you.<\/p>\n
The pause statement does not work as you have it in quotes. powershell takes quotes literal.<\/p>\n
Why don’t you try it and let us know when you get stuck.<\/p>","upvoteCount":0,"datePublished":"2018-05-08T18:14:56.000Z","url":"https://community.spiceworks.com/t/find-host-of-hyper-v-vm/650373/18","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}}]}}
Neally
(Neally)
May 8, 2018, 6:06pm
17
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don't say "Need help" or "PowerShell Help", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…