Sysinternals Suite是一个工具的集合,里面有很多Windows相关的工具,如:Autoruns,Process Explorer,Bginfo等工具。但是这些工具经常性的更新,如果需要更新,还需要把整个工具包下载下来,并重新覆盖,那么如何自动更新 SysInternals 工具套件,其实我们可以用powershell脚本进行定时任务,每天进行自动检查更新。脚本如下:
function Update-SysinternalsHTTP ($ToolsLocalDir = 'c:\temp\sys')
{
if (Test-Path $ToolsLocalDir){
cd $ToolsLocalDir
$DebugPreference = 'SilentlyContinue'
$wc = new-object System.Net.WebClient
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)'
$wc.Headers.Add('user-agent', $userAgent)
$ToolsUrl = 'https://live.'
$toolsBlock='
.*
'$WebPageCulture = New-Object System.Globalization.CultureInfo('en-us')
$Tools = @{}
$ToolsPage = $wc.DownloadString($ToolsUrl)
$matches=[string] $ToolsPage |select-string -pattern '$ToolsBlock' -AllMatches
foreach($match in $matches.Matches) {
$txt = ( ($match.Value -replace '
', '`r`n') -replace '<[^>]*?>','')
foreach($lines in $txt.Split('`r`n')){
$line=$lines|select-string -NotMatch -Pattern 'To Parent|^$|
'if ($line -ne $null){
$date=(([string]$line).substring(0,38)).trimstart(' ') -replace ' ',' '
$file=([string]$line).substring(52,(([string]$line).length-52))
$Tools['$file']= [datetime]::ParseExact($date,'f',$WebPageCulture)
}
}
}
$Tools.keys|
ForEach-Object {
$NeedUpdate=$false
if (Test-Path $_)
{
$SubtractSeconds = New-Object System.TimeSpan 0, 0, 0, ((dir $_).lastWriteTime).second, 0
$LocalFileDate= ( (dir $_).lastWriteTime ).Subtract( $SubtractSeconds )
$needupdate=(($tools[$_]).touniversaltime() -lt $LocalFileDate.touniversaltime())
} else {$NeedUpdate=$true}
if ( $NeedUpdate )
{
Try {
$wc.DownloadFile('$ToolsUrl/$_','$ToolsLocalDir\$_' )
$f=dir '$ToolsLocalDir\$_'
$f.lastWriteTime=($tools[$_])
'Updated $_'
}
catch { Write-debug '发生错误: $_' }
}
}
}
}
cls
'更新开始...'
Update-Sysinternalshttp -ToolsLocalDir 'E:\soft\SysinternalsSuite'
'更新结束'
[^>