I would like to have a screenshot tool in PS. Because I don't want to reinvent the wheel I searched and found a script at github (https://github.com/mikepruett3/psfetch), which I adapted for my needs.
Now I would like to change the behaviour - when the script is started with no parameter it should make a screenshot in the current directory. If the user enters a path (with -Path
) the screenshot should be saved there.
My idea was to define (in my case) $Tarpath
and redefine it when the option is given. How to do this?
Here is my actual script:
# PSFetch.ps1
# A Screenfetch writen in PowerShell
#
# -----------------------------------------------------------
# The Original Inspirations for CMDfetch:
# -----------------------------------------------------------
# screenFetch by KittyKatt
# https://github.com/KittyKatt/screenFetch
# A very nice screenshotting and information tool. For GNU/Linux (Almost all Major Distros Supported) *This has been ported to Windows, link below.*
#
# archey by djmelik
# https://github.com/djmelik/archey
# Another nice screenshotting and information tool. More hardware oriented than screenFetch. For GNU/Linux
# -----------------------------------------------------------
#
# DONE: Function to Take the Screenshot
Function Take-Screenshot {
[CmdletBinding()]
Param(
[string]$Width,
[string]$Height,
[string]$TarPath = "$PSScriptRoot"
)
PROCESS {
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") > $Null
# Changed how $bounds is calculated so that screen shots with multiple monitors that are offset work correctly
$bounds = [Windows.Forms.SystemInformation]::VirtualScreen
# Check Path for Trailing BackSlashes
# $TarPath = $PSScriptRoot
if ( $TarPath.EndsWith("\") ) {
$TarPath = $TarPath.Substring(0,$Path.Length-1)
}
# Define The Target Path
$stamp = get-date -f MM-dd-yyyy_HH_mm_ss
$target = "$TarPath\screenshot-$stamp.png"
# Take the Screenshot
$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($target)
$graphics.Dispose()
$bmp.Dispose()
}
}
# DONE: Fix support for Multiple Monitors
# FROM: Shay Levy's Response - http://stackoverflow.com/questions/7967699/get-screen-resolution-using-wmi-powershell-in-windows-7
$ScreenWidth = 0
$ScreenHeight = 0
Add-Type -AssemblyName System.Windows.Forms
$DisplayCount = [System.Windows.Forms.Screen]::AllScreens.Bounds.Count
$Bounds = [System.Windows.Forms.Screen]::AllScreens | Select-Object -ExpandProperty Bounds
$ScreenWidth = $Bounds | Measure-Object -Property Width -Sum | Select-Object -ExpandProperty Sum
$ScreenHeight = $Bounds | Measure-Object -Property Height -Maximum | Select-Object -ExpandProperty Maximum
$RESOLUTION = "$ScreenWidth x $ScreenHeight"
# Take Screenshot if the Parameters are assigned...
Take-Screenshot -Width $ScreenWidth -Height $ScreenHeight -TarPath $target
edit i forgot to remove the $tarpath int the PROCESS-block. It remained here from my first tests...