Bash
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/bin/bash currentHosts="/etc/hosts" myHostsPart="/etc/hosts.my" newHosts="http://winhelp2002.mvps.org/hosts.txt" timeStamp=$(date '+%Y-%m-%d_%H-%M') log="/var/log/hosts-file-update" cp ${currentHosts} ${currentHosts}.${timeStamp}.bak wget ${newHosts} -O ${currentHosts}.${timeStamp} wgetstat=$? if [ ${wgetstat} -eq 0 ]; then cat ${myHostsPart} > ${currentHosts} cat ${currentHosts}.${timeStamp} >> ${currentHosts} echo "${timeStamp} ok" >> $log else rm -f ${currentHosts}.${timeStamp}.bak echo "${timeStamp} error, wget err code = ${wgetstat}" >> $log fi |
Powershell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$Log = "c:\Windows\System32\drivers\etc\hosts-update.log" $newHostsURL="http://winhelp2002.mvps.org/hosts.txt" $newHosts = "c:\Windows\System32\drivers\etc\HOSTS.new" $myHostsPart = "c:\Windows\System32\drivers\etc\HOSTS.my" $Hosts = "c:\Windows\System32\drivers\etc\HOSTS" $Timestamp = $([System.DateTime]::Now.ToString('yyyy-MM-dd--HH-mm')) Copy-Item "$Hosts" "$Hosts.$Timestamp.bak" $Client = New-Object System.Net.WebClient $Client.DownloadFile( $newHostsURL, "$newHosts.$Timestamp") $downStat = $? if ($downStat) { Get-Content $myHostsPart,"$newHosts.$Timestamp" | Out-File $Hosts -Encoding utf8 "$Timestamp ok" >> $Log } else { Remove-Item "$Hosts.$Timestamp.bak" -Force "$Timestamp error" >> $Log } |
Sometimes it suits me…
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 |
param ( [Parameter(Mandatory=$true)][string]$target, [Parameter(Mandatory=$false)][int]$waitUntilNextCycle=1, [Parameter(Mandatory=$false)][bool]$displayOkStatus=$true ) $ping = New-Object System.Net.NetworkInformation.Ping function Get-Timestamp { return [System.DateTime]::Now.ToString('HH:mm:ss') } if (!$displayOkStatus) { Write-Warning "Only connection errors will be displayed!" } while ($true) { try { $res = $ping.send($target) $status = $res.Status if ($status -ilike "Success") { if ($displayOkStatus) { Write-Host "[Ok $(Get-Timestamp)]" -NoNewline -BackgroundColor DarkGreen -ForegroundColor White } } else { Write-Host "[$status $(Get-Timestamp)]" -NoNewline -BackgroundColor DarkRed -ForegroundColor White } Start-Sleep -Seconds $waitUntilNextCycle } catch { Write-Host "[DNS or network connection error $(Get-Timestamp)]" -NoNewline -BackgroundColor Black -ForegroundColor White } } |
The script executes a commands stored in a variable (array). The output is conveniently collected and stored in a log file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[string]$log = "c:\temp\log.txt" [array]$commands = @( 'if (!([System.Environment]::OSVersion.Version).Major -ge 6) {"win xp"} else {"win 7 or higher"}'; 'gps | select id,@{Name="WorkingSet(MB)";Expression={"{0:N1} M" -f($_.WorkingSet/1mb)}},name,company | sort name | ft'; 'netstat -noa -p TCP | findstr /i close' ) foreach ($command in $commands) { $command = [scriptblock]::Create($command) [array]$output = & $command $output | fl | Out-File -FilePath $log -Encoding UTF8 -Append } |