PowerShell ile SysInternals'ı indirin


#requires -version 3.0

<#
Download Sysinternals tools from web to a local folder.

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************
#>

[cmdletbinding(SupportsShouldProcess)]

Param(
[Parameter(Position=0)]
[ValidateScript({Test-Path -Path $_})]
[string]$Destination = "G:\SysInternals"
)

#start the WebClient service if it is not running
if ((Get-Service WebClient).Status -eq 'Stopped') {
     Write-Verbose "Starting WebClient"
     #always start the webclient service even if using -Whatif
     Start-Service WebClient -WhatIf:$false
     $Stopped = $True
}
else {
    <#
     Define a variable to indicate service was already running
     so that we don't stop it. Making an assumption that the
     service is already running for a reason.
    #>
    $Stopped = $False
}

#get current files in destination
$current = dir -Path $Destination -File

Write-Host "Updating Sysinternals tools from \\live.sysinternals.com\tools to $destination" -ForegroundColor Cyan

foreach ($file in $current) {
  #construct a path to the live web version and compare dates
  $online = Join-Path -path \\live.sysinternals.com\tools -ChildPath $file.name
  Write-Verbose "Testing $online"
  if ((Get-Item -Path $online).LastWriteTime.Date -ge $file.LastWriteTime.Date) {
    Copy-Item $online -Destination $Destination -PassThru
  }
}

Write-Host "Testing for online files not in $destination" -ForegroundColor Green

#test for files online but not in the destination and copy them
dir -path \\live.sysinternals.com\tools -file | 
Where {$current.name -notcontains $_.name} |
Copy-Item -Destination $Destination -PassThru

<#
alternative but this might still copy files that haven't
really changed
Robocopy \\live.sysinternals.com\tools $destination /MIR
#>

if ( $Stopped ) {
    Write-Verbose "Stopping web client"
    #always stop the service even if using -Whatif
    Stop-Service WebClient -WhatIf:$False
}

Write-Host "Sysinternals Update Complete" -ForegroundColor Cyan
#end of script

Sürümüm PowerShell 3.0 gerektiriyor. İnternet dosyalarını listelemek için WebClient hizmetinin çalışıyor olması gerekir. Komut dosyamda hizmeti gerektiği gibi başlatmak ve durdurmak için kod var. Hizmetin zaten çalıştığını tespit etse de, komut dosyası, muhtemelen onu bir nedenden dolayı çalıştırdığınız varsayımıyla hizmeti durdurmayacaktır.

Betiğin mantığı oldukça basittir, eğer çevrimiçi sürümün tarihi yerel sürümden büyükse dosyayı kopyalayın. Ancak karşılaştığım şeylerden biri, zaman dilimleri ve/veya yaz saati uygulaması nedeniyle zaman damgalarında bir tutarsızlık olabileceğidir. Bunları hesaba katmanın kolay bir yolunu bulamadım, bu yüzden sadece tarihi test etmeyi ve saati görmezden gelmeyi tercih ettim.

Yapmam gereken bir sonraki şey, betiği aylık olarak çalıştırmak için zamanlanmış bir PowerShell işi ayarlamak. Umarım bunun sizin için nasıl sonuçlanacağını bana bildirirsiniz.

İlgili makaleler