# Function: Get-IniContent
function Get-IniContent {
<#
.SYNOPSIS
Gets the content of an INI file.
.DESCRIPTION
Reads an INI file and returns its content as a hashtable.
.NOTES
Author : Oliver Lipkau
Blog : http://oliver.lipkau.net/blog/
Source : GitHub - lipkau/PsIni
Version : 1.1 - Typo fixes (Thanks SLDR, Dave Stiff)
#Requires -Version 2.0
.PARAMETER FilePath
Specifies the path to the input file.
.EXAMPLE
$FileContent = Get-IniContent "C:\myinifile.ini"
# Saves the content of the file into a hashtable.
.EXAMPLE
$inifilepath | $FileContent = Get-IniContent
# Reads an INI file passed through the pipeline.
.EXAMPLE
$FileContent["Section"]["Key"]
# Returns a specific key from a section.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Test-Path $_ -and (Get-Item $_).Extension -eq ".ini" })]
[string]$FilePath
)
begin {
Write-Verbose "Starting function $($MyInvocation.MyCommand.Name)"
}
process {
Write-Verbose "Processing file: $FilePath"
$ini = @{}
$section = $null
$CommentCount = 0
switch -regex -file $FilePath {
"^\[(.+)\]$" {
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" {
if (-not $section) { $section = "No-Section"; $ini[$section] = @{} }
$ini[$section]["Comment$($CommentCount++)"] = $matches[1]
}
"(.+?)\s*=\s*(.*)" {
if (-not $section) { $section = "No-Section"; $ini[$section] = @{} }
$ini[$section][$matches[1]] = $matches[2]
}
}
Write-Verbose "Finished processing file: $FilePath"
return $ini
}
end {
Write-Verbose "Function $($MyInvocation.MyCommand.Name) completed"
}
}
# Function: Get-WebClient
function Get-WebClient {
$wc = New-Object Net.WebClient
$wc.UseDefaultCredentials = $true
$wc.Proxy.Credentials = $wc.Credentials
return $wc
}
# Function: DownloadFile
function Download-File {
param(
[string]$Url,
[string]$TargetFile
)
$request = [System.Net.WebRequest]::Create($Url)
$request.Timeout = 15000
$response = $request.GetResponse()
if (-not $response) { return 1 }
$stream = $response.GetResponseStream()
$fileStream = New-Object IO.FileStream($TargetFile, 'Create')
$buffer = New-Object byte[] 64KB
$bytesRead = $stream.Read($buffer, 0, $buffer.Length)
while ($bytesRead -gt 0) {
$fileStream.Write($buffer, 0, $bytesRead)
$bytesRead = $stream.Read($buffer, 0, $buffer.Length)
}
$fileStream.Close()
$stream.Close()
$response.Close()
return 0
}
# Main script
$clmid = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Cryptography' -Name MachineGuid).MachineGuid
$hash = (New-Object Security.Cryptography.MD5CryptoServiceProvider).ComputeHash([Text.Encoding]::UTF8.GetBytes($clmid))
$hash = ([BitConverter]::ToString($hash)).ToLower() -replace '[-]', ''
$rkey = 'HKCU:\SOFTWARE\GNU\XviD'
$pf = (Get-ItemProperty -Path $rkey -Name PerfCount).PerfCount
Set-ItemProperty -Path $rkey -Name PerfCount -Value 0
$scriptPath = $MyInvocation.MyCommand.Path
$xvidDir = Split-Path $scriptPath
$tmpDir = $env:TEMP
# Get update.ini content
$FileContent = Get-IniContent "$xvidDir\update.ini"
if (-not $FileContent) { exit 0 }
$url = $FileContent["Update"]["url"] + "&p=$pf&h=$hash"
$localPath = "$tmpDir\update.xml"
$webClient = Get-WebClient
$webClient.DownloadFile($url, $localPath)
# Parse the XML
$xml = [xml](Get-Content $localPath)
$localVersion = $FileContent["Update"]["version_id"]
if ($xml.installerInformation.versionId -le $localVersion) { exit 0 }
# UI and further logic...