This might take you 3 minutes to read.

Ever wanted a desktop background that you dynamically could write to. (like bginfo) I did, but I didn’t want an exe running in that environment. And it was Windows 2008R2 Terminal Servers, with very little rights to the users that logged on. So what i came up with was a powershell script that did run at logon via a GPO.

The difficulties I needed to overcome was:

  1. Multi monitor setup.
  2. Text only on main screen
  3. Different resolutions
  4. No .exe was allowed
  5. Shared environment (terminal servers and clients)

I found some code on http://poshcode.org/3401 that I used to after some time spent on trying to set the desktop background with Powershell alone.

What it does is following:

takes an image in the same directory as it is run in (background.png). In this case a logo. Then it reads the resolution of all the screens and calculates the new image resolution. Calculates where the primary desktop is located and puts the logo on the primary desktop. Then it takes Username, domanname, and doe custom text, for example help desk phone number and puts on the background. Saves the image in the users appdata-folder and updates the registry and refreshes the screen.

# http://poshcode.org/3401
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
#requires -version 2.0
add-type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
public enum Style : int
{
Tile, Center, Stretch, NoChange
}
public class Setter {
public const int SetDesktopWallpaper = 20;
public const int UpdateIniFile = 0x01;
public const int SendWinIniChange = 0x02;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
public static void SetWallpaper ( string path, Wallpaper.Style style ) {
SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
RegistryKey key = Registry.CurrentUser.OpenSubKey("Control PanelDesktop", true);
switch( style )
{
case Style.Stretch :
key.SetValue(@"WallpaperStyle", "2") ;
key.SetValue(@"TileWallpaper", "0") ;
break;
case Style.Center :
key.SetValue(@"WallpaperStyle", "1") ;
key.SetValue(@"TileWallpaper", "0") ;
break;
case Style.Tile :
key.SetValue(@"WallpaperStyle", "1") ;
key.SetValue(@"TileWallpaper", "1") ;
break;
case Style.NoChange :
break;
}
key.Close();
}
}
}
"@
function Set-Wallpaper {
Param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[Alias("FullName")]
[string]
$Path
,
[Parameter(Position=1, Mandatory=$false)]
[Wallpaper.Style]
$Style = "NoChange"
)
[Wallpaper.Setter]::SetWallpaper( (Convert-Path $Path), $Style )
}
$totalWidth = 0
$totalHeight = 0
$PrimaryWidth = 0
$PrimaryHeight = 0
$mostLeft = 0
$mostRight = 0
$mostTop = 0
$mostBottom = 0
$Screens = [system.windows.forms.screen]::AllScreens
foreach ($screen in $screens) {
if ($screen.primary) {
$PrimaryWidth = $screen.Bounds.Width
$PrimaryHeight = $screen.Bounds.Height
}
if ($screen.WorkingArea.Left -lt $mostLeft) {
$mostLeft = $screen.WorkingArea.Left
}
if ($screen.WorkingArea.Right -gt $mostRight) {
$mostRight = $screen.WorkingArea.Right
}
if ($screen.WorkingArea.Top -lt $mostTop) {
$mostTop = $screen.WorkingArea.top
}
if ($screen.WorkingArea.Bottom -gt $mostBottom) {
$mostBottom = $screen.WorkingArea.Bottom
}
}
if ($mostLeft -lt 0){
$mostLeft = -$mostLeft
}
if ($mostBottom -lt 0){
$mostBottom = -$mostBottom
}
$ScreenWidth = $mostLeft + $mostRight
$ScreenHeight = $mostTop + $mostBottom
$primaryX = ($mostLeft)
$PrimaryY = $ScreenHeight - $mostBottom
$computerName = get-content env:computername
$text = "Helpdesk: `t+46 8 123 123 12"
$savefilename = "$env:APPDATAbackground.bmp"
$image = [System.Drawing.Bitmap]::FromFile("background.png")
$username = get-content env:UserName
$domain = get-content env:UserDomain
$SID = ([wmi]"win32_userAccount.Domain='$domain',Name='$username'").sid
$DN = Get-ItemProperty "HKLM:SOFTWAREMicrosoftWindowsCurrentVersionGroup PolicyState$SID" | select -ExpandProperty "Distinguished-Name"
$indexof = ($DN.IndexOf("(") - 3)
if ($indexOf -lt 0) {
$indexof = ($DN.indexof(",") - 3 )
}
$fullname = $DN.substring(3,$indexof)
$textArray = @(("User: `t`t" + $fullname),("Computername: `t`t" + $computerName), $text)
$NewBitmap = new-object System.Drawing.Bitmap $ScreenWidth,$ScreenHeight
$fontSize = 12
$newLine = $fontSize + 10
$font = new-object System.Drawing.Font Verdana, $fontSize
$color = [System.Drawing.Color]::FromArgb(59, 110, 165)
$brushBg = new-object Drawing.SolidBrush $color
$brushFg = [System.Drawing.Brushes]::White
$brushShadow = [System.Drawing.Brushes]::Gray
$graphics = [System.Drawing.Graphics]::FromImage($NewBitmap)
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$graphics.FillRectangle($brushBg,0,0,$ScreenWidth,$ScreenHeight)
$graphics.DrawImage($image, (($PrimaryWidth)-$image.width)-20, 20, $image.width, $image.height)
$h = 17
$h = ($PrimaryHeight - ($textArray.count * $newLine ) - 50)
foreach ($t in $textArray) {
$graphics.DrawString($t,$font,$brushFg,(($PrimaryWidth)-400)-20,$h)
$h = $h + $newline
}
$graphics.Dispose()
$NewBitmap.Save($savefilename , [system.drawing.imaging.imageformat]::bmp)
#Invoke-Item $savefilename
Set-Wallpaper $savefilename "Tile"

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.