powershell Get-Counter -ComputerName parameter on Windows 7

血红的双手。 提交于 2019-12-11 00:39:45

问题


When I try to use the Get-Counter cmdlet on Windows 7 running as administrator I get the following error.

Get-Counter -computername "$env:ComputerName" '\Memory\Available MBytes'

Get-Counter : Unable to connect to the specified computer or the computer is of
fline.
At line:1 char:12
+ Get-Counter <<<<  -computername "$env:ComputerName" '\Memory\Available MBytes
'
    + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
    + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.Ge
   tCounterCommand

This same command works when I try it on XP 64 as is and also on Windows 7 when I exclude the -computername parameter.

Any idea on how to get this to work on Windows 7 with the computername parameter?

Thanks


回答1:


You can omit the -computername parameter and path the counters directly:

 get-counter "\\$env:computername\Memory\Available MBytes"

and that seems to work.




回答2:


Since Set-Counter does not work with -ComputerName $env:COMPUTERNAME put some logic in your function that does something similar to this:

function Get-ServerCounter {
    param ($Server)

    if ($env:COMPUTERNAME -eq $Server) {
        Get-Counter -Counter '\Memory\Available MBytes'        
    } else {
        Get-Counter -computername $Server -Counter '\Memory\Available MBytes'
    }
}



回答3:


It if unfortunate. Many other cmdlets still allow you to specify the local computername with -Computername, but apparently not Get-Counter. On the other hand, the best practice for performance monitoring is to do it remotely anyway.

Get-Counter -computername (get-content computers.txt) '\Memory\Available MBytes'


来源:https://stackoverflow.com/questions/9336258/powershell-get-counter-computername-parameter-on-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!