segunda-feira, 12 de agosto de 2019

Windows - bginfo + powershell


Changelog:
1.0.0.1 - Cleaner PS script, get-help .\psBGinfo.ps1 -full
1.0.0.2 - Added a deployment version - psBGinfo_sccm.ps1

How many times have you walked up to a system in your office and 
needed to click through several diagnostic windows to remind yourself of
 important aspects of its configuration, such as its name, IP address, 
or operating system version? If you manage multiple
 computers you probably need BGInfo. It automatically displays 
relevant information about a Windows computer on the desktop's 
background, such as the computer name, IP address, service pack version,
 and more. You can edit any field as well as
 the font and background colors, and can place it in your startup folder
 so that it runs every boot, or even configure it to display as the 
background for the logon screen.
Because BGInfo simply writes a new desktop bitmap and exits, you don't have to worry about it consuming system resources or interfering with other applications.
https://technet.microsoft.com/en-us/sysinternals/bginfo.aspx?f=255&MSPPError=-2147217396
I have taken Mark Russinovich tool "BgInfo" and made a powershell script for simple deployment/configuring of BgInfo in your environment.
The zip file contain following:
  • PSBgInfo.ps1*
  • restore.bgi*
  • default.bgi*
  • W2012.bgi
  • W2008.bgi
  • W7.bgi
*Requirement: Windows Management Framework 4.0 https://www.microsoft.com/en-us/download/details.aspx?id=40855
The file marked with * is needed.
To install BgInfo just run the PSBinfo.ps1 with administrator right. It will then use the "default.bgi" config settings and create a BgInfo key in the regedit under HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. This will then run on everyone who log on.
The script output what happening to the console:

PowerShell
PS S:\Dev\PSBgInfo> S:\Dev\PSBgInfo\PSBgInfo.ps1 
24-02-16 09:43:37 # DeploymentType: Install Config: default 
24-02-16 09:43:37 # Downloading BGInfo.zip to C:\Users\Dev\AppData\Local\Temp 
24-02-16 09:43:38 # Extracting BGInfo to: c:\BGinfo 
24-02-16 09:43:38 # Copy config: 'default.bgi' to 'c:\BGinfo\' folder 
24-02-16 09:43:38 # c:\BGinfo\Bginfo.exe c:\BGinfo\default.bgi /timer:00 /accepteula /silent 
24-02-16 09:43:38 # Creating regedit Key for Startup - HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\BgInfo 
 
 
BgInfo       : c:\BGinfo\Bginfo.exe "c:\BGinfo\default.bgi" "/timer:00 /accepteula /silent" 
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion 
PSChildName  : Run 
PSDrive      : HKLM 
PSProvider   : Microsoft.PowerShell.Core\Registry 
 
24-02-16 09:43:38 # Time taken: 0 second(s)
Custom bgi files

The script can handle custom bgi files to.
example on a Windows 2012 server i want to use this file "W2012.bgi".
.\PSBgInfo.ps1 -Config "W2012"
The custom bgi file is placed in the same folder as the script PSBgInfo.ps1
If the script can't find the custom provided bgi file it will use the default.bgi.
To restore/uninstall PSBinfo
.\PSBgInfo.ps1 -DeploymentType "Uninstall"

PowerShell
PS S:\Dev\PSBgInfo> .\PSBgInfo.ps1 -DeploymentType Uninstall 
24-02-16 09:48:00 # DeploymentType: Uninstall Config: default 
24-02-16 09:48:00 # Restoring wallpaper 
24-02-16 09:48:01 # Removing regedit Key for Startup - HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\BgInfo 
24-02-16 09:48:02 # Removing folder: c:\BGinfo 
24-02-16 09:48:02 # Time taken: 1 second(s)

Install folder
  • $instfolder = "c:\BGinfo"
The script install BGinfo in a folder named "BgInfo" under C:. This can be change by editing the $instfolder value.

The New Script PSBgInfo.ps1




PowerShell
<# 
 
.SYNOPSIS 
This Powershell script simplify the installation/configuration of BGInfo local/remote. 
 
.DESCRIPTION 
This script need to be run with administrator rights. 
 
To install on remote computer enable: "Enable-PSRemoting -Force" on remote maschine.  
Target_Host need to be a member of a domain 
Read more here: https://technet.microsoft.com/da-dk/library/hh849694(v=wps.630).aspx 
 
.EXAMPLE 
BGInfo Examples 
Install Local with default config: 
PSBgInfo.ps1  
 
Install with custom config: 
Local install:                    PSBgInfo.ps1 -DeploymentType "Install" 
Remote install single Host:        PSBgInfo.ps1 -DeploymentType "Install" -ComputerName "Target_Host" 
 
