Archive

Author Archive

Function: Get-Documentation

March 18, 2011 Leave a comment

Righto – As mentioned here in Get-Screenshot we can easily capture our working screen. Now more than just the novelty of having a .png file filling up a random folder…. think this concept through to automatically documenting a script / build / app install.

Function Get-Documentation ($cmd,$filename="screenshot",$fold="C:\wiki\Get-Documentation",[switch]$PAUSE,[switch]$CLS,[switch]$DISPLAY)
{
	# note use of script block $cmd =  {gf get-screenshot -de}
	# syntax Get-Documentation -cmd {gf get-screenshot -de} -PAUSE -CLS -DISPLAY

	if ($CLS) {clear}
	"`nRunning command ""$cmd""`n" | Write-Host -ForegroundColor Green
	. $cmd

	sleep 1
	if ($PAUSE) {pause}
	if ($display) {get-screenshot $filename -fold $fold  -DISPLAY} else {get-screenshot $filename -fold $fold}
}

Talking through the various param options:
$cmd………………..is the script block to execute
$filename……………name of the file, with a default of “screenshot”, but will also have datestamp appended
$fold…………………folder, default is “C:\wiki\Documentation”
[switch]$PAUSE……wait for user input before continuing
[switch]$CLS……….clear the powershell window before executing the script block
[switch]$DISPLAY….invoke the screengrab (for review purposes)

sample output from running command:
Get-Documentation {gf get-screenshot -de} SampleScreenShot -PAUSE -CLS -DISPLAY
get-screenshot

tbc.

Advertisements
Categories: Powershell

Function: Get-Screenshot

March 17, 2011 1 comment

Function: Get-Screenshot

Just as I was leaving for the day… From across the room came a granade:

I wish there was a way to grab a screenshot from the command line

Somewhere in the back of my mind I have a vague memory of catching this in a mail / post, and true enough, techtalkz.com had the sample I was after. I messed with it a few minutes, and refined it adding my usual variables. Then stripping out the taskbar (thanks to Shay Levi) and getting the screen size dynamically (thanks Keith Hill). The result is the Function below.

