PowerShell script to automate BGInfo update every hour

 # PowerShell script to automate BGInfo update every hour


# Variables

$sourcePath = "\\win1\c$\Windows\SYSVOL\sysvol\apex.local\scripts"  # Example path "\\YourServer\SharedFolder"

$destinationPath = "C:\ProgramData\Packages\bginfo"  # Example path "C:\LocalFolder"

$bgInfoExecutable = "Bginfo64.exe"

$configFile = "config.bgi"


# Function to copy files if they are updated

function Update-Files {

    $sourceFiles = Get-ChildItem -Path $sourcePath

    foreach ($file in $sourceFiles) {

        $localFile = Join-Path -Path $destinationPath -ChildPath $file.Name

        if (Test-Path $localFile) {

            $localFileInfo = Get-Item $localFile

            if ($localFileInfo.LastWriteTime -lt $file.LastWriteTime) {

                Copy-Item -Path $file.FullName -Destination $localFile -Force

            }

        } else {

            Copy-Item -Path $file.FullName -Destination $localFile

        }

    }

}


# Function to schedule BGInfo update

function Schedule-BGInfoUpdate {

    $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -WindowStyle Hidden -command `"$destinationPath\$bgInfoExecutable $destinationPath\$configFile /silent /nolicprompt /timer:0`""

    $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddHours(1)

    Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "BGInfoUpdate" -Description "Updates BGInfo every hour" -RunLevel Highest -User "SYSTEM"

}


# Check and copy files

Update-Files


# Schedule BGInfo update

Schedule-BGInfoUpdate

Comments

Popular posts from this blog

Clearing DNS cache on vCenter Server Appliance

Start and stop services in vCenter Server Appliance

How to step by step in detail install ssh on cachyos