title: PowerShell Screen Capture
body: <p>I have been stuck on this for last day or so and I think its something small that I just can't see. </p>
  
  <p>To summarize the code: Powershell script that will run every 60 seconds to take a screen capture. On Windows Server 2012 this works perfectly fine. While on Windows 7, only first screen shot is taken and then all after are "blank" with just white space the size of dimensions. Any Ideas why this happens? </p>
  
  <p>I also attempted to remove the bigger while loop and do a scheduled task that runs every 5 minutes, but again that doesn't work and all I get is a blank white image. Any thoughts? On windows 7. I am using local Admin user.  </p>
  
  <p>I am using powershell 4 across both windows 7 and windows server 2012. And .Net 4.5</p>
  
  <pre><code>while($true){   
  [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  function screenshot([Drawing.Rectangle]$bounds, $path) {
     $bmp = New-Object Drawing.Bitmap $bounds.width, $bounds.height
     $graphics = [Drawing.Graphics]::FromImage($bmp)
  
     $graphics.CopyFromScreen($bounds.Location, [Drawing.Point]::Empty, $bounds.size)
  
     $bmp.Save($path)
  
     $graphics.Dispose()
     $bmp.Dispose()
  }
  
  #NUC bounds
  $bounds = [Drawing.Rectangle]::FromLTRB(0, -1080, 1920, 1080)
  
  #remote display bounds
  #$bounds = [Drawing.Rectangle]::FromLTRB(0, 0, 1280, 800)
  
  $PC_name=$(Get-WmiObject Win32_Computersystem).name
  
  $dateandtime = Get-Date -Format yyyy-MM-dd-hh-mm-ss
  
  $path_pcname= "C:\Scripts\Screenshots\" + $PC_name + "_screenshot_" + "$dateandtime"+ ".png"
  
  
  screenshot $bounds $path_pcname
  
  $limit = (Get-Date).AddMinutes(-15)
  $path_todelete = "C:\Scripts\Screenshots\"
  
  # Delete files older than the $limit.
  Get-ChildItem -Path $path_todelete -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
  
  Start-Sleep -Seconds 60
  </code></pre>
  
  <p>}</p>
  
  <p>Edit:
  I realized that this line:
      [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  which reads from the GAC, isn't being initiated correctly. Not sure if this helps. </p>