你能写一个与BGInfo做同样事情的powershell脚本吗?

时间:2013-10-21 14:34:55

标签: powershell scripting

我希望能够在我的桌面上显示我的计算机名称,内部和外部IP,域等。我想知道是否有可能编写一个Powershell脚本来执行此操作,或者我是否真的必须使用BGInfo或类似的东西。

3 个答案:

答案 0 :(得分:3)

如果您真的想在PowerShell脚本中执行此操作而不是使用BGInfo,那么这是一个可以帮助您入门的脚本:

function Write-Bitmap
{
    param($imagePath, $newImagePath, [string[]]$Text, [float]$X = 0, [float]$Y = 0)

    Add-Type -AssemblyName System.Drawing

    $bmp = $font = $g = $null

    try {
        $width = $rect.Right - $rect.Left + 1
        $height = $rect.Bottom - $rect.Top + 1
        $bmp = new-object System.Drawing.Bitmap $imagePath
        $g = [System.Drawing.Graphics]::FromImage($bmp)
        $font = new-object System.Drawing.Font 'Segoe UI',24
        $brush = [Drawing.Brushes]::Black
        foreach ($line in $text) {
            $g.DrawString($line, $font, $brush, $X, $Y)
            $Y += 30
        }
        $bmp.Save($newImagePath)
    }
    finally {
        if ($bmp) { $bmp.Dispose() }
        if ($font) { $font.Dispose() }
        if ($g) { $g.Dispose() }
    }
}

答案 1 :(得分:0)

我以此为出发点: https://p0w3rsh3ll.wordpress.com/2014/08/29/poc-tatoo-the-background-of-your-virtual-machines/

这是我最终得到的脚本: https://gist.github.com/dieseltravis/3066def0ddaf7a8a0b6d

# PS-BGInfo
# Powershell script that updates the background image with a random image from a folder and writes out system info text to it.

# Configuration:

# Font Family name
$font="Input"
# Font size in pixels
$size=10.0
# spacing in pixels
$textPaddingLeft = 5
$textPaddingTop = 5
$textItemSpace = 3
#TODO: Line-height multiplyer of the $size in pixels.
#$lineHeight = 1.80

$wallpaperImagesSource = "$Env:USERPROFILE\Pictures\wallpaper"
$wallpaperImageOutput = "$Env:USERPROFILE"

# Get local info to write out to wallpaper
$os = Get-CimInstance Win32_OperatingSystem
$cpu = (Get-WmiObject Win32_Processor).Name.Replace("Intel(R) Core(TM) ", "")
$BootTimeSpan = (New-TimeSpan -Start $os.LastBootUpTime -End (Get-Date))
$ip = (Get-NetIPAddress | Where-Object {$_.InterfaceAlias -eq "Ethernet" -and  $_.AddressFamily -eq "IPv4"}).IPAddress

$o = ([ordered]@{
    User = $os.RegisteredUser
    Host = "$($os.CSName) `n$($os.Description)"
    CPU = $cpu
    RAM = "$([math]::round($os.TotalVisibleMemorySize / 1MB))GB"
    OS = "$($os.Caption) `n$($os.OSArchitecture), $($os.Version)"
    Boot = $os.LastBootUpTime
    Uptime = "$($BootTimeSpan.Days) days, $($BootTimeSpan.Hours) hours"
    Snapshot = $os.LocalDateTime
    IP = $ip
})

