locked
Get-VM fails with error code 2, file not found after some time ... RRS feed

  • Question

  • I keep running this command in a big while loop once every 200 ms or so as I want to be notified of any new VM coming online or any VM going offline. After say 10-15 min it fails and GetLastError() reports error code 2 saying file not found. I have couple of questions

    1. Is this the best way to check VM count change?
    2. Is 200ms too small a interval?
    3. I am running this as part of the main program, I can start its own thread and check it at a much slow rate like once in 60 seconds or even couple of min. Would that help?
    4. The main while loop is meant to run for ever, under that scenario is there is better way to check VM count change?
    5. Is there a limit to how many times one can call this command? When it fails if I retry it, would that help?
    6. Can any memory buffer overflow else where in the program can cause this? Not to rule out any other options
    		if( (pPipe = _wpopen(L"powershell -Command \"$TotalVM = Get-VM | Where-Object {$_.State -eq 'Running'}; $TotalVM.count;\" ",  L"rt" )) == NULL ) {
    			wprintf(L"%d: GetVmCount: Get-VM failed to get Running Only VMs. Err = %d\n", __LINE__, GetLastError());
    		}

    Thanks 

     


    Wednesday, May 6, 2020 2:14 PM

Answers

  • Why waste time trying to use PowerShell.  Just call the Net Framework classes for Hyper-V or use the APIs.

    If you do use PowerShell then you will need to look at the PS SDK for examples of how to call PowerShell from compiled code.


    \_(ツ)_/

    Wednesday, May 6, 2020 2:23 PM
  • You can also just subscribe to system events and get notifications when a VM goes online or offline.

    If you are going to try to write compiled code in WIndows you will need to learn SENS as it is the backbone of Windows automation.

    I would recommend posting your real issue in the MSDN  "C" forum.  Ask about the real issue and not about how to use PowerShell.   The experienced Windows programmers there will point you at docs and examples for getting system notifications for events and how to use this to manage Hyper-V.

    The key rule in Windows and modern programming is "Don't use loops to monitor events."  Use any of teh many notification methods to monitor system state.


    \_(ツ)_/

    Wednesday, May 6, 2020 2:31 PM

All replies

  • Why waste time trying to use PowerShell.  Just call the Net Framework classes for Hyper-V or use the APIs.

    If you do use PowerShell then you will need to look at the PS SDK for examples of how to call PowerShell from compiled code.


    \_(ツ)_/

    Wednesday, May 6, 2020 2:23 PM
  • You can also just subscribe to system events and get notifications when a VM goes online or offline.

    If you are going to try to write compiled code in WIndows you will need to learn SENS as it is the backbone of Windows automation.

    I would recommend posting your real issue in the MSDN  "C" forum.  Ask about the real issue and not about how to use PowerShell.   The experienced Windows programmers there will point you at docs and examples for getting system notifications for events and how to use this to manage Hyper-V.

    The key rule in Windows and modern programming is "Don't use loops to monitor events."  Use any of teh many notification methods to monitor system state.


    \_(ツ)_/

    Wednesday, May 6, 2020 2:31 PM
  • Thanks @jrv. I felt I was doing wrong but did not know how much, now I realize. Notifications is the best method then. Thanks and this posting is closed. 
    Wednesday, May 6, 2020 2:44 PM
  • When asking for assistance it is always more effective to ask assistance for accomplishing your task without stating how you think you want to accomplish it.  This will become clearer as you learn morae a bout programming systems.

    When building a compiled program the best place to start is in the forum for the compiled language.

    PowerShell is not a good utility to execute as an external process for many reasons and can be a security issue.  Using PS hosting SDK/API makes this easier and more secure.

    System notifications are available on almost all modern operating systems and are a critical tool for all programmers to master.  Windows is one of the richest environments for notifications at all levels of teh OS and remotely over the network.  SENS is on framework for this.  That can be simplified by WMI and other notification subsystems available from the GUI to the diver/kernel level.

    Learning this greatly simplifies design and coding of almost everything you will do in computing.


    \_(ツ)_/

    Thursday, May 7, 2020 4:11 AM