Function Get-Screenshot ($filename="PrintScreen",$ext="png",$fold="C:\temp",[switch]$DISPLAY,[switch]$FULL)
{
	$date = Get-Date -Format yyyyMMdd_HHmm
	if (!(Test-Path $fold)) {md $fold}
	$repfile = "$fold\$filename`_$date.$ext"
	
	if ($FULL) 
	{
		$screen = gwmi Win32_DesktopMonitor
		[int32]$height = $screen[0].screenHeight
		[int32]$width = $screen[0].screenWidth
	} else {
		$workarea = [System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
		[int32]$height = [System.Windows.SystemParameters]::FullPrimaryScreenHeight
		[int32]$width = [System.Windows.SystemParameters]::FullPrimaryScreenWidth
	}
			
	[reflection.assembly]::LoadWithPartialName("System.Drawing") > $null
	$Bitmap = New-Object System.Drawing.Bitmap $width,$height
	$Size = New-object System.Drawing.Size $width,$height
	$FromImage = [System.Drawing.Graphics]::FromImage($Bitmap)
	$FromImage.copyfromscreen(0,0,0,0, $Size,([System.Drawing.CopyPixelOperation]::SourceCopy))
	$Bitmap.Save($repfile,([system.drawing.imaging.imageformat]::png)); # the acceptable values: png, jpeg, bmp, gif...
	
	if ($DISPLAY) {
		Invoke-Item $repfile
	} else {
		return "PrintScreen saved to: $repfile"
	}
}

Notes:

  • The $DISPLAY switch is a simple way to flag the output to screen
  • The mix of WMI and .NET is here to show that both can be used
  • There’s a few ideas spawned off from here, tbc

    Categories: Powershell

    Function : Get-Function

    February 24, 2011 Leave a comment

    Here’s a time saver… well a quick function that has saved me a whole herd of time when trying to find a function that has been “forgotten”

    Function Get-Function ($searchterm,[switch]$DEFINITION)
    {
    	$functions = @(Get-Command "*$searchterm*" -CommandType "Function")
    	"`nFunctions matching ""$searchterm""`n" | Write-Host -ForegroundColor Green
    	if (!$DEFINITION) {$functions | %{$_.name}} else {$functions | %{"`n$($_.name)" | Write-Host -f Yellow ;$_.definition}}
    }
    
    sal gf Get-Function
    

    the last line is setting an alias “gf”
    – so let’s try this on something familiar… we had a function searching for apps…

    Command: gf apps
    Output:
    Functions matching “apps”
    Get-Apps
    Get-CoreApps
    Get-InstalledApps
    Set-InfsApps

    Ahh – “installed apps”, let’s try that one again, and get Details this time:

    Command: gf installedapps -de
    Output:
    Functions matching “installedapps”
    Get-InstalledApps
    param (
    $computername=$env:computername,
    $regkey_x64 = “SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”,
    $regkey_x86 = “SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall”,
    [switch]$DISPLAY,
    [switch]$EXCLUDE32,
    [switch]$EXCLUDE64
    )
    $bits_to_search = @()
    if (!$EXCLUDE32){$bits_to_search += “x86”}
    if (!$EXCLUDE64){$bits_to_search += “x64”}

    $apps = @()
    foreach ($bit in $bits_to_search)
    {
    $regkey = Get-Variable “regkey_$bit” | %{$_.value}
    if (Test-Path HKLM:$regkey)
    {
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(“LocalMachine”,$computername)
    $key = $reg.OpenSubKey($regkey)
    $SubKeys = $key.GetSubKeyNames() | %{$key.OpenSubKey($_)}

    foreach ($subkey in $subkeys)
    {
    $app = $subkey | select @{N=”Bit”;E={$bit}},`
    @{N=”Name”;E={$_.getvalue(‘DisplayName’)}},`
    @{N=”Version”;E={$_.GetValue(‘DisplayVersion’)}},`
    @{N=”Publisher”;E={$_.GetValue(‘Publisher’)}}
    if ($app.name) {$apps += $app}
    }
    }
    $key.Close()
    $reg.Close()
    }

    # Remove duplicates, but annoyingly names like of “C++” error when using “-match”
    $unique_apps = @()
    foreach ($app in $apps)
    {
    if (!($unique_apps | ?{$_.Bit -eq $app.bit} | ?{$_.name -eq $app.name} |
    ?{$_.version -eq $app.version})) {$unique_apps += $app}
    }

    return $unique_apps | sort bit,name,version

    Function : Get-InstalledApps

    February 16, 2011 Leave a comment

    So there I was happily using

    gwmi win32_product 

    to get me a list of installed applications, but this is slow, and often we just want a quick verification that an application is installed or not.

    Thanks to powershell.com and their “PowerTip of the Day” I was reminded about hitting the registry for this information…
    So, inspired by their code, I hacked together a quick function to do this.

    Note the removal of duplicates was first done as per the concept here Script of the Day – remove duplicate lines in a CSV file but there were a few entries with special characters in their names, eg “C++” that don’t play nicely with “-match” so this was recoded comparing each entry of the object. This is going to be slower, but still solves the “Get Installed Applications” faster than the WMI route!

    Function Get-InstalledApps 
    {
    	param (
    		$computername=$env:computername,
    		$regkey_x86 = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
    		$regkey_x64 = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
    		[switch]$DISPLAY
    		)
    
    	$apps = @()	
    	foreach ($bit in "x86","x64")
    	{	
    		$regkey = Get-Variable "regkey_$bit" | %{$_.value}
    		if (Test-Path HKLM:$regkey)
    		{
    			$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
    			$key = $reg.OpenSubKey($regkey)
    			$SubKeys = $key.GetSubKeyNames() | %{$key.OpenSubKey($_)}
    	
    			foreach ($subkey in $subkeys)
    			{
    				$app = $subkey | select @{N="Bit";E={$bit}},`
    										@{N="Name";E={$_.getvalue('DisplayName')}},`
    										@{N="Version";E={$_.GetValue('DisplayVersion')}},`
    										@{N="Publisher";E={$_.GetValue('Publisher')}}
    				if ($app.name) {$apps += $app}
    				}
    			}
    		    $key.Close()
    		    $reg.Close()
    		}
       
    	# Remove duplicates, but annoyingly names like of "C++" error when using "-match"
    	$unique_apps = @()
    	foreach ($app in $apps) 
    	{
    		if (!($unique_apps | ?{$_.Bit -eq $app.bit} | ?{$_.name -eq $app.name} | 
    		?{$_.version -eq $app.version})) {$unique_apps += $app}
    	}
    
    return $unique_apps | sort bit,name,version
    }