ดาวน์โหลด SysInternals ด้วย 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 scriptเวอร์ชันของฉันต้องใช้ PowerShell 3.0 คุณต้องให้บริการ WebClient ทำงานเพื่อที่จะแสดงรายการไฟล์อินเทอร์เน็ต สคริปต์ของฉันมีรหัสเพื่อเริ่มและหยุดบริการตามความจำเป็น แม้ว่าจะตรวจพบว่าบริการกำลังทำงานอยู่ สคริปต์จะไม่หยุดการทำงานภายใต้สมมติฐานที่ว่าคุณน่าจะเรียกใช้บริการด้วยเหตุผลบางประการ
ตรรกะเบื้องหลังสคริปต์นั้นค่อนข้างง่าย หากวันที่ของเวอร์ชันออนไลน์มากกว่าเวอร์ชันในเครื่อง ให้คัดลอกไฟล์ สิ่งหนึ่งที่ฉันพบคืออาจมีความคลาดเคลื่อนกับการประทับเวลาเนื่องจากโซนเวลาและ/หรือเวลาออมแสง ฉันไม่สามารถหาวิธีง่ายๆ ในการพิจารณาสิ่งเหล่านั้นได้ ดังนั้นฉันจึงเลือกที่จะทดสอบวันที่และเพิกเฉยต่อเวลา
สิ่งถัดไปที่ฉันควรทำคือตั้งค่างานตามกำหนดเวลาของ PowerShell เพื่อเรียกใช้สคริปต์เป็นรายเดือน ฉันหวังว่าคุณจะแจ้งให้เราทราบว่าสิ่งนี้ได้ผลสำหรับคุณอย่างไร