Uninstall 
Local uninstall:                PSBgInfo.ps1 -DeploymentType "Uninstall" 
Remote uninstall single Host:    PSBgInfo.ps1 -DeploymentType "Uninstall" -ComputerName "Target_Host" 
 
Remote multiple host 
Remote install multiple Host:    PSBgInfo.ps1 -DeploymentType "Install" -File "c:\Host_list.txt" 
Remote uninstall multiple Host:    PSBgInfo.ps1 -DeploymentType "Uninstall" -File "c:\Host_list.txt" 
 
Host_list.txt contains hostname on seperate lines like 
Target_Host01 
Target_Host02 
Target_Host03 
 
.NOTES 
# Powershell - Created with Windows PowerShell ISE 
# NAME:  PSBgInfo.ps1 
# AUTHOR: Long Truong 
# DATE  : 06-03-2016 
# Version : 1.0.0.1   
# Requirement: 
# Windows Management Framework 4.0 https://www.microsoft.com/en-us/download/details.aspx?id=40855 
 
.LINK 
https://gallery.technet.microsoft.com/scriptcenter/PSBginfo-BgInfo-powershell-199249c7 
 
#> 
 
[CmdletBinding(DefaultParametersetName = 'Set 1')] 
Param ( 
    [ValidateSet('Install''Uninstall')] 
    [string] 
    $DeploymentType = 'Install', 
     
    [Parameter(ParameterSetName = 'Set 1')] 
    [string] 
    $ComputerName = $env:ComputerName, 
     
    [Parameter(ParameterSetName = 'Set 2')] 
    [ValidateScript({ Test-Path $_ })] 
    [string] 
    $File, 
     
    [string] 
    $Config = 'default', 
     
    [PSCredential] 
    $Credential, 
     
    [string] 
    $UserName = 'devlab\admin' 
) 
$start_time = Get-Date 
 
# Retrieve the active parameter set 
$ParSet = $PSCmdlet.ParameterSetName 
 
#region Utility Functions 
function Write-Message 
{ 
    param ( 
        [string] 
        $sMessage 
    ) 
    Write-Verbose "$(Get-Date -Format 'dd-MM-yy HH:mm:ss') # $sMessage" 
} 
 