# original src: https://p0w3rsh3ll.wordpress.com/2014/08/29/poc-tatoo-the-background-of-your-virtual-machines/
Function New-ImageInfo {
    # src: https://github.com/fabriceleal/Imagify/blob/master/imagify.ps1
    param(  
        [Parameter(Mandatory=$True, Position=1)]
        [object] $data,
        [Parameter(Mandatory=$True)]
        [string] $in="",
        [string] $font="Courier New",
        [float] $size=12.0,
        #[float] $lineHeight = 1.4,
        [float] $textPaddingLeft = 0,
        [float] $textPaddingTop = 0,
        [float] $textItemSpace = 0,
        [string] $out="out.png" 
    )


    [system.reflection.assembly]::loadWithPartialName('system') | out-null
    [system.reflection.assembly]::loadWithPartialName('system.drawing') | out-null
    [system.reflection.assembly]::loadWithPartialName('system.drawing.imaging') | out-null
    [system.reflection.assembly]::loadWithPartialName('system.windows.forms') | out-null

    $foreBrush  = [System.Drawing.Brushes]::White
    $backBrush  = new-object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(192, 0, 0, 0))

    # Create font
    $nFont = new-object system.drawing.font($font, $size, [System.Drawing.GraphicsUnit]::Pixel)

    # Create Bitmap
    $SR = [System.Windows.Forms.Screen]::AllScreens | Where Primary | Select -ExpandProperty Bounds | Select Width,Height

    echo $SR >> "$wallpaperImageOutput\wallpaper.log"

    $background = new-object system.drawing.bitmap($SR.Width, $SR.Height)
    $bmp = new-object system.drawing.bitmap -ArgumentList $in

    # Create Graphics
    $image = [System.Drawing.Graphics]::FromImage($background)

    # Paint image's background
    $rect = new-object system.drawing.rectanglef(0, 0, $SR.width, $SR.height)
    $image.FillRectangle($backBrush, $rect)

    # add in image
    $topLeft = new-object System.Drawing.RectangleF(0, 0, $SR.Width, $SR.Height)
    $image.DrawImage($bmp, $topLeft)

    # Draw string
    $strFrmt = new-object system.drawing.stringformat
    $strFrmt.Alignment = [system.drawing.StringAlignment]::Near
    $strFrmt.LineAlignment = [system.drawing.StringAlignment]::Near

    $taskbar = [System.Windows.Forms.Screen]::AllScreens
    $taskbarOffset = $taskbar.Bounds.Height - $taskbar.WorkingArea.Height

    # first get max key & val widths
    $maxKeyWidth = 0
    $maxValWidth = 0
    $textBgHeight = 0 + $taskbarOffset
    $textBgWidth = 0

    # a reversed ordered collection is used since it starts from the bottom
    $reversed = [ordered]@{}

    foreach ($h in $data.GetEnumerator()) {
        $valString = "$($h.Value)"
        $valFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Regular)
        $valSize = [system.windows.forms.textrenderer]::MeasureText($valString, $valFont)
        $maxValWidth = [math]::Max($maxValWidth, $valSize.Width)

        $keyString = "$($h.Name): "
        $keyFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Bold)
        $keySize = [system.windows.forms.textrenderer]::MeasureText($keyString, $keyFont)
        $maxKeyWidth = [math]::Max($maxKeyWidth, $keySize.Width)

        $maxItemHeight = [math]::Max($valSize.Height, $keySize.Height)
        $textBgHeight += ($maxItemHeight + $textItemSpace)

        $reversed.Insert(0, $h.Name, $h.Value)
    }

    $textBgWidth = $maxKeyWidth + $maxValWidth
    $textBgX = $SR.Width - ($textBgWidth + $textPaddingLeft)
    $textBgY = $SR.Height - ($textBgHeight + $textPaddingTop)

    $textBgRect = New-Object System.Drawing.RectangleF($textBgX, $textBgY, $textBgWidth, $textBgHeight)
    $image.FillRectangle($backBrush, $textBgRect)

    echo $textBgRect >> "$wallpaperImageOutput\wallpaper.log"

    $i = 0
    $cumulativeHeight = $SR.Height - $taskbarOffset

    foreach ($h in $reversed.GetEnumerator()) {
        $valString = "$($h.Value)"
        $valFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Regular)
        $valSize = [system.windows.forms.textrenderer]::MeasureText($valString, $valFont)

        $keyString = "$($h.Name): "
        $keyFont = New-Object System.Drawing.Font($font, $size, [System.Drawing.FontStyle]::Bold)
        $keySize = [system.windows.forms.textrenderer]::MeasureText($keyString, $keyFont)

        echo $valString >> "$wallpaperImageOutput\wallpaper.log"
        echo $keyString >> "$wallpaperImageOutput\wallpaper.log"

        $maxItemHeight = [math]::Max($valSize.Height, $keySize.Height) + $textItemSpace

        $valX = $SR.Width - $maxValWidth
        $valY = $cumulativeHeight - $maxItemHeight

        $keyX = $valX - $maxKeyWidth
        $keyY = $valY

        $valRect = New-Object System.Drawing.RectangleF($valX, $valY, $maxValWidth, $valSize.Height)
        $keyRect = New-Object System.Drawing.RectangleF($keyX, $keyY, $maxKeyWidth, $keySize.Height)

        $cumulativeHeight = $valRect.Top

        $image.DrawString($keyString, $keyFont, $foreBrush, $keyRect, $strFrmt)
        $image.DrawString($valString, $valFont, $foreBrush, $valRect, $strFrmt)

        $i++
    }

    # Close Graphics
    $image.Dispose();

    # Save and close Bitmap
    $background.Save($out, [system.drawing.imaging.imageformat]::Png);
    $background.Dispose();
    $bmp.Dispose();

    # Output file
    Get-Item -Path $out
}

Function Set-Wallpaper {
    # src: http://powershell.com/cs/blogs/tips/archive/2014/01/10/change-desktop-wallpaper.aspx
    param(
        [Parameter(Mandatory=$true)]
        $Path,

        [ValidateSet('Center', 'Stretch', 'Fill', 'Tile', 'Fit')]
        $Style = 'Center'
    )

    #TODO: there in't a better way to do this than inline C#?
    Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Wallpaper
{
    public enum Style : int
    {
        Center, Stretch, Fill, Fit, Tile
    }

    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 Panel\\Desktop", true);
            switch( style )
            {
                case Style.Tile :
                    key.SetValue(@"WallpaperStyle", "0") ; 
                    key.SetValue(@"TileWallpaper", "1") ; 
                    break;
                case Style.Center :
                    key.SetValue(@"WallpaperStyle", "0") ; 
                    key.SetValue(@"TileWallpaper", "0") ; 
                    break;
                case Style.Stretch :
                    key.SetValue(@"WallpaperStyle", "2") ; 
                    key.SetValue(@"TileWallpaper", "0") ;
                    break;
                case Style.Fill :
                    key.SetValue(@"WallpaperStyle", "10") ; 
                    key.SetValue(@"TileWallpaper", "0") ; 
                    break;
                case Style.Fit :
                    key.SetValue(@"WallpaperStyle", "6") ; 
                    key.SetValue(@"TileWallpaper", "0") ; 
                    break;
            }
            key.Close();
        }
    }
}
"@

    [Wallpaper.Setter]::SetWallpaper( $Path, $Style )
}

# execute tasks

echo $o > "$wallpaperImageOutput\wallpaper.log"

# get random wallpaper from a folder full of images
Get-ChildItem -Path "$wallpaperImagesSource\*" -Include *.* -Exclude current.jpg | Get-Random | Foreach-Object { Copy-Item -Path $_ -Destination "$wallpaperImagesSource\current.jpg" }

# create wallpaper image and save it in user profile
$WallPaper = New-ImageInfo -data $o -in "$wallpaperImagesSource\current.jpg" -out "$wallpaperImageOutput\wallpaper.png" -font $font -size $size -textPaddingLeft $textPaddingLeft -textPaddingTop $textPaddingTop -textItemSpace $textItemSpace #-lineHeight $lineHeight
echo $WallPaper.FullName >> "$wallpaperImageOutput\wallpaper.log"

# update wallpaper for logged in user
Set-Wallpaper -Path $WallPaper.FullName

它在壁纸的右下角写出了这个信息:

info

除了右下角之外,我没有任何其他定位,但如果你愿意的话,可以随意分叉并解决。我有一个计划任务,每隔几分钟更新一次。

答案 2 :(得分:0)

I am late on the question, but me too, I would like to share with you my script for this purpose (2011) and I decided to publish it to Technet recently https://gallery.technet.microsoft.com/scriptcenter/LockScreenInfo-Display-2adfc20b Best regards.

PS> .\LockScreenInfo.ps1 -SourceImage C:\temp\abstract.jpg -FitScreen -KeepRatio -MessagePicture "C:\Users\install\Pictures\advancedsettings.png" -MessageText "Welcome to StackOverflow!" -TargetImage C:\Temp\Background.jpg

Sample tatoued background

相关问题