Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $global:hosts = "C:\Windows\system32\drivers\etc\hosts"
- $global:url = "https://someonewhocares.org/hosts/hosts"
- $global:temp = "C:\temp\hosts"
- $global:logFile = "C:\_Scripts\Update_Hosts_File.log"
- Enum LogLevel {
- Info = 0
- Warning = 1
- Error = 2
- }
- function Write-Log ([string]$message, [LogLevel]$logLevel) {
- if ($null -eq $logLevel) {
- $logLevel = [LogLevel]::Info
- }
- if ($logLevel -eq [LogLevel]::Info) {
- Add-Content -Path $logFile -Value "$(Get-Date): $message"
- Write-Host $message
- }
- elseif ($logLevel -eq [LogLevel]::Warning) {
- Add-Content -Path $logFile -Value "$(Get-Date): WARNING: $message"
- Write-Host $message -ForegroundColor Yellow
- }
- elseif ($logLevel -eq [LogLevel]::Error) {
- Add-Content -Path $logFile -Value "$(Get-Date): ERROR: $message"
- Write-Host $message -ForegroundColor Red
- }
- }
- Write-Log "Script started."
- try {
- Write-Log "Downloading hosts file from URL $url..."
- Invoke-WebRequest -Uri $url -OutFile $temp
- $newHash = (Get-FileHash -Path $temp -Algorithm SHA256).Hash
- $oldHash = (Get-FileHash -Path $hosts -Algorithm SHA256).Hash
- if ($newHash -ne $oldHash) {
- Write-Log "Copying new file..."
- Move-Item -Path $temp -Destination $(Split-Path $hosts) -Force
- Write-Log "Done."
- }
- else {
- Write-Log "File is up to date."
- }
- }
- catch {
- Write-Log "Script failed: $($_.Exception.Message)" Error
- exit 1
- }
- Write-Log "Script finished."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement