There are many reasons for taking a screenshot of your local desktop or a remote user’s desktop. Sometimes we have some task automated but would like to see the end result, but not only as a blob of text.
This is especially true to those of us who have worked in a help desk role know the difficulty involved with having an end user describe an error that’s occurred on their machine.
Wouldn’t it be great if they knew how to take a screenshot and send the file to you? Sometimes we just want a picture to look at. Pictures really are worth a thousand words sometimes.
In order to do this, we’re going to leverage .NET for a few things. We’re going to need to create a canvas to save our image data to. We’re going to need to capture our image data. We’re going to need to save that image data to our canvas and then save it to a file. How exciting!
Taking a screenshot with PowerShell and .NET
This script can be broken down into the following steps:
Get screen information to determine screen capture size
Create a bitmap object to save our screen capture to
Create a graphics object to perform the screen capture
Save our screen capture to file
Getting total screen width and height
.NET info – System.Windows.Forms.SystemInformation.VirtualScreen (link)
In this step, we’re using the VirtualScreen property to get width, height and the top-left coordinates. We will use these to configure the area of the screen that we wish to copy.
Creating a bitmap object
.NET info – System.Drawing.Bitmap (link)
We need to create a bitmap object to store our image. We can do that a number of ways thanks to the many constructors of System.Drawing.Bitmap. One of the simplest ways is to simply specify the width and height (in pixels) for the bitmap that you wish to create:
Creating a graphics object
The System.Drawing.Graphics class is used as a drawing surface to draw on the bitmap object. This is what we’ll be using to perform a screen capture. For our purposes, we will create the graphics object by using the previously-created bitmap object. This will create the graphics object with the same width and height as the bitmap.
Performing a screen capture
.NET info – System.Drawing.Graphics.CopyFromScreen (link)
Using the Graphics object we previously created, we’ll use one of the methods provided, CopyFromScreen, in order to capture an area of the screen. We’ll set the the coordinates to copy to and from (source and destination, respectively). We’ll also set the size of the area that we wish to copy.
Saving to file
.NET info – System.Drawing.Bitmap.Save (link)
This is the simplest way to save the bitmap to a file.
You can compress and save this bitmap in other formats, but that is out of the scope of this blog post. Here’s a link if you’re interested in an MSDN example for saving as jpeg with several quality levels – link.
Putting it all together
That’s it! Like magic, you now have a screenshot of your local desktop.
But, wait! Didn’t I mention taking a screenshot of remote users?!
Yep. This is where PDQ Deploy comes in.
Remote screenshots with PDQ Deploy
Since PDQ Deploy can deploy just about any software, script or patch to a machine (Yes. It’s that awesome), we’re going to utilize that for taking a screenshot of a remote user desktop.
This is a Pro license feature, so if you’re using Free Mode, you should definitely sign up for a free trial and test this out for yourself.
Here is the general outline of what needs to be done
Open/create new package
Install File : Point to your Powershell script (.ps1 file)
Parameters : -Path “\\Path\ForYour\Screenshots”
Logged On State : Only run if a user is logged on
Run As : Logged on User
Here’s an animation of all the steps
For this package, I have modified the script slightly. I have added a Path parameter to allow us to change the Path for any given Install Step. I have also modified the FileName to be a combination of the computer name with a time stamp (Get-Date).
You should be able to deploy that to any machine that has a user currently logged on. It will save the screenshots to the Path that you specify in your package. It makes troubleshooting a breeze!
There are a few things to keep in mind when using this script with remote users.
The script will fail if somebody isn’t logged into the machine or if the machine is locked.
If you are running this script with PDQ Deploy in the context of a logged on user, the logged on user will need write permissions to the Path that you specify in the Install Step, otherwise they will not be able to save the screenshot to the target Path.
The Path you specify needs to exist for this to work. You can add logic into your script to check for the path or to force its creation. For example, to force the directory to be created you can do something like this:
-ItemType Directory -Force -Path $Path
Other than that, good luck with your screenshots!
Kris was an employee at PDQ.