Advertisement
Guest User

p0w3rsh3ll.wordpress.com

a guest
Feb 28th, 2015
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function New-BGinfo {
  2.     Param(  [Parameter(Mandatory)]
  3.             [string] $Text,
  4.  
  5.             [Parameter()]
  6.             [string] $OutFile= "$($env:temp)\BGInfo.bmp",
  7.  
  8.             [Parameter()]
  9.             [ValidateSet("Left","Center")]
  10.             [string]$Align="Center",
  11.  
  12.  
  13.             [Parameter()]
  14.             [ValidateSet("Current","Blue","Grey","Black")]
  15.             [string]$Theme="Current",
  16.  
  17.             [Parameter()]
  18.             [string]$FontName="Arial",
  19.  
  20.             [Parameter()]
  21.             [ValidateRange(9,45)]
  22.             [int32]$FontSize = 12,
  23.  
  24.             [Parameter()]
  25.             [switch]$UseCurrentWallpaperAsSource
  26.     )
  27.     Begin {
  28.  
  29.         # Enumerate current wallpaper now, so we can decide whether it's a solid colour or not
  30.         try {
  31.             $wpath = (Get-ItemProperty 'HKCU:\Control Panel\Desktop' -Name WallPaper -ErrorAction Stop).WallPaper
  32.             if ($wpath.Length -eq 0) {
  33.                 # Solid colour used
  34.                 $UseCurrentWallpaperAsSource = $false
  35.                 $Theme = "Current"
  36.             }
  37.         } catch {
  38.             $UseCurrentWallpaperAsSource = $false
  39.             $Theme = "Current"
  40.         }
  41.  
  42.         Switch ($Theme) {
  43.             Current {
  44.                 $RGB = (Get-ItemProperty 'HKCU:\Control Panel\Colors' -ErrorAction Stop).BackGround
  45.                 if ($RGB.Length -eq 0) {
  46.                     $Theme = "Black" # Default to Black and don't break the switch
  47.                 } else {
  48.                     $BG = $RGB -split " "
  49.                     $FC1 = $FC2 = @(255,255,255)
  50.                     $FS1=$FS2=$FontSize
  51.                     break
  52.                 }
  53.             }
  54.             Blue {
  55.                 $BG = @(58,110,165)
  56.                 $FC1 = @(254,253,254)
  57.                 $FC2 = @(185,190,188)
  58.                 $FS1 = $FontSize+1
  59.                 $FS2 = $FontSize-2
  60.                 break
  61.             }
  62.             Grey {
  63.                 $BG = @(77,77,77)
  64.                 $FC1 = $FC2 = @(255,255,255)
  65.                 $FS1=$FS2=$FontSize
  66.                 break
  67.             }
  68.             Black {
  69.                 $BG = @(0,0,0)
  70.                 $FC1 = $FC2 = @(255,255,255)
  71.                 $FS1=$FS2=$FontSize
  72.             }
  73.         }
  74.         Try {
  75.             [system.reflection.assembly]::loadWithPartialName('system.drawing.imaging') | out-null
  76.             [system.reflection.assembly]::loadWithPartialName('system.windows.forms') | out-null
  77.  
  78.             # Draw string > alignement
  79.             $sFormat = new-object system.drawing.stringformat
  80.  
  81.             Switch ($Align) {
  82.                 Center {
  83.                     $sFormat.Alignment = [system.drawing.StringAlignment]::Center
  84.                     $sFormat.LineAlignment = [system.drawing.StringAlignment]::Center
  85.                     break
  86.                 }
  87.                 Left {
  88.                     $sFormat.Alignment = [system.drawing.StringAlignment]::Center
  89.                     $sFormat.LineAlignment = [system.drawing.StringAlignment]::Near
  90.                 }
  91.             }
  92.  
  93.             if ($UseCurrentWallpaperAsSource) {
  94.                 if (Test-Path -Path $wpath -PathType Leaf) {
  95.                     $bmp = new-object system.drawing.bitmap -ArgumentList $wpath
  96.                     $image = [System.Drawing.Graphics]::FromImage($bmp)
  97.                     $SR = $bmp | Select Width,Height
  98.                 } else {
  99.                     Write-Warning -Message "Failed cannot find the current wallpaper $($wpath)"
  100.                     break
  101.                 }
  102.             } else {
  103.                 $SR = [System.Windows.Forms.Screen]::AllScreens | Where Primary |
  104.                 Select -ExpandProperty Bounds | Select Width,Height
  105.  
  106.                 Write-Verbose -Message "Screen resolution is set to $($SR.Width)x$($SR.Height)" -Verbose
  107.  
  108.                 # Create Bitmap
  109.                 $bmp = new-object system.drawing.bitmap($SR.Width,$SR.Height)
  110.                 $image = [System.Drawing.Graphics]::FromImage($bmp)
  111.      
  112.                 $image.FillRectangle(
  113.                     (New-Object Drawing.SolidBrush (
  114.                         [System.Drawing.Color]::FromArgb($BG[0],$BG[1],$BG[2])
  115.                     )),
  116.                     (new-object system.drawing.rectanglef(0,0,($SR.Width),($SR.Height)))
  117.                 )
  118.  
  119.             }
  120.         } Catch {
  121.             Write-Warning -Message "Failed to $($_.Exception.Message)"
  122.             break
  123.         }
  124.     }
  125.     Process {
  126.  
  127.         # Split our string as it can be multiline
  128.         $artext = ($text -split "\r\n")
  129.      
  130.         $i = 1
  131.         Try {
  132.             for ($i ; $i -le $artext.Count ; $i++) {
  133.                 if ($i -eq 1) {
  134.                     $font1 = New-Object System.Drawing.Font($FontName,$FS1,[System.Drawing.FontStyle]::Bold)
  135.                     $Brush1 = New-Object Drawing.SolidBrush (
  136.                         [System.Drawing.Color]::FromArgb($FC1[0],$FC1[1],$FC1[2])
  137.                     )
  138.                     $sz1 = [system.windows.forms.textrenderer]::MeasureText($artext[$i-1], $font1)
  139.                     $rect1 = New-Object System.Drawing.RectangleF (0,($sz1.Height),$SR.Width,$SR.Height)
  140.                     $image.DrawString($artext[$i-1], $font1, $brush1, $rect1, $sFormat)
  141.                 } else {
  142.                     $font2 = New-Object System.Drawing.Font($FontName,$FS2,[System.Drawing.FontStyle]::Bold)
  143.                     $Brush2 = New-Object Drawing.SolidBrush (
  144.                         [System.Drawing.Color]::FromArgb($FC2[0],$FC2[1],$FC2[2])
  145.                     )
  146.                     $sz2 = [system.windows.forms.textrenderer]::MeasureText($artext[$i-1], $font2)
  147.                     $rect2 = New-Object System.Drawing.RectangleF (0,($i*$FontSize*2 + $sz2.Height),$SR.Width,$SR.Height)
  148.                     $image.DrawString($artext[$i-1], $font2, $brush2, $rect2, $sFormat)
  149.                 }
  150.             }
  151.         } Catch {
  152.             Write-Warning -Message "Failed to $($_.Exception.Message)"
  153.             break
  154.         }
  155.     }
  156.     End {  
  157.         Try {
  158.             # Close Graphics
  159.             $image.Dispose();
  160.  
  161.             # Save and close Bitmap
  162.             $bmp.Save($OutFile, [system.drawing.imaging.imageformat]::Bmp);
  163.             $bmp.Dispose();
  164.  
  165.             # Output our file
  166.             Get-Item -Path $OutFile
  167.         } Catch {
  168.             Write-Warning -Message "Failed to $($_.Exception.Message)"
  169.             break
  170.         }
  171.     }
  172.  
  173. } # endof function
  174.  
  175.  
  176. Function Set-Wallpaper {
  177.     Param(
  178.         [Parameter(Mandatory=$true)]
  179.         $Path,
  180.          
  181.         [ValidateSet('Center','Stretch','Fill','Tile','Fit')]
  182.         $Style = 'Stretch'
  183.     )
  184.     Try {
  185.         if (-not ([System.Management.Automation.PSTypeName]'Wallpaper.Setter').Type) {
  186.             Add-Type -TypeDefinition @"
  187.            using System;
  188.            using System.Runtime.InteropServices;
  189.            using Microsoft.Win32;
  190.            namespace Wallpaper {
  191.                public enum Style : int {
  192.                Center, Stretch, Fill, Fit, Tile
  193.                }
  194.                public class Setter {
  195.                    public const int SetDesktopWallpaper = 20;
  196.                    public const int UpdateIniFile = 0x01;
  197.                    public const int SendWinIniChange = 0x02;
  198.                    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  199.                    private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
  200.                    public static void SetWallpaper ( string path, Wallpaper.Style style ) {
  201.                        SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
  202.                        RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
  203.                        switch( style ) {
  204.                            case Style.Tile :
  205.                                key.SetValue(@"WallpaperStyle", "0") ;
  206.                                key.SetValue(@"TileWallpaper", "1") ;
  207.                                break;
  208.                            case Style.Center :
  209.                                key.SetValue(@"WallpaperStyle", "0") ;
  210.                                key.SetValue(@"TileWallpaper", "0") ;
  211.                                break;
  212.                            case Style.Stretch :
  213.                                key.SetValue(@"WallpaperStyle", "2") ;
  214.                                key.SetValue(@"TileWallpaper", "0") ;
  215.                                break;
  216.                            case Style.Fill :
  217.                                key.SetValue(@"WallpaperStyle", "10") ;
  218.                                key.SetValue(@"TileWallpaper", "0") ;
  219.                                break;
  220.                            case Style.Fit :
  221.                                key.SetValue(@"WallpaperStyle", "6") ;
  222.                                key.SetValue(@"TileWallpaper", "0") ;
  223.                                break;
  224. }
  225.                        key.Close();
  226.                    }
  227.                }
  228.            }
  229. "@ -ErrorAction Stop
  230.             } else {
  231.                 Write-Verbose -Message "Type already loaded" -Verbose
  232.             }
  233.         # } Catch TYPE_ALREADY_EXISTS
  234.         } Catch {
  235.             Write-Warning -Message "Failed because $($_.Exception.Message)"
  236.         }
  237.      
  238.     [Wallpaper.Setter]::SetWallpaper( $Path, $Style )
  239. }
  240.  
  241.  
  242. $os = Get-CimInstance Win32_OperatingSystem
  243. ($o = [pscustomobject]@{
  244.     HostName =  $env:COMPUTERNAME
  245.     UserName = '{0}\{1}' -f  $env:USERDOMAIN,$env:USERNAME
  246.     'Operating System' = '{0} Service Pack {1} (build {2})' -f  $os.Caption,
  247.     $os.ServicePackMajorVersion,$os.BuildNumber
  248. }) | ft -AutoSize
  249. $BootTime = (New-TimeSpan -Start $os.LastBootUpTime -End (Get-Date)).ToString()
  250.  
  251. # $t is the multiline text defined as here-string
  252. $t = @"
  253. $($o.HostName)
  254. Logged on user: $($o.UserName)
  255. $($o.'Operating System')
  256. Uptime: $BootTime
  257. "@
  258.  
  259. $BGHT = @{
  260.  Text  = $t ;
  261.  Theme = "Current" ;
  262.  FontName = "Verdana" ;
  263.  UseCurrentWallpaperAsSource = $true ;
  264. }
  265. $WallPaper = New-BGinfo @BGHT
  266. Set-Wallpaper -Path $WallPaper.FullName -Style Fill
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement