ryzhov_al

BGInfo PowerShell replacement

Aug 21st, 2021 (edited)
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Script to set the wallpaper on a machine similar to BGInfo but without VBScripts
  2.  
  3. $line1     = "ПК: $env:COMPUTERNAME"
  4. $line2     = "Пользователь: $Env:USERNAME"
  5. $typeFace  = 'Vernada'
  6. $bgColor   = (255, 70, 70, 70) #aRGB
  7. $txtColor  = (255, 255, 255, 255) #aRGB
  8. $txtShadow = (200, 0, 0, 0) #aRGB
  9. $src = (Get-ItemProperty -Path 'HKCU:\Control Panel\Desktop' -Name Wallpaper).Wallpaper
  10. $dst = "$PSScriptRoot\dst.jpg"
  11.  
  12. # Based on https://github.com/cwhits/PSBGinfo, written by Chris Helming, Oscar Santiano and Chase Caynoski
  13. Function AddTextToImage {
  14.     [CmdletBinding()]
  15.     PARAM (
  16.         [Parameter(Mandatory=$false)][String]$sourcePath,
  17.         [Parameter(Mandatory=$true)][String]$destPath,
  18.         [Parameter(Mandatory=$true)][String]$line1,
  19.         [Parameter(Mandatory=$true)][String]$line2,
  20.         [Parameter(Mandatory=$true)][String]$typeFace,
  21.         [Parameter(Mandatory=$true)][Array]$bgColor,
  22.         [Parameter(Mandatory=$true)][Array]$txtColor,
  23.         [Parameter(Mandatory=$true)][Array]$txtShadow
  24.     )
  25.  
  26.     [Reflection.Assembly]::LoadWithPartialName('System.Drawing') | Out-Null
  27.  
  28.     $screen = Get-WmiObject -Class win32_videocontroller | where CurrentHorizontalResolution -NE $null
  29.     $screenWidth = $screen[0].CurrentHorizontalResolution
  30.     $screenHeight = $screen[0].CurrentVerticalResolution
  31.  
  32.     # Move the text in 20 pixels from the right side of the screen
  33.     $textPos = $screenwidth - 20
  34.     $shadowPos = $textPos + 2
  35.    
  36.     $bmpFile = New-Object System.Drawing.Bitmap([int]($screenWidth)),([int]($screenHeight))
  37.  
  38.     $Image = [System.Drawing.Graphics]::FromImage($bmpFile)
  39.     $Image.clear([System.Drawing.Color]::FromArgb($bgcolor[0], $bgcolor[1], $bgcolor[2], $bgcolor[3]))
  40.     $Image.SmoothingMode = 'AntiAlias'
  41.  
  42.     $stringFormat = New-Object System.Drawing.StringFormat
  43.     $stringFormat.Alignment = [System.Drawing.StringAlignment]::Far
  44.  
  45.     if($sourcePath){
  46.         $srcImg = [System.Drawing.Image]::FromFile($sourcePath)
  47.         $Rectangle = New-Object Drawing.Rectangle ($screenWidth/2 - $srcImg.width/2), ($screenHeight/2 - $srcImg.height/2), $srcImg.Width, $srcImg.Height
  48.         $Image.DrawImage($srcImg, $Rectangle, 0, 0, $srcImg.Width, $srcImg.Height, ([Drawing.GraphicsUnit]::Pixel))
  49.         $srcImg.Dispose()
  50.     }
  51.  
  52.     # Drop shadow for text
  53.     $Font = New-Object System.Drawing.Font($typeFace, 15)
  54.     $Brush = New-Object Drawing.SolidBrush ([System.Drawing.Color]::FromArgb($txtShadow[0], $txtShadow[1], $txtShadow[2], $txtShadow[3]))
  55.     $Image.DrawString($line1, $Font, $Brush, $shadowPos, 12, $stringFormat)
  56.    
  57.     $Font = New-object System.Drawing.Font($typeFace, 15)
  58.     $Brush = New-Object Drawing.SolidBrush ([System.Drawing.Color]::FromArgb($txtShadow[0], $txtShadow[1], $txtShadow[2], $txtShadow[3]))
  59.     $Image.DrawString($line2, $Font, $Brush, $shadowPos, 42, $stringFormat)
  60.    
  61.     # Draw text strings
  62.     $Font = new-object System.Drawing.Font($typeFace, 15)
  63.     $Brush = New-Object Drawing.SolidBrush ([System.Drawing.Color]::FromArgb($txtColor[0], $txtColor[1], $txtColor[2], $txtColor[3]))
  64.     $Image.DrawString($line1, $Font, $Brush, $textpos, 10, $stringFormat)
  65.  
  66.     $Font = New-object System.Drawing.Font($typeFace, 15)
  67.     $Brush = New-Object Drawing.SolidBrush ([System.Drawing.Color]::FromArgb($txtColor[0], $txtColor[1], $txtColor[2], $txtColor[3]))
  68.     $Image.DrawString($line2, $Font, $Brush, $textpos, 40, $stringFormat)
  69.    
  70.     # Save image
  71.     $bmpFile.save($destPath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
  72.     $bmpFile.Dispose()  
  73. }
  74.  
  75. AddTextToImage -sourcePath $src -destPath $dst -line1 $line1 -line2 $line2 -typeFace $typeFace -bgColor $bgColor -txtColor $txtColor -txtShadow $txtShadow
  76.  
  77. # Set wallpaper
  78. Set-ItemProperty -path 'HKCU:\Control Panel\Desktop' -name wallpaper -value $dst
  79. Add-Type -TypeDefinition @'
  80. using System;
  81. using System.Runtime.InteropServices;
  82.  
  83. public class Params
  84. {
  85.    [DllImport("User32.dll",CharSet=CharSet.Unicode)]
  86.    public static extern int SystemParametersInfo (Int32 uAction, Int32 uParam, String lpvParam, Int32 fuWinIni);
  87. }
  88. '@
  89.  
  90. $SPI_SETDESKWALLPAPER = 0x0014
  91. $UpdateIniFile = 0x01
  92. $SendChangeEvent = 0x02
  93. $fWinIni = $UpdateIniFile -bor $SendChangeEvent
  94. [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $dst, $fWinIni) | Out-Null
  95.  
  96. # Add new task if necessary
  97. <#
  98. if(!(Get-ScheduledTask -TaskName 'BGInfo' -ErrorAction SilentlyContinue)) {
  99.     $TaskAction = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-WindowStyle Hidden -NonInteractive -File "$($MyInvocation.InvocationName)"
  100.     $TaskTrigger =  New-ScheduledTaskTrigger ...
  101.     Register-ScheduledTask -Action $TaskAction -Trigger $TaskTrigger -TaskName 'BGInfo' -Description 'Set background info with PC and User names'
  102. }
  103. #>
Advertisement
Add Comment
Please, Sign In to add comment