Function Install-BGInfo 
{ 
    [CmdletBinding()] 
    Param ( 
        [string] 
        $ComputerName, 
         
        [PSCredential] 
        $Credential, 
         
        [string] 
        $Config 
    ) 
     
    # Prepare Paths 
    $url = 'https://download.sysinternals.com/files/BGInfo.zip' 
    $instfile = "$env:temp\BGInfo.zip" 
    $instfolder = "\\$ComputerName\c$\BGinfo" 
    $exec_bginfo = "$instfolder\Bginfo.exe" 
     
     
    Write-Message "DeploymentType: $DeploymentType" 
    Write-Message "Config: $Config" 
     
    #region Ensure BGInfo.exe is in place 
    if (Test-Path -Path $instfolder\Bginfo.exe) 
    { 
        Write-Message "$instfolder\Bginfo.exe exist" 
    } 
    Else 
    { 
        if (Test-Path -Path $instfile) 
        { 
            Write-Message "$env:temp\BGInfo.zip exist" 
        } 
        Else 
        { 
            Write-Message "Downloading BGInfo.zip to $env:temp" 
            Import-Module BitsTransfer 
            Start-BitsTransfer -Source $url -Destination $instfile 
        } 
        Write-Message "Extracting BGInfo to: $instfolder" 
        try { Add-Type -AssemblyName 'System.IO.Compression.FileSystem' -ErrorAction Stop } 
        catch { } 
        [System.IO.Compression.ZipFile]::ExtractToDirectory($instfile$instfolder) 
    } 
    #endregion Ensure BGInfo.exe is in place 
     
    #region Transfer BGI Config file 
    if (Test-Path -Path $PSScriptRoot\$config.bgi) 
    { 
        Write-Message "Copy config: '$config.bgi' to '$instfolder\' folder" 
        Copy-Item -Path $PSScriptRoot\$config.bgi -Destination $instfolder\$config.bgi 
    } 
    Else 
    { 
        Write-Message "Config: '$config.bgi' does not exist in '$PSScriptRoot\' check folder!!!" 
        $config = 'default' 
         
        if (Test-Path -Path $PSScriptRoot\$config.bgi) 
        { 
            Write-Message "Using default config: 'default.bgi'" 
            Write-Message "Copy config: '$config.bgi' to '$instfolder\' folder" 
            Copy-Item -Path $PSScriptRoot\$config.bgi -Destination $instfolder\default.bgi 
        } 
        Else 
        { 
            Write-Message "Config: '$config.bgi' does not exist in '$PSScriptRoot\' check folder!!!" 
        } 
    } 
    #endregion Transfer BGI Config file 
     
    #region Add BGInfo to Autorun 
    If ($ComputerName -ne $env:ComputerName) 
    { 
        Write-Message 'Remote - Creating regedit Key for Startup - HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\BgInfo' 
        Invoke-Command -Credential $credential -ComputerName $ComputerName -ScriptBlock { 
            param ($instfolder, 
                 
                $Config) 
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo -Value """$instfolder\Bginfo.exe"" $instfolder\$config.bgi /timer:00 /accepteula /silent" -PropertyType 'String' -Force 
        } -ArgumentList $instfolder$Config 
    } 
    Else 
    { 
        Write-Message 'Local - Creating regedit Key for Startup under:' 
        Write-Message 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\BgInfo' 
        New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo -Value """$instfolder\Bginfo.exe"" $instfolder\$config.bgi /timer:00 /accepteula /silent" -PropertyType 'String' -Force 
        Write-Message "Executing: $instfolder\Bginfo.exe $instfolder\$config.bgi /timer:00 /accepteula /silent" 
        & $instfolder\Bginfo.exe "$instfolder\$config.bgi" /timer:00 /accepteula /silent 
    } 
    #endregion Add BGInfo to Autorun 
} 
 
Function Uninstall-BGInfo 
{ 
    [CmdletBinding()] 
    Param ( 
        [string] 
        $ComputerName, 
         
        [PSCredential] 
        $Credential, 
         
        [string] 
        $DeploymentType, 
         
        [string] 
        $Config 
    ) 
     
    # Prepare Paths 
    $url = 'https://download.sysinternals.com/files/BGInfo.zip' 
    $instfile = "$env:temp\BGInfo.zip" 
    $instfolder = "\\$ComputerName\c$\BGinfo" 
    $exec_bginfo = "$instfolder\Bginfo.exe" 
     
    Write-Message "DeploymentType: $DeploymentType" 
    Write-Message "Config: $Config" 
     
    #region remote Execution 
    If ($ComputerName -ne $env:ComputerName) 
    { 
        Write-Message 'Remote - Cleanup desktop at next reboot' 
        Copy-Item -Path $PSScriptRoot\restore.bgi -Destination $instfolder\restore.bgi 
        Invoke-Command -Credential $credential -ComputerName $ComputerName -ScriptBlock { 
            param ($instfolder, 
                 
                $Config) 
            remove-itemproperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo 
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' -Name BgInfo -Value """$instfolder\Bginfo.exe"" $instfolder\restore.bgi /timer:00 /accepteula /silent" -PropertyType 'String' -Force 
        } -ArgumentList $instfolder$Config 
    } 
    #endregion remote Execution 
     
    #region Local Execution 
    Else 
    { 
        Write-Message 'Local - Removing BgInfo' 
        Write-Message 'Restoring wallpaper' 
        Write-Message "Executing: $instfolder\Bginfo.exe $PSScriptRoot\restore.bgi /timer:00 /accepteula /silent" 
        & $instfolder\Bginfo.exe "$PSScriptRoot\restore.bgi" /timer:00 /accepteula /silent 
        Write-Message 'Removing regedit Key for Startup:' 
        Write-Message 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\BgInfo' 
        remove-itemproperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo 
        Start-Sleep -m 2000 
        Write-Message "Removing folder: $instfolder" 
        Remove-Item "$instfolder" -recurse 
    } 
    #endregion Local Execution 
} 
#endregion Utility Functions 
 
switch ($ParSet) 
{ 
    'Set 1' 
    { 
        If ($ComputerName -ne $env:ComputerName) 
        { 
            if ($PSBoundParameters['Credential']) { $UseCred = $Credential } 
            else { $UseCred = Get-Credential -Credential $UserName } 
             
            Write-Message "ComputerName: $ComputerName" 
            switch ($DeploymentType) 
            { 
                'Install' { Install-BGInfo -ComputerName $ComputerName -Credential $UseCred -Config $Config } 
                'Uninstall' { Uninstall-BGInfo -ComputerName $ComputerName -Credential $UseCred -Config $Config } 
            } 
            Write-Message "Executed on remote host: $ComputerName" 
        } 
        else 
        { 
            Write-Message "ComputerName: $ComputerName" 
            switch ($DeploymentType) 
            { 
                'Install' { Install-BGInfo -ComputerName $ComputerName -Config $Config } 
                'Uninstall' { Uninstall-BGInfo -ComputerName $ComputerName -Config $Config } 
            } 
            Write-Message "Executed on localhost: $env:computername" 
        } 
    } 
    'Set 2' 
    { 
        if ($PSBoundParameters['Credential']) { $UseCred = $Credential } 
        else { $UseCred = Get-Credential -Credential $UserName } 
         
        Write-output "$File is a file" 
         
        $computers = Get-Content -Path $File 
        foreach ($Computer in $computers) 
        { 
            switch ($DeploymentType) 
            { 
                'Install' { Install-BGInfo -ComputerName $ComputerName -Credential $UseCred -Config $Config } 
                'Uninstall' { Uninstall-BGInfo -ComputerName $ComputerName -Credential $UseCred -Config $Config } 
            } 
            Write-Message "Executed on remote host: $Computer" 
        } 
    } 
} 
Write-Message "Powershell script execution time taken: $((Get-Date).Subtract($start_time).TotalSeconds) second(s)"




The Old Script PSBgInfo.ps1

PowerShell
#=====================================================================================================  
# Powershell -- Created with Windows PowerShell ISE 
# NAME:  PSBgInfo.ps1 
# AUTHOR: Long Truong 
# DATE  : 20-02-2016 
# Version : 1.0.0.0       
# COMMENT: Script to install/config/config BgInfo    
 
# Requirement: 
# Windows Management Framework 4.0 https://www.microsoft.com/en-us/download/details.aspx?id=40855 
#=====================================================================================================  
Param( 
    [Parameter(Mandatory=$false)] 
    [ValidateSet('Install','Uninstall')] 
    [string]$DeploymentType = 'Install', 
    [Parameter(Mandatory=$false)] 
    [string]$Config = 'default' 
) 
 
    $start_time = Get-Date 
    $url = "https://download.sysinternals.com/files/BGInfo.zip" 
    $instfile = "$env:temp\BGInfo.zip" 
    $exec_bginfo = "$instfolder\Bginfo.exe" 
    $instfolder = "c:\BGinfo" 
 
function sMessage{ 
    param([string]$sMessage) 
    Write-Output "$(Get-Date -Format "dd-MM-yy HH:mm:ss") # $sMessage"} 
 
function Unzip{ 
     param([string]$zipfile, [string]$outpath) 
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile$outpath)} 
 
