none
How to get the Powershell environment variables value into C++ program RRS feed

  • Question

  • wstring getVM = L"powershell -Command \"& {$TotalVM = Get-VM; $TotalVMCount = $TotalVM.count;  echo $TotalVMsCount > VmCount.txt}\" ";
    _wsystem(getVM.c_str());

    The above 2 lines will work in a C++ code and the contents of VmCount.txt will have the VmCount. Now to get the VmCount into my C++ program I have to open the file do a getline() and then convert the string to int. Can I somehow get the value TotalVM.count straigt into my C++ without having to write to a file, open it, read it etc... 

    Something like int VmCount = $TotalVM.count???

    thanks

    ananda

    • Moved by jrv Wednesday, October 2, 2019 12:18 AM Correct forum
    Wednesday, October 2, 2019 12:13 AM

Answers

  • Yes, but it's a lot more trouble than what you're doing.

    What you are asking for is a "pipe".  You can use popen to run that command, which will return a pipe handle that you can read as a file.  Check the documentation for popen.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, October 2, 2019 4:22 AM

All replies

  • Sorry but this is not a C++ forum.


    \_(ツ)_/

    Wednesday, October 2, 2019 12:17 AM
  • wstring getVM = L"powershell -Command \"& {$TotalVM = Get-VM; $TotalVMCount = $TotalVM.count;  echo $TotalVMsCount > VmCount.txt}\" ";
    _wsystem(getVM.c_str());

    The above 2 lines will work in a C++ code and the contents of VmCount.txt will have the VmCount. Question is how to get the VmCount into my C++ program I have to open the file do a getline() and then convert the string to int. Can I somehow get the value TotalVM.count straigt into my C++ without having to write to a file, open it, read it etc... 

    Something like int VmCount = $TotalVM.count???. Help will be much appreciated.

    thanks

    ananda

    Wednesday, October 2, 2019 1:29 AM
  • But it says:

     , 
     > 

    I reposted it too and it went to back to this forum only ... Did you mean I should post this in a Powershell forum?

    Wednesday, October 2, 2019 1:37 AM
  • I moved your post6 to the VC++ forum. You are now in the correct forum. They will help you understand why you cannot get anyting returned from another process and how to use the PowerShell SDK to run PS scripts from VC++ applications.

    Here is a starter to get you going: https://docs.microsoft.com/en-us/powershell/developer/hosting/writing-a-windows-powershell-host-application


    \_(ツ)_/

    Wednesday, October 2, 2019 1:54 AM
  • Yes, but it's a lot more trouble than what you're doing.

    What you are asking for is a "pipe".  You can use popen to run that command, which will return a pipe handle that you can read as a file.  Check the documentation for popen.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, October 2, 2019 4:22 AM
  • Yes - you can't pass output back to a variable but you can capture the output stream.  The output will only be text.  You cannot pass objects.  You can pass XML or Json representations of objects as these are just text,

    See the following for examples: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/popen-wpopen?view=vs-2019


    \_(ツ)_/

    Wednesday, October 2, 2019 4:43 AM
  • I can also point out that he title seems to say you want PowerShell environment variables. PowerShell does not have its own environment variables. It uses the users session variables which are available to any program running. The C++ program variables will be the same as those that PowerShell uses.

    If you are asking how to get PS variables into a C++ program then you will need to output a text representation of those variables and use popen to capture the output.

    It will be much easier to run the PS script suing the SDK hosting method which does let you read the PS session variables and to get back output directly to a variable as well as returning full objects.


    \_(ツ)_/

    Wednesday, October 2, 2019 5:51 AM
  • Fantastic this is very simple and elegant and saved me a ton of work. Here is the working program. I took the example provided my MSFT and added my code. It worked like charm. Tim 1001 thanks for your help. 

    All the others who gave me helpful tips thanks the same. For now I will go with the popen()

    ananda

    // crt_popen.c
    /* This program uses _popen and _pclose to receive a
    * stream of text from a system process.
    */

    #include <stdio.h>
    #include <stdlib.h>

    int main( void )
    {

       char   psBuffer[128];
       FILE   *pPipe;

            /* Run DIR so that it writes its output to a pipe. Open this
             * pipe with read text attribute so that we can read it
             * like a text file.
             */

       if( (pPipe = _wpopen(L"powershell -Command \"& {$TotalVM = Get-VM; $TotalVMCount = $TotalVM.count;  echo $TotalVMCount}\" ",  L"rt" )) == NULL )
          exit( 1 );

       /* Read pipe until end of file, or an error occurs. */

       while(fgets(psBuffer, 128, pPipe))
       {
          printf("VmCount = %s\n", psBuffer);
       }

       /* Close pipe and print return value of pPipe. */
       if (feof( pPipe))
       {
         printf( "\nProcess returned %d\n", _pclose( pPipe ) );
       }
       else
       {
         printf( "Error: Failed to read the pipe to the end.\n");
       }
    }

    Wednesday, October 2, 2019 4:37 PM