By Bobby Jefferson on Tuesday, 24 September 2024
Category: Tech News

PowerShell Screen Captures: How To Automate Screenshots in Your Scripts

As a PowerShell enthusiast, I rely on scripts to automate everything from routine maintenance to complex management tasks in my IT environment. Last week, I noticed that one of my scripts didn’t perform as expected, and tracking down the issue proved tricky because the script runs automatically in the middle of the night.

Determined to get to the bottom of the problem, I decided to add a new feature to the script: automated screen captures. By capturing screenshots at strategic points during the script’s execution, I could review them later to pinpoint what went wrong.

At first, I was unsure if PowerShell could create screen captures, but after some research, I discovered it is relatively straightforward.

Here is the code that I used:

Add-Type -AssemblyName System.Drawing Add-Type -AssemblyName System.Windows.Forms $OutputPath = "C:\Temp\PowerShellScreenCapture.jpg" # Get the screen resolution $ScreenResolution = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds # Create a bitmap with the same resolution as the screen $Bitmap = New-Object System.Drawing.Bitmap $ScreenResolution.Width, $ScreenResolution.Height # Create a graphics object from the bitmap $Graphics = [System.Drawing.Graphics]::FromImage($Bitmap) # Copy the screen to the bitmap $Graphics.CopyFromScreen($ScreenResolution.Location, [System.Drawing.Point]::Empty, $ScreenResolution.Size) # Save the screen capture $Bitmap.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Jpeg) # Clean Up $Graphics.Dispose() $Bitmap.Dispose()

How the Script Works

The first two lines of code add the required assemblies. The System.Drawing assembly works with graphical elements, such as colors or fonts. The System.Drawing assembly includes a class called Bitmap. The Bitmap class defines an image based on pixel data. Better still, you can use a Bitmap to create and manipulate an image within the system’s memory, which is what we need to do to capture a screenshot.

The second assembly, System.Windows.Forms, is generally used to create graphical user interfaces. However, in this script, it is used to determine the machine’s screen resolution.

Related:How To Use Automatic PowerShell Transcription

Next, the script creates an OutputPath variable to specify the location for saving the screen capture file. In this case, I am generating a file named C:\Temp\PowerShellScreenCapture.jpg, but you can modify the path and filename as needed.

The script then determines the system’s display resolution. There are several ways of doing this, but this script references the PrimaryScreen.Bounds class within System.Windows.Forms.Screen. This class provides the screen resolution, which gets stored in a variable named $ScreenResolution.

It’s important to note that the command retrieves the resolution of the primary screen. You can modify the script to work in multi-monitor setups, and I may write a version for that if there is enough interest.

After establishing the screen resolution, the script creates a bitmap. This bitmap initially exists as a bitmap object in the system’s memory, with pixel dimensions matching the PC’s screen resolution.

Next, the script creates a graphics object from the bitmap and copies the screen’s contents to this graphics object. The script stores the graphics object in a variable called $Graphics. The script calls a System.Drawing.Graphics class called CopyFromScreen to capture the screen. This action updates the bitmap with a copy of all the pixels displayed.

Related:Debugging Scripts in PowerShell: Breakpoints and Stepping

With the screen capture stored within the bitmap, the script then exports the bitmap. System.Drawing.Bitmap.Save handles the export process. The script also uses System.Drawing.Imaging.ImageFormat to convert the bitmap to JPEG format before writing it to the output path defined earlier in the script.

Finally, the last two lines of code perform some cleanup by disposing of the bitmap and graphics objects, as they are no longer needed.

About the Author

Sign up for the ITPro Today newsletter

Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

Newsletter Sign-Up

Original link
(Originally posted by Brien Posey)
Leave Comments