Public/Install-TunerBgInfo.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#requires -RunAsAdministrator
function Install-TunerBGInfo {
    <#
    .SYNOPSIS
        Install and enable Sysinternals BGINFO
    .DESCRIPTION
        Install and enable Sysinternals BGINFO
    .PARAMETER ConfigFile
        Custom configuration file (.bgi extension) to apply
    .EXAMPLE
        Install-TunerBGInfo
    .EXAMPLE
        Install-TunerBGInfo -ConfigFile "c:\windows\system32\contoso_servers.bgi"
    #>

    [CmdletBinding(SupportsShouldProcess=$True)]
    param(
        [parameter(Mandatory=$False)]
        [string] $ConfigFile = ""
    )
    try {
        Write-Verbose "verifying bginfo.exe is installed"
        $instfolder = Join-Path -Path $env:ProgramData -ChildPath "chocolatey\lib\bginfo\tools"
        $instfile = Join-Path -Path $instfolder -ChildPath "bginfo.exe"
        if (($ConfigFile -ne "") -and (!(Test-Path $ConfigFile))) {
            Write-Warning "$ConfigFile was not found. Using default configuration."
            $ConfigFile = ""
        }
        if (!(Test-Path $instfile)) {
            Set-TunerChoco
            Write-Host "installing chocolatey package: bginfo" -ForegroundColor Magenta
            cup bginfo -y
        }
        if (Test-Path $instfile) {
            Write-Verbose "creating registry key to launch bginfo"
            New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run' -Name BgInfo -Value """$instfile"" $ConfigFile /timer:00 /accepteula /silent" -PropertyType 'String' -Force 
            & $instfile $ConfigFile /timer:00 /accepteula /silent
        }
    }
    catch {
        Write-Error "$($Error[0].Exception.Message)"
    }
}

Export-ModuleMember -Function Install-TunerBGInfo