Parent Directory | Revision Log
Revision 2142 - (view) (download)
1 : | Isibaar | 2110 | Function Get-IniContent { |
2 : | <# | ||
3 : | .Synopsis | ||
4 : | Gets the content of an INI file | ||
5 : | |||
6 : | .Description | ||
7 : | Gets the content of an INI file and returns it as a hashtable | ||
8 : | |||
9 : | .Notes | ||
10 : | Author : Oliver Lipkau <oliver@lipkau.net> | ||
11 : | Blog : http://oliver.lipkau.net/blog/ | ||
12 : | Source : https://github.com/lipkau/PsIni | ||
13 : | http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91 | ||
14 : | Version : 1.0 - 2010/03/12 - Initial release | ||
15 : | 1.1 - 2014/12/11 - Typo (Thx SLDR) | ||
16 : | Typo (Thx Dave Stiff) | ||
17 : | |||
18 : | #Requires -Version 2.0 | ||
19 : | |||
20 : | .Inputs | ||
21 : | System.String | ||
22 : | |||
23 : | .Outputs | ||
24 : | System.Collections.Hashtable | ||
25 : | |||
26 : | .Parameter FilePath | ||
27 : | Specifies the path to the input file. | ||
28 : | |||
29 : | .Example | ||
30 : | $FileContent = Get-IniContent "C:\myinifile.ini" | ||
31 : | ----------- | ||
32 : | Description | ||
33 : | Saves the content of the c:\myinifile.ini in a hashtable called $FileContent | ||
34 : | |||
35 : | .Example | ||
36 : | $inifilepath | $FileContent = Get-IniContent | ||
37 : | ----------- | ||
38 : | Description | ||
39 : | Gets the content of the ini file passed through the pipe into a hashtable called $FileContent | ||
40 : | |||
41 : | .Example | ||
42 : | C:\PS>$FileContent = Get-IniContent "c:\settings.ini" | ||
43 : | C:\PS>$FileContent["Section"]["Key"] | ||
44 : | ----------- | ||
45 : | Description | ||
46 : | Returns the key "Key" of the section "Section" from the C:\settings.ini file | ||
47 : | |||
48 : | .Link | ||
49 : | Out-IniFile | ||
50 : | #> | ||
51 : | |||
52 : | [CmdletBinding()] | ||
53 : | Param( | ||
54 : | [ValidateNotNullOrEmpty()] | ||
55 : | [ValidateScript({(Test-Path $_) -and ((Get-Item $_).Extension -eq ".ini")})] | ||
56 : | [Parameter(ValueFromPipeline=$True,Mandatory=$True)] | ||
57 : | [string]$FilePath | ||
58 : | ) | ||
59 : | |||
60 : | Begin | ||
61 : | {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function started"} | ||
62 : | |||
63 : | Process | ||
64 : | { | ||
65 : | Write-Verbose "$($MyInvocation.MyCommand.Name):: Processing file: $Filepath" | ||
66 : | |||
67 : | $ini = @{} | ||
68 : | switch -regex -file $FilePath | ||
69 : | { | ||
70 : | "^\[(.+)\]$" # Section | ||
71 : | { | ||
72 : | $section = $matches[1] | ||
73 : | $ini[$section] = @{} | ||
74 : | $CommentCount = 0 | ||
75 : | } | ||
76 : | "^(;.*)$" # Comment | ||
77 : | { | ||
78 : | if (!($section)) | ||
79 : | { | ||
80 : | $section = "No-Section" | ||
81 : | $ini[$section] = @{} | ||
82 : | } | ||
83 : | $value = $matches[1] | ||
84 : | $CommentCount = $CommentCount + 1 | ||
85 : | $name = "Comment" + $CommentCount | ||
86 : | $ini[$section][$name] = $value | ||
87 : | } | ||
88 : | "(.+?)\s*=\s*(.*)" # Key | ||
89 : | { | ||
90 : | if (!($section)) | ||
91 : | { | ||
92 : | $section = "No-Section" | ||
93 : | $ini[$section] = @{} | ||
94 : | } | ||
95 : | $name,$value = $matches[1..2] | ||
96 : | $ini[$section][$name] = $value | ||
97 : | } | ||
98 : | } | ||
99 : | Write-Verbose "$($MyInvocation.MyCommand.Name):: Finished Processing file: $FilePath" | ||
100 : | Return $ini | ||
101 : | } | ||
102 : | |||
103 : | End | ||
104 : | {Write-Verbose "$($MyInvocation.MyCommand.Name):: Function ended"} | ||
105 : | } | ||
106 : | |||
107 : | function Get-WebClient | ||
108 : | { | ||
109 : | $wc = New-Object Net.WebClient | ||
110 : | $wc.UseDefaultCredentials = $true | ||
111 : | $wc.Proxy.Credentials = $wc.Credentials | ||
112 : | $wc | ||
113 : | } | ||
114 : | |||
115 : | # http://blogs.msdn.com/b/jasonn/archive/2008/06/13/downloading-files-from-the-internet-in-powershell-with-progress.aspx | ||
116 : | function downloadFile($my_url, $targetFile) | ||
117 : | { | ||
118 : | $ret = 0 | ||
119 : | $request = [System.Net.WebRequest]::Create($my_url) | ||
120 : | $request.set_Timeout(15000) #15 second timeout | ||
121 : | $response = $request.GetResponse() | ||
122 : | If (!$response) { | ||
123 : | return 1 | ||
124 : | } | ||
125 : | |||
126 : | $totalLength = [System.Math]::Floor($response.get_ContentLength() / 1000) | ||
127 : | $responseStream = $response.GetResponseStream() | ||
128 : | $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create | ||
129 : | $progressBar.Value = 0 | ||
130 : | $buffer = new-object byte[] 64KB | ||
131 : | $count = $responseStream.Read($buffer, 0, $buffer.length) | ||
132 : | $downloadedBytes = $count | ||
133 : | |||
134 : | while ($count -gt 0) | ||
135 : | { | ||
136 : | $progressBar.Value = [System.Math]::Floor([System.Math]::Floor($downloadedBytes/10) / $totalLength) | ||
137 : | $targetStream.Write($buffer, 0, $count) | ||
138 : | $count = $responseStream.Read($buffer, 0, $buffer.length) | ||
139 : | $downloadedBytes = $downloadedBytes + $count | ||
140 : | $form.Refresh() | ||
141 : | [System.Windows.Forms.Application]::DoEvents() | ||
142 : | |||
143 : | if($script:canceledDownload -eq $true) | ||
144 : | { | ||
145 : | # Exit the loop | ||
146 : | $ret = 1 | ||
147 : | break | ||
148 : | } | ||
149 : | |||
150 : | start-sleep -Milliseconds 100 | ||
151 : | } | ||
152 : | |||
153 : | $targetStream.Flush() | ||
154 : | $targetStream.Close() | ||
155 : | $targetStream.Dispose() | ||
156 : | $responseStream.Dispose() | ||
157 : | $response.Close() | ||
158 : | |||
159 : | return $ret | ||
160 : | } | ||
161 : | |||
162 : | |||
163 : | # Main | ||
164 : | |||
165 : | Isibaar | 2120 | $clmid = 'HKLM:\SOFTWARE\Microsoft\Cryptography' |
166 : | $clmid = (Get-ItemProperty -Path $clmid -Name MachineGuid).MachineGuid | ||
167 : | $clmid = $clmid -replace '[-]','' | ||
168 : | $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider | ||
169 : | $utf8 = [system.Text.Encoding]::UTF8 | ||
170 : | $hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($clmid))) | ||
171 : | $hash = $hash.ToLower() -replace '[-]','' | ||
172 : | $rkey = 'HKCU:\SOFTWARE\GNU\XviD' | ||
173 : | $pf = (Get-ItemProperty -Path $rkey -Name PerfCount).PerfCount | ||
174 : | Isibaar | 2110 | $scriptpath = $MyInvocation.MyCommand.Path |
175 : | $script:xvid_dir = Split-Path $scriptpath | ||
176 : | $tmp = $env:temp | ||
177 : | |||
178 : | $FileContent = Get-IniContent "$script:xvid_dir\update.ini" | ||
179 : | If (!$FileContent) { | ||
180 : | Exit 0 | ||
181 : | } | ||
182 : | |||
183 : | $url = $FileContent["Update"]["url"] | ||
184 : | Isibaar | 2142 | $url = $url + "&h=" + $hash |
185 : | Isibaar | 2110 | $object = Get-WebClient |
186 : | $localPath = “$tmp\update.xml” | ||
187 : | $object.DownloadFile($url, $localPath) | ||
188 : | |||
189 : | $xml = [xml](Get-Content $localPath) | ||
190 : | |||
191 : | $script:local_xvid_version = $FileContent["Update"]["version_id"] | ||
192 : | |||
193 : | # Now check if Update file from the server has a newer version than we have locally | ||
194 : | If (!$xml -or $xml.installerInformation.versionId -le $script:local_xvid_version) { | ||
195 : | Exit 0 | ||
196 : | } | ||
197 : | |||
198 : | # Check if the current update is skipped by user-choice | ||
199 : | If (test-path $env:userprofile\.xvid\skip_update.log) { | ||
200 : | $skip_str = Get-Content $env:userprofile\.xvid\skip_update.log | ||
201 : | If ($skip_str -and $xml.installerInformation.versionId -le [System.Decimal]::Parse($skip_str)) { | ||
202 : | Exit 0 | ||
203 : | } | ||
204 : | } | ||
205 : | |||
206 : | $script:remote_xvid_version = $xml.installerInformation.versionId | ||
207 : | |||
208 : | # Parse the xml child elements | ||
209 : | $xml_root = $xml.get_DocumentElement() | ||
210 : | $fileList = $xml_root.platformFileList | ||
211 : | $serverList = $xml_root.downloadLocationList | ||
212 : | |||
213 : | $newItem = $null | ||
214 : | foreach($newItem in $fileList.ChildNodes) | ||
215 : | { | ||
216 : | $script:newInstallerFile = $newItem.filename | ||
217 : | $platform = $newItem.platform | ||
218 : | |||
219 : | If ($platform -eq "windows" -or $platform -eq "Windows") { | ||
220 : | Break | ||
221 : | } | ||
222 : | } | ||
223 : | |||
224 : | $newItem = $null | ||
225 : | foreach($newItem in $serverList.ChildNodes) | ||
226 : | { | ||
227 : | $script:newDownloadURL = $newItem.url | ||
228 : | Break # Go with the first always | ||
229 : | } | ||
230 : | |||
231 : | If (!$script:newInstallerFile -or !$script:newDownloadURL) { | ||
232 : | Exit 0 | ||
233 : | } | ||
234 : | |||
235 : | # We have an Update! Show the Dialogs... | ||
236 : | |||
237 : | # Loosely based on http://www.vistax64.com/powershell/202216-display-image-powershell.html | ||
238 : | [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | ||
239 : | |||
240 : | $xvid_file = (get-item "$script:xvid_dir\xvid.png") | ||
241 : | $xvid_img = [System.Drawing.Image]::Fromfile($xvid_file); | ||
242 : | |||
243 : | # This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274 | ||
244 : | [System.Windows.Forms.Application]::EnableVisualStyles(); | ||
245 : | $form = new-object Windows.Forms.Form | ||
246 : | $form.Text = "Xvid Software Update" | ||
247 : | $form.Size = New-Object System.Drawing.Size(640,480) | ||
248 : | $form.StartPosition = "CenterScreen" | ||
249 : | $form.FormBorderStyle = 'Fixed3D' | ||
250 : | $form.MaximizeBox = $false | ||
251 : | $form.MinimizeBox = $False | ||
252 : | $form.WindowState = "Normal" | ||
253 : | |||
254 : | $Icon = New-Object system.drawing.icon ("$script:xvid_dir\XVID.ICO") | ||
255 : | $Form.Icon = $Icon | ||
256 : | |||
257 : | $pictureBox = new-object Windows.Forms.PictureBox | ||
258 : | $pictureBox.Width = $xvid_img.Size.Width; | ||
259 : | $pictureBox.Height = $xvid_img.Size.Height; | ||
260 : | $pictureBox.Location = New-Object System.Drawing.Size(20, 12) | ||
261 : | |||
262 : | $pictureBox.Image = $xvid_img; | ||
263 : | $form.controls.add($pictureBox) | ||
264 : | |||
265 : | $label1 = New-Object System.Windows.Forms.Label | ||
266 : | $label1.Text = "A new version of the Xvid software is available!" | ||
267 : | If ($xml.installerInformation.mainScreenHeader) { | ||
268 : | $label1.Text = $xml.installerInformation.mainScreenHeader | ||
269 : | } | ||
270 : | $label1.AutoSize = $True | ||
271 : | $label1.ForeColor = "#0c3384" | ||
272 : | $label1.Location = New-Object System.Drawing.Size(96, 12) | ||
273 : | $font = New-Object System.Drawing.Font("Arial",14,[System.Drawing.FontStyle]::Regular) | ||
274 : | $label1.Font = $font | ||
275 : | $form.Controls.Add($label1) | ||
276 : | |||
277 : | $label2 = New-Object System.Windows.Forms.Label | ||
278 : | $new_version = $xml.installerInformation.version | ||
279 : | $label2.Text = "We're proud to announce the new Xvid $new_version release." | ||
280 : | If ($xml.installerInformation.mainScreenText) { | ||
281 : | $label2.Text = $xml.installerInformation.mainScreenText | ||
282 : | } | ||
283 : | $label2.AutoSize = $True | ||
284 : | $label2.Location = New-Object System.Drawing.Size(96, 40) | ||
285 : | $font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Regular) | ||
286 : | $label2.Font = $font | ||
287 : | $form.Controls.Add($label2) | ||
288 : | |||
289 : | $pictureBox = new-object Windows.Forms.PictureBox | ||
290 : | $pictureBox.Width = 500; | ||
291 : | $pictureBox.Height = 300; | ||
292 : | $pictureBox.Location = New-Object System.Drawing.Size(96, 72) | ||
293 : | |||
294 : | $script:download_banner_link = $xml.installerInformation.downloadBannerLink | ||
295 : | $script:banner_link = $xml.installerInformation.mainBannerLink | ||
296 : | $pictureBox.Add_Click({ If ($script:banner_link) { Invoke-Expression “cmd.exe /C start $script:banner_link”} }) | ||
297 : | |||
298 : | $script:download_banner_url = $xml.installerInformation.downloadBannerURI | ||
299 : | $script:error_banner_url = $xml.installerInformation.errorBannerURI | ||
300 : | $main_banner_url = $xml.installerInformation.mainBannerURI | ||
301 : | |||
302 : | If ($main_banner_url) { | ||
303 : | $object = Get-WebClient | ||
304 : | $localPath = “$tmp\xvid_main_banner.png” | ||
305 : | $object.DownloadFile($main_banner_url, $localPath) | ||
306 : | $update_file = (get-item "$tmp\xvid_main_banner.png") | ||
307 : | $main_banner = [System.Drawing.Image]::Fromfile($update_file); | ||
308 : | $pictureBox.Image = $main_banner; | ||
309 : | } | ||
310 : | $form.controls.add($pictureBox) | ||
311 : | |||
312 : | $SkipButton = New-Object System.Windows.Forms.Button | ||
313 : | $SkipButton.Text = "Skip this version" | ||
314 : | $LaterButton = New-Object System.Windows.Forms.Button | ||
315 : | $LaterButton.Text = "Remind me later" | ||
316 : | $InstallButton = New-Object System.Windows.Forms.Button | ||
317 : | $InstallButton.Text = "Install update" | ||
318 : | |||
319 : | $SkipButton.Location = New-Object System.Drawing.Size(96, 400) | ||
320 : | $SkipButton.Size = New-Object System.Drawing.Size(100, 25) | ||
321 : | $form.controls.add($SkipButton) | ||
322 : | |||
323 : | $LaterButton.Location = New-Object System.Drawing.Size(380, 400) | ||
324 : | $LaterButton.Size = New-Object System.Drawing.Size(100, 25) | ||
325 : | $form.controls.add($LaterButton) | ||
326 : | |||
327 : | $InstallButton.Location = New-Object System.Drawing.Size(496, 400) | ||
328 : | $InstallButton.Size = New-Object System.Drawing.Size(100, 25) | ||
329 : | $form.controls.add($InstallButton) | ||
330 : | |||
331 : | $SkipButton.Add_Click( | ||
332 : | { | ||
333 : | New-Item -ItemType Directory -Force -Path $env:userprofile\.xvid | ||
334 : | "$script:remote_xvid_version" | Out-File $env:userprofile\.xvid\skip_update.log | ||
335 : | $form.Close() | ||
336 : | } | ||
337 : | ) | ||
338 : | |||
339 : | $LaterButton.Add_Click( | ||
340 : | { | ||
341 : | If ($script:downloadSuccessful -eq 0 -or $script:canceledDownload -eq $true) { | ||
342 : | $form.Close() | ||
343 : | } | ||
344 : | |||
345 : | $script:canceledDownload = $true; | ||
346 : | } | ||
347 : | ) | ||
348 : | |||
349 : | $InstallButton.Add_Click( | ||
350 : | { | ||
351 : | $label1.Text = "Preparing the Xvid Update" | ||
352 : | $label2.Text = "Please stay tuned while the update is downloading..." | ||
353 : | $form.Refresh() | ||
354 : | $LaterButton.Location = New-Object System.Drawing.Size(496, 400) | ||
355 : | $LaterButton.Text = "Cancel" | ||
356 : | If ($script:download_banner_link) { | ||
357 : | $script:banner_link = $script:download_banner_link | ||
358 : | } | ||
359 : | else { | ||
360 : | $script:banner_link = $null | ||
361 : | } | ||
362 : | If ($script:download_banner_url) { | ||
363 : | $object = Get-WebClient | ||
364 : | $localPath = “$tmp\xvid_download_banner.png” | ||
365 : | $object.DownloadFile($script:download_banner_url, $localPath) | ||
366 : | $download_img = (get-item "$tmp\xvid_download_banner.png") | ||
367 : | $download_banner = [System.Drawing.Image]::Fromfile($download_img); | ||
368 : | $pictureBox.Image = $download_banner; | ||
369 : | } | ||
370 : | else { | ||
371 : | $pictureBox.Image = $null | ||
372 : | } | ||
373 : | $pictureBox.Height = 220; | ||
374 : | $form.controls.remove($InstallButton) | ||
375 : | $form.controls.remove($SkipButton) | ||
376 : | |||
377 : | $progressBar = New-Object System.Windows.Forms.ProgressBar | ||
378 : | $progressBar.Name = 'progressBar00' | ||
379 : | $progressBar.Value = 0 | ||
380 : | $progressBar.Style="Continuous" | ||
381 : | $progressBar.Size = New-Object System.Drawing.Size(500, 30) | ||
382 : | $progressBar.Left = 96 | ||
383 : | $progressBar.Top = 330 | ||
384 : | $form.Controls.Add($progressBar) | ||
385 : | $labelDownload = New-Object System.Windows.Forms.Label | ||
386 : | $labelDownload.Text = "Downloading $script:newInstallerFile..." | ||
387 : | $labelDownload.AutoSize = $True | ||
388 : | $labelDownload.Location = New-Object System.Drawing.Size(96, 310) | ||
389 : | $form.Controls.Add($labelDownload) | ||
390 : | $label0 = New-Object System.Windows.Forms.Label | ||
391 : | $label0.Text = "0%" | ||
392 : | $label0.AutoSize = $True | ||
393 : | $label0.Location = New-Object System.Drawing.Size(96, 368) | ||
394 : | $form.Controls.Add($label0) | ||
395 : | $label100 = New-Object System.Windows.Forms.Label | ||
396 : | $label100.Text = "100%" | ||
397 : | $label100.AutoSize = $True | ||
398 : | $label100.Location = New-Object System.Drawing.Size(564, 368) | ||
399 : | $form.Controls.Add($label100) | ||
400 : | $form.Refresh() | ||
401 : | |||
402 : | $script:downloadSuccessful = 1 | ||
403 : | $downloadUrl = "$script:newDownloadURL$script:newInstallerFile" | ||
404 : | $ret = (downloadFile "$downloadUrl" “$tmp\$script:newInstallerFile”) | ||
405 : | |||
406 : | If ($ret -ne 0) { | ||
407 : | If ($script:canceledDownload -eq $false) { | ||
408 : | $label1.Text = "Download Error!" | ||
409 : | $label2.Text = "We're sorry but the download failed. Please try again later." | ||
410 : | } | ||
411 : | else { | ||
412 : | $label1.Text = "Update Canceled!" | ||
413 : | $label2.Text = "We've stopped the update at your request. You may try again later." | ||
414 : | } | ||
415 : | |||
416 : | $LaterButton.Text = "OK" | ||
417 : | $form.controls.remove($progressBar) | ||
418 : | $form.controls.remove($labelDownload) | ||
419 : | $form.controls.remove($label0) | ||
420 : | $form.controls.remove($label100) | ||
421 : | |||
422 : | If ($script:error_banner_url) { | ||
423 : | $object = Get-WebClient | ||
424 : | $localPath = “$tmp\xvid_error_banner.png” | ||
425 : | $object.DownloadFile($script:error_banner_url, $localPath) | ||
426 : | $error_img = (get-item "$tmp\xvid_error_banner.png") | ||
427 : | $error_banner = [System.Drawing.Image]::Fromfile($error_img) | ||
428 : | $pictureBox.Image = $error_banner | ||
429 : | } | ||
430 : | else { | ||
431 : | $pictureBox.Image = $null | ||
432 : | } | ||
433 : | $script:banner_link = $null | ||
434 : | $pictureBox.Height = 300; | ||
435 : | $form.Refresh() | ||
436 : | } | ||
437 : | else { | ||
438 : | $script:downloadSuccessful = 2 | ||
439 : | $form.Close(); | ||
440 : | } | ||
441 : | }) | ||
442 : | |||
443 : | $form.Add_Closing({$script:canceledDownload = $true}) | ||
444 : | |||
445 : | $script:downloadSuccessful = 0 | ||
446 : | $script:canceledDownload = $false | ||
447 : | |||
448 : | $form.Add_Shown( { $form.Activate(); $InstallButton.focus() } ) | ||
449 : | $ret = $form.ShowDialog() | ||
450 : | |||
451 : | if ($script:downloadSuccessful -eq 2) { | ||
452 : | $cmd="$tmp\$script:newInstallerFile" | ||
453 : | Try { | ||
454 : | $key = 'HKCU:\SOFTWARE\GNU\XviD' | ||
455 : | $key = (Get-ItemProperty -Path $key -Name Supported_4CC).Supported_4CC | ||
456 : | |||
457 : | $other = $divx = $3ivx = 0 | ||
458 : | If ($key -band 4) { | ||
459 : | $other = 1 | ||
460 : | } | ||
461 : | If ($key -band 1) { | ||
462 : | $3ivx = 1 | ||
463 : | } | ||
464 : | If ($key -band 2) { | ||
465 : | $divx = 1 | ||
466 : | } | ||
467 : | |||
468 : | If ((start-process $cmd -ArgumentList "--mode unattended --unattendedmodeui minimalWithDialogs --decode_divx $divx --decode_3ivx $3ivx --decode_other $other" -PassThru -Wait).ExitCode -ne 0) { | ||
469 : | $ret = [System.Windows.Forms.MessageBox]::Show("Unfortunately, the Xvid update failed! Please try again." , "Xvid Update Error", 16) | ||
470 : | } | ||
471 : | else { | ||
472 : | $ret = [System.Windows.Forms.MessageBox]::Show("The Xvid update got installed successfully. Have fun!" , "Xvid Update Success", 0, 64) | ||
473 : | } | ||
474 : | } | ||
475 : | Catch { | ||
476 : | $ret = [System.Windows.Forms.MessageBox]::Show("The Xvid update could not be run. Please try again." , "Xvid Update Error", 0, 16) | ||
477 : | } | ||
478 : | } | ||
479 : | Exit 1 |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |