how to capture print screen screenshots from inside azure VM without being connected to it through RDP?

Mohamad Ayad 0 Reputation points
2024-08-22T12:39:34.5866667+00:00

i have a python code that captures screenshots that i have put inside a VM on azure

when I am connected over RDP, the code works just fine with no errors.

if I close the RDP connection, the code faces below error:

OSError: screen grab failed

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,811 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Mahesh Goud Juvvadi 975 Reputation points Microsoft Vendor
    2024-08-22T14:22:52.04+00:00

    Hi Mohamad Ayad,

    Welcome to the Microsoft Q&A forum.

    Capturing screenshots from an Azure VM when you're not connected through RDP can be challenging because the screen session needs to be active for traditional screenshot methods to work. When you disconnect from RDP.

    Here are a few we suggest alternative approaches you can consider:

    Azure VM Extensions can be used to automate tasks within your VM. You can create a script that captures screenshots and runs on a schedule or in response to specific triggers. 

    Capture Screenshots with Scheduled Tasks you can use scheduled tasks to run your screenshot capture script. However, this might require the VM to have a graphical user interface that can function without an active RDP session. Windows Task Scheduler: Schedule your Python script to run at specific intervals.   

    Remote Execution Tools

    Use remote execution tools to interact with your VM and run screenshot capture scripts. Examples include:

    • Azure Automation: Automate tasks and scripts within your Azure environment.

    Example Using pyautogui (if GUI is active)

    Here’s a basic example of a Python script using pyautogui to take screenshots, but remember, this will only work if the GUI session is active:  

    import pyautogui import time
     
    # Set a delay if needed time.sleep(5)
     
    # Capture screenshot screenshot = pyautogui.screenshot()
     
    # Save the screenshot screenshot.save('screenshot.png')
    
    

    Ensure the VM is configured to keep the GUI active if you intend to use pyautogui.
    If it was helpful, please click "Upvote" on his post to let us know.

    Thank You.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.