Jenkins + PowerShell Script that takes screenshot of a remote folder

759 views
Skip to first unread message

VikS

unread,
Mar 27, 2017, 1:37:30 AM3/27/17
to Jenkins Users
We have a build which requires us to replace files to a newer version on remote computers and the requirement is the script needs to take screenshot of the folder for validation.
We have built our ps script and it does what is needed when executed outisde of Jenkins but not when scheduled with Jenkins, here is the screenshot portion of the script:
function ScreenshotFolder
{

# Get the list of servers
$servers = Get-Content "C:\tmp\script\updated\computers.txt"

  # Foreach loop to go to each server and take the screenshot
  foreach ($server in $servers)
  {
   
    # Invoke the path of Application
    $FolderWindows = Start "\\$server\C$\Temp\Application" -WindowStyle Maximized

    # Pause the script for 2 seconds so that the invoked path Windows will stay for 2 seconds before screencapture
    Start-Sleep -s 2

    # Provide the unique name for the .bmp file
    $File = join-path -path "C:\tmp\script\updated\Logs" -childpath $($server + "-screenshot" + ".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
   
    # Create bitmap using the top-left and bottom-right bounds
    $bitmap = New-Object System.Drawing.Bitmap $Width, $Height
   
    # Create Graphics object
    $graphic = [System.Drawing.Graphics]::FromImage($bitmap)
   
    # Capture screen
    $graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)
   
    # Save to file
    $bitmap.Save($File)
    Write-Output "Screenshot saved to:"
    Write-Output $File

    # Close each folder windows so that it would capture each windows at a time
    $CloseWindows = (New-Object -comObject Shell.Application).Windows() |? { $_.FullName -ne $null} | ? { $_.FullName.toLower().Endswith('\explorer.exe') }
    $CloseWindows | % {  $_.Quit()}

}

}


Jenkins service us configured to be running using service account which has access to the remote servers. Console output:
Start-Process : This command cannot be run due to the error: Server execution
failed.At
C:\Users\ctoCITRIX4win09\AppData\Local\Temp\hudson7511677238111411019.ps1:385
char:22
+     $FolderWindows = Start "\\$server\C$\Temp\Application" -WindowStyle Maximized
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOp
   erationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
   ommands.StartProcessCommand

Any suggestions to achieve this screenshot function using script on Jenkins? We're ok to explore other tools as well.

Slide

unread,
Mar 27, 2017, 9:32:25 AM3/27/17
to Jenkins Users

Windows services can't interact with the desktop. This feature went away in Vista. You would need to run the agent as a non-service for the screenshot to work.


--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/d2a0f96b-c413-49ef-b588-41693407fb35%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

VikS

unread,
Mar 29, 2017, 11:45:09 PM3/29/17
to Jenkins Users
Got it and I have setup a Jenkins slave which is not running as service. I have tried multiple modification to my script but the screenshot does not work. In summary, this is what I'm trying to do with powerrshell: Jenkins Slave -> launch the remote path of the folder that needs to be screen captured -> take a screenshot and save it

Script says that the path is launched successfully but for screenshot, this will be the error:
Exception calling "CopyFromScreen" with "5" argument(s): "The handle is
invalid"
At C:\WINDOWS\TEMP\hudson1670262990811364138.ps1:30 char:5
+     $graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : Win32Exception

Does a screen capture of a remote path still be possible on a slave without users logged on?
Reply all
Reply to author
Forward
0 new messages