Share via


Enter-PSSession : Invalid URI: The hostname could not be parsed

Question

Thursday, December 12, 2019 8:42 AM

Hi,

Im trying to write a script to get the mac address of a specific VM located in a cluster managed by VMM. The script will be running from management server and not hyper-v host in the cluster (is this better approach? do you usually do that?)

My plan is to first find what HV host hosting the vm , ps session to that server.  and then run get-vmnetworkadapter

$VMName = Read-Host -Prompt 'Enter VM Name'
$HostName = Get-VM -VMMServer VMM01.working.local | Where-Object {$_.Name -like $VMname} | Select HostName
Write-Output $HostName
Enter-PSSession -ComputerName "$HostName"
MACAddress= Get-VMNetworkAdapter -VMName $VMName

im getting the below error

Enter-PSSession : Invalid URI: The hostname could not be parsed.

At line:4 char:1

+ Enter-PSSession -ComputerName "$HostName"

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Enter-PSSession], UriFormatException

    + FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.EnterPSSessionCommand

why PSsession not accepting this value?

All replies (11)

Thursday, December 12, 2019 8:50 AM

When you use a "-like" comparison you should use a wildcard like "*" at the end.

Your variable $Hostname contains an object wth the property "HostName". If you need only the pure property you will have to use $HostName.HostName instead of only $HostName.

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Thursday, December 12, 2019 9:09 AM

Please refrain from asking the same questions multiple times.

\(ツ)_/


Thursday, December 12, 2019 9:19 AM

Thank you for the reply.

I changed it now and it worked but strangly i got now totally Another error

$VMName = Read-Host -Prompt 'Enter VM Name'
$HostName = Get-VM -VMMServer VMM01.working.net | Where-Object {$_.Name -like $VMname} | Select HostName
Write-Output $HostName
Enter-PSSession -ComputerName $HostName.HostName
$MACAddress= Get-VMNetworkAdapter -VMName $VMName
Write-Output $MACAddress

Enter VM Name: VMNAME-Prod01

HostName                 

                 

HV02.working.net

Get-VMNetworkAdapter : The Hyper-V Management Tools could not access an expected WMI class on computer 'AdminServer'. This may indicate that the Hyper-V Platform is not

 installed on the computer or that the version of the Hyper-V Platform is incompatible with these management tools.

At D:\Scripts\CreateVM.ps1:5 char:14

+ $MACAddress= Get-VMNetworkAdapter -VMName $VMName

+              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-VMNetworkAdapter], VirtualizationException

    + FullyQualifiedErrorId : Unspecified,Microsoft.HyperV.PowerShell.Commands.GetVMNetworkAdapter "

AdminServer is the management server that im trying to run the script from.

I checked the get-vm paramenters and i couldnt see VMMServer but it was working Before and now its not. and im getting back the right value where the VM is hosted. Anything im missing?


Thursday, December 12, 2019 9:29 AM

The VM CmdLets are not installed on a VM. They are only installed locally. You cannot use Enter-PsSession with these CmdLets.

\(ツ)_/


Thursday, December 12, 2019 9:32 AM

Please carefully read the error message:

"the Hyper-V Platform is not installed on the computer or that the version of the Hyper-V Platform is incompatible with these management tools."

\(ツ)_/


Thursday, December 12, 2019 10:37 AM

Strange. The VM console is installed on the management server as well as Hyper-V.

Am i approaching this in the wrong way?

I really appreciate any guidance.


Thursday, December 12, 2019 10:42 AM

But the VM is on a different server.     To get a local VM just use:

Get-VM vmname

\(ツ)_/


Thursday, December 12, 2019 11:04 AM

I have failover cluster which consist of several VMs. I will need to get the MAC address for a different VM everytime. The VM might be located on a different Host. thats why i thought to get which Host the VM is located in. pssession to this host. and then run get-vmnetadapter.


Thursday, December 12, 2019 11:10 AM

Remoting is not required and you cannot normally remotely connect to you local system.

Two things are needed.  Learn PowerShell and learn how to use Hyper-V and the CmdLets.  There are sites with instructions and many blogs that have fundamental instructions on how to manage Hyper-V via PowerShell.  You can't do this with guesswork.  It requires learning the technology unil you understand how it is designed to work.

\(ツ)_/


Thursday, December 12, 2019 12:04 PM

Thank you again for your help.

as it seems that the default snapin was used was for VMM. as you can see in the error message. i forced the script to run into hyperv module and it started to work.


Thursday, December 12, 2019 12:17 PM

So the error message was correct. You weren't us9injg the correct module for Hyper-V.

Mixed VMWare and Hyper-V is an issue that most don't understand.  Yu can be specific by specifying the module before the CmdLet.

ModuleName:Get-VM ...

This allows your script to be specific in a mixed environment and makes it safe from errors.

\(ツ)_/