Descargar SysInternals con PowerShell
#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 scriptMi versión requiere PowerShell 3.0. Debe tener el servicio WebClient ejecutándose para poder listar los archivos de Internet. Mi script tiene código para iniciar y detener el servicio según sea necesario. Aunque si detecta que el servicio ya se está ejecutando, el script no lo detendrá bajo el supuesto de que probablemente lo tenía ejecutándose por algún motivo.
La lógica detrás del script es bastante simple: si la fecha de la versión en línea es mayor que la de la versión local, copie el archivo. Sin embargo, una cosa con la que me encontré es que puede haber una discrepancia con las marcas de tiempo debido a las zonas horarias y/o al horario de verano. No pude encontrar una manera fácil de tener en cuenta esas cosas, así que opté por simplemente probar la fecha e ignorar la hora.
Lo siguiente que debo hacer es configurar un trabajo programado de PowerShell para ejecutar el script mensualmente. Espero que me cuentes cómo te funciona esto.
