1: #===================================================================================
2: # Update-SysInternals.ps1
3: #===================================================================================
4: param(
5: [string]$tools_dir = $null,
6: [string[]]$commands = $null
7: );
8:
9: if ( !$tools_dir ) { $tools_dir = (Get-Location).Path; }
10: $script:TOOLSDIR = $tools_dir;
11:
12: #-----------------------------------------------------------------------------------
13: # function Download-Command
14: #-----------------------------------------------------------------------------------
15: function Download-Command()
16: {
17: param([string]$command = $null);
18: if ( $command )
19: {
20: $url = "http://live.sysinternals.com/$command";
21: $target = "$($script:TOOLSDIR)\$command";
22:
23: $wc = New-Object System.Net.WebClient
24: $wc.DownloadFile($url, $target);
25: $time = ([DateTime]::Now).ToLongTimeString();
26: $fi = Get-ChildItem $target -ErrorAction SilentlyContinue
27: if ( $fi )
28: {
29: $len = $fi.Length;
30:
31: "$time URL:$url [$len/$len] -> ""$target""";
32: }
33: }
34: }
35:
36: #-----------------------------------------------------------------------------------
37: # function Update-Command
38: #-----------------------------------------------------------------------------------
39: function Update-Command()
40: {
41: param([string]$command);
42: if ( $command )
43: {
44: pushd $script:TOOLSDIR;
45: $full_command = "$($script:TOOLSDIR)\$command";
46:
47: if ( [System.IO.File]::Exists($full_command) )
48: {
49: Write-Host "Renaming $full_command to ${full_command}.bak...";
50: # Rename file
51: Copy-Item $command "${command}.bak"
52: Remove-Item $command;
53: }
54:
55: Download-Command $command;
56:
57: popd;
58: }
59: }
60:
61: #-----------------------------------------------------------------------------------
62: # function Update-Commands
63: #-----------------------------------------------------------------------------------
64: function Update-Commands()
65: {
66: param([string[]]$commands);
67: if ( $commands )
68: {
69: foreach ($command in $commands)
70: {
71: Update-Command $command;
72: }
73: }
74: }
75:
76: #-----------------------------------------------------------------------------------
77: # function Get-Command
78: #-----------------------------------------------------------------------------------
79: function Get-Commands()
80: {
81: $commands = @();
82:
83: Write-Host "Querying commands from live.sysinternals.com...";
84:
85: $wc = New-Object System.Net.WebClient;
86: $site = $wc.DownloadString('http://live.sysinternals.com');
87: [regex]$re = ">[a-zA-Z0-9._-]*</A>";
88: $found_matches = $re.Matches($site);
89: foreach ($found_match in $found_matches)
90: {
91: $cmd = $found_match.Value.Substring(1, $found_match.Value.Length-5);
92: $do_download = $false;
93: switch -wildcard ($cmd.ToLower())
94: {
95: "*.exe" { $do_download = $true; }
96: "*.dll" { $do_download = $true; }
97: "*.chm" { $do_download = $true; }
98: "*.hlp" { $do_download = $true; }
99: "*.sys" { $do_download = $true; }
100: }
101: if ( $do_download )
102: {
103: $commands += @($cmd);
104: }
105: }
106: $commands;
107: }
108:
109: #-----------------------------------------------------------------------------------
110: # Main program logic
111: #-----------------------------------------------------------------------------------
112: if ( ![System.IO.Directory]::Exists($script:TOOLSDIR) )
113: {
114: mkdir $script:TOOLSDIR;
115: }
116:
117: if ( $null -eq $commands )
118: {
119: $commands = Get-Commands;
120: }
121:
122: Update-Commands $commands;