whytheq
whytheq

Reputation: 35557

Get-process computername difference between using . or localhost

When I run the following, using the . I get a list of processes:

get-process -ComputerName .

When I use localhost I get the following error:

get-process -ComputerName localhost

get-process : Couldn't connect to remote machine.
At line:1 char:1
+ get-process -ComputerName localhost
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-Process], InvalidOperationException
    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessComm  
   and

I'm using VS Code and running the following version of PS:

enter image description here

What are the possible reasons for this?


If I run the following it comes back with LAPTOP-E####### which is obviously not localhost - would csName usually be set as localhost or am I looking in the wrong part of ComputerInfo ?

Get-ComputerInfo | Select-Object csName

Upvotes: 1

Views: 231

Answers (1)

Ash
Ash

Reputation: 3246

As per the comments you have to enable the Remote Registry Windows service for localhost to work, because it is treated as a remote machine and the underlying .NET method used in the original cmdlet reads the performance counters from the registry. This is not an issue in newer versions of PowerShell because the -ComputerName parameter doesn't exist in the PowerShell Core version of the cmdlet.

For anyone considering enabling the Remote Registry service, please be warned there are a number of security implications to consider when allowing remote access to a computer's registry. A lot of corporate users may find that this service is disabled via GPO. If you need process information from remote machines, it is better to use WinRM via Invoke-Command.

Generally if you want to refer to the local machine in a script it is best to use the PowerShell environment variable $env:COMPUTERNAME.

Upvotes: 1

Related Questions