Add-Type -AssemblyName "System.IO.Compression.FileSystem" 
 
If ($deploymentType -ine 'Uninstall'){ 
    sMessage "DeploymentType: $DeploymentType Config: $Config" 
    if (Test-Path -Path $instfolder\Bginfo.exe) {sMessage "$instfolder\Bginfo.exe exist"} 
    Else { 
    if (Test-Path -Path $instfile) {sMessage "$env:temp\BGInfo.zip exist" 
    sMessage "Extracting BGInfo to: $instfolder" 
    Unzip "$instfile" "$instfolder" 
    } 
    Else {sMessage "Downloading BGInfo.zip to $env:temp" 
    Import-Module BitsTransfer   
    Start-BitsTransfer -Source $url -Destination $instfile 
    sMessage "Extracting BGInfo to: $instfolder" 
    Unzip "$instfile" "$instfolder"}}  
 
    if (Test-Path -Path $PSScriptRoot\$config.bgi) { 
    sMessage "Copy config: '$config.bgi' to '$instfolder\' folder" 
    Copy-Item -Path $PSScriptRoot\$config.bgi -Destination $instfolder\$config.bgi} 
    Else { 
    sMessage "Config: '$config.bgi' does not exist in '$PSScriptRoot\' check folder!!!" 
    $config = "default" 
 
    if (Test-Path -Path $PSScriptRoot\$config.bgi){ 
    sMessage "Using default config: 'default.bgi'" 
    sMessage "Copy config: '$config.bgi' to '$instfolder\' folder" 
    Copy-Item -Path $PSScriptRoot\$config.bgi -Destination $instfolder\default.bgi} 
    Else { 
    sMessage "Config: '$config.bgi' does not exist in '$PSScriptRoot\' check folder!!!"}} 
    sMessage "$instfolder\Bginfo.exe $instfolder\$config.bgi /timer:00 /accepteula /silent" 
    & $instfolder\Bginfo.exe "$instfolder\$config.bgi" /timer:00 /accepteula /silent 
    sMessage "Creating regedit Key for Startup - HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\BgInfo" 
    New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo -Value "$instfolder\Bginfo.exe ""$instfolder\$config.bgi"" ""/timer:00 /accepteula /silent""" -PropertyType "String" -Force 
 
} 
ElseIf ($deploymentType -ieq 'Uninstall'){ 
    sMessage "DeploymentType: $DeploymentType Config: $Config" 
    sMessage "Restoring wallpaper" 
    & $instfolder\Bginfo.exe "$PSScriptRoot\restore.bgi" /timer:00 /accepteula /silent   
    sMessage "Removing regedit Key for Startup - HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\BgInfo" 
    remove-itemproperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo 
    Start-Sleep -m 1000 
    sMessage "Removing folder: $instfolder" 
    Remove-Item "$instfolder" -recurse 
} 
 
sMessage "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"  
 
 
 
fonte: https://gallery.technet.microsoft.com/scriptcenter/PSBginfo-BgInfo-powershell-199249c7 

0 comentários: