powershellactive-directoryscreen-capture

How to take a remote screenshot with Powershell


I'm trying to figure out how to take remote screenshots over PowerShell from an administrator's account on the AD server to any computer on the network.

So far I've got the following.

 $ComputerName = '<THECOMPUTER>'

 copy-item "C:\Public\Software\Take-Screenshot.ps1" "\\$ComputerName\C$\"

 Invoke-Command -ComputerName $ComputerName -ScriptBlock {
     powershell -nop -c "C:\Take-Screenshot.ps1"
 } 

Take-Screenshot.ps1 is in from here, but I've added the following to the bottom of the script to actually run the function.

Take-ScreenShot -screen -file C:\s.png -imagetype png 

After the screenshot is taken, I'll copy it back to the host, but the problem is the picture is completely black.

I'm thinking this might be because the powershell is running the program, but there's not session attached to it, so there really is not screen??


Solution

  • So I got this to work but it is a little involved. Works with multiple monitors.

    You will need Screenshot.ps1 on the remote PC, your trigger script and PSExec on local PC (Google).

    # This is Screenshot.ps1
    # Add types and variables
    $File = "C:\Temp\Screenshot1.bmp"
    Add-Type -AssemblyName System.Windows.Forms
    Add-type -AssemblyName System.Drawing
    
    # Gather Screen resolution information
    $Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
    $Width = $Screen.Width
    $Height = $Screen.Height
    $Left = $Screen.Left
    $Top = $Screen.Top
    
    # Set bounds
    $bitmap = New-Object System.Drawing.Bitmap $Width, $Height
    
    # Create Object
    $graphic = [System.Drawing.Graphics]::FromImage($bitmap)
    
    # Capture
    $graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)
    
    # Save
    $bitmap.Save($File)
    

    And then for the trigger script

    #Setup Variables
    $ComputerName = "ComputerName"
    $PSExec = "C:\temp\tools\psexec.exe"
    
    # Captures session details
    $quser = (((query user /server:$ComputerName) -replace '^>', '') -replace '\s{2,}', ',' | ConvertFrom-Csv)
    
    # Takes screenshot of remote PC
    &$PSExec -s -i $quser.ID "\\$ComputerName\" PowerShell -WindowStyle Hidden -File "C:\Temp\screenshot.ps1"