- <#
- .SYNOPSIS
- MSDN Sample of Try/Catch/Finally in PowerShell
- .DESCRIPTION
- This script opens a file gets 1st 10 characters. The idea
- is to show more detail on try/catch/finally with PowerShell
- .NOTES
- File Name : show-trycatchfinally2.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- Also needs c:\foo\gd.txt to exist.
- .LINK
- Script posted at:
- http://pshscripts.blogspot.com/2009/01/show-trycatchfinally2ps1.html
- MSDN Sample
- http://msdn.microsoft.com/en-us/library/dszsf989.aspx
- .EXAMPLE
- PSH [C:\foo]: .Show-TryCatchFinally2.PS1'
- First 10 characters:
- N
- a
- m
- e
- -
- -
- -
- #>
- ###
- # Start of Script
- ##
- # Setup and open a file
- $path = "c:\foo\gd.txt"
- $file = new-object System.IO.StreamReader $path
- $buffer = new-object char[] 10
- # now try and read the file
- try {
- [void] $file.ReadBlock($buffer, $index, $buffer.Length)
- "First {0} characters:" -f $buffer.length
- $buffer
- }
- # catch IO exceptions
- catch [System.IO.IOException] {
- "Error reading from {0}. Message = {1}" -f $path, $Error[0].Message
- }
- #cleanup
- finally {
- if ($file) {
- $file.Close()
- }
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 25 January 2009
Show-TryCatchFinally2.ps1
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment