none
Get-Counter "\Hyper-V VM VID Partition(*)\*" How to make this work as a powershell command RRS feed

  • Question

  • I have spent couple of hours and Googled all over no solution at all. Let me give an working example of a different command

    cmd> powershell -Command "& {Get-VMProcessor *}" "  ==> Works 

    ps> Get-VMProcessor *  ==> Works

    ps> Get-Counter  "\Hyper-V VM VID Partition(*)\*" ==> Works

    cmd> powershell -Command "& {Get-Counter "\Hyper-V VM VID Partition(*)\\*"}" ==> DOES NOT WORK

    I have tried all possible single/double quotes, all kind of escape "\" chars none seem to work. It is so frustrating when logic fails. Please enlighten and help me. Thanks

    Tuesday, October 8, 2019 11:55 PM

Answers

  • Sorry. I ended up with too many slashes. Try this:

    powershell -Command "Get-Counter '\Hyper-V VM VID Partition(*)\*'"

    Note that this has only one ending slash and not two like my original.

     


    \_(ツ)_/

    Wednesday, October 9, 2019 6:00 AM
    Moderator
  • You  still don't need the &(). It serves no useful purpose and no code should ever be written that includes unneeded bits, First rule of good codin.

    Your code in C++ or C# should look like this.

    if ((pPipe = _wpopen(L"powershell -Command \"Get-Counter '\\Hyper-V VM VID Partition(*)\\*'\"", L"rt")) == NULL) exit(1); 
    fgetws(psBuffer, 128, pPipe);
    


    \_(ツ)_/


    Thursday, October 10, 2019 4:12 AM
    Moderator

All replies

  • Bad and unnecessary syntax.

    powershell -Command "Get-Counter '\Hyper-V VM VID Partition(*)\\*'"


    \_(ツ)_/

    Wednesday, October 9, 2019 12:08 AM
    Moderator
  • It fails and I have seen all kind of error messages and what ever I tried did not work. Here is the error msg:

    C:\>powershell -Command "Get-Counter '\Hyper-V VM VID Partition(*)\\*'"
    Get-Counter : The specified object was not found on the computer.
    At line:1 char:1
    + Get-Counter '\Hyper-V VM VID Partition(*)\\*'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
        + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand

    Just to give you some additional data Get-counter does work in powershell

    C:\>powershell
    Windows PowerShell
    Copyright (C) Microsoft Corporation. All rights reserved.

    Try the new cross-platform PowerShell https://aka.ms/pscore6

    PS C:\> Get-Counter  "\Hyper-V VM VID Partition(*)\*"

    Timestamp                  CounterSamples
    ---------                  --------------
    10/8/2019 5:18:06 PM       \\rdt-clx1\hyper-v vm vid partition(lg0)\remote physical pages :
                               0

                               \\rdt-clx1\hyper-v vm vid partition(lg0)\preferred numa node index :
                               1

    Wednesday, October 9, 2019 12:21 AM
  • That is because the counter name is wrong. You need to use a valid counter name.

    Counter names can be found like this:

     Get-Counter -listset processor | select -expand paths


    \_(ツ)_/

    Wednesday, October 9, 2019 12:38 AM
    Moderator
  • The counter is fine because the same command works under powershell. Please check my earlier reply. Let me put it once more:

    PS C:\> Get-Counter  "\Hyper-V VM VID Partition(*)\*"

    Timestamp                  CounterSamples
    ---------                  --------------
    10/8/2019 5:18:06 PM       \\rdt-clx1\hyper-v vm vid partition(lg0)\remote physical pages :
                               0

                               \\rdt-clx1\hyper-v vm vid partition(lg0)\preferred numa node index :
                               1

    All I want is the above command work in a command line using the powershell -Command option. Thanks

    Wednesday, October 9, 2019 5:55 AM
  • Sorry. I ended up with too many slashes. Try this:

    powershell -Command "Get-Counter '\Hyper-V VM VID Partition(*)\*'"

    Note that this has only one ending slash and not two like my original.

     


    \_(ツ)_/

    Wednesday, October 9, 2019 6:00 AM
    Moderator
  • Wow!! It worked :). Thanks a lot. So the main diff in my expts and yours are no & and {} and ' instead of " and no extra \. They say if 1000 monkey jumped on a key board they can recreate Beethoven music. I dont want to go there could you kindly explain the logic behind your syntax and not having the &, {} and " as I used? I had used them based on what people advised in Google. Thanks again you made my day, 
    Wednesday, October 9, 2019 1:14 PM
  • What is your logic behind using that syntax. It is just not the correct syntax for a command in PowerShell.


    \_(ツ)_/

    Wednesday, October 9, 2019 1:56 PM
    Moderator
  • Got it and let me close this with my final working example:

    wchar_t   psBuffer[128];
    FILE   *pPipe;

    if ((pPipe = _wpopen(L"powershell -Command \" &  {Get-Counter '\\Hyper-V VM VID Partition(*)\\*'}\" ", L"rt")) == NULL)
    exit(1);

            fgetws(psBuffer, 128, pPipe)

    Thanks

    ananda

    Thursday, October 10, 2019 3:54 AM
  • You  still don't need the &(). It serves no useful purpose and no code should ever be written that includes unneeded bits, First rule of good codin.

    Your code in C++ or C# should look like this.

    if ((pPipe = _wpopen(L"powershell -Command \"Get-Counter '\\Hyper-V VM VID Partition(*)\\*'\"", L"rt")) == NULL) exit(1); 
    fgetws(psBuffer, 128, pPipe);
    


    \_(ツ)_/


    Thursday, October 10, 2019 4:12 AM
    Moderator
  • Yes and thank you again. There is too much information out in the net and people (like me) blindly copy and dont worry to verify. Your syntax makes perfect sense. Thanks
    Thursday, October 10, 2019 2:56 PM