- <#
- .SYNOPSIS
- This script defines a function that tests to
- see if a file is open.
- .DESCRIPTION
- This script used the System.Io.FileStream class
- and the FileInfo class to try to open a file
- stream for write. If it fails, we return $false,
- else we close the file and return $True
- .NOTES
- File Name : Test-FileOpen.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh[Cookham8:C:\foo]> $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx
- Psh[Cookham8:C:\foo]>Test-FileOpen $file
- True
- #>
- Function Test-FileOpen {
- Param (
- $fileName = $(Throw '***** No File specified')
- )
- $ErrorActionPreference = "SilentlyContinue"
- [System.IO.FileStream] $fs = $file.OpenWrite();
- if (!$?) {$true}
- else {$fs.Dispose();$false}
- }
- # Test the function
- $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx
- Test-FileOpen $file
Tuesday, 7 February 2012
Test-FileOpen
Sunday, 22 January 2012
New-SpanishCulture.ps1
- <#
- .SYNOPSIS
- This script creates a Spanish cultureinfo object with a traditional
- sort and another with an international sort. The script then compares them.
- .DESCRIPTION
- This script re-implements an MSDN sample.
- .NOTES
- File Name : New-SpanishCulture.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
- .EXAMPLE
- C:\foo> .\New-SpanishCulture.ps1
- PROPERTY INTERNATIONAL TRADITIONAL
- CompareInfo CompareInfo - es-ES CompareInfo - es-ES_tradnl
- DisplayName Spanish (Spain) Spanish (Spain)
- EnglishName Spanish (Spain, International Sort) Spanish (Spain, Traditional Sort)
- IsNeutralCulture False False
- IsReadOnly False
- LCID 3082 1034
- Name es-ES es-ES
- NativeName Español (España, alfabetización internacional) Español (España, alfabetización tradicional)
- Parent es es
- TextInfo TextInfo - es-ES TextInfo - es-ES_tradnl
- ThreeLetterISOLanguageName spa spa
- ThreeLetterWindowsLanguageName ESN ESP
- TwoLetterISOLanguageName es es
- Comparing [llegar] and [lugar]
- With myCIintl.CompareInfo.Compare: -1
- With myCItrad.CompareInfo.Compare: 1
- #>
- # Create and initialize the CultureInfo which uses the international sort
- $myCIintl = New-Object System.Globalization.CultureInfo "es-ES", $false
- # Create and initialize the CultureInfo which uses the traditional sort
- $myCItrad = New-Object System.Globalization.CultureINfo 0x040A, $false
- # Display the properties of each culture.
- "{0,-31}{1,-47}{2,-25}" -f "PROPERTY", "INTERNATIONAL", "TRADITIONAL"
- "{0,-31}{1,-47}{2,-25}" -f "CompareInfo", $myCIintl.CompareInfo, $myCItrad.CompareInfo
- "{0,-31}{1,-47}{2,-25}" -f "DisplayName", $myCIintl.DisplayName, $myCItrad.DisplayName
- "{0,-31}{1,-47}{2,-25}" -f "EnglishName", $myCIintl.EnglishName, $myCItrad.EnglishName
- "{0,-31}{1,-47}{2,-25}" -f "IsNeutralCulture", $myCIintl.IsNeutralCulture, $myCItrad.IsNeutralCulture
- "{0,-31}{1,-47}{2,-25}" -f "IsReadOnly", $myCIintl.$IsReadOnly, $myCItrad.IsReadOnly
- "{0,-31}{1,-47}{2,-25}" -f "LCID", $myCIintl.LCID, $myCItrad.LCID
- "{0,-31}{1,-47}{2,-25}" -f "Name", $myCIintl.Name, $myCItrad.Name
- "{0,-31}{1,-47}{2,-25}" -f "NativeName", $myCIintl.NativeName, $myCItrad.NativeName
- "{0,-31}{1,-47}{2,-25}" -f "Parent", $myCIintl.Parent, $myCItrad.Parent
- "{0,-31}{1,-47}{2,-25}" -f "TextInfo", $myCIintl.TextInfo, $myCItrad.TextInfo
- "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterISOLanguageName", $myCIintl.ThreeLetterISOLanguageName, $myCItrad.ThreeLetterISOLanguageName
- "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterWindowsLanguageName",$myCIintl.ThreeLetterWindowsLanguageName, $myCItrad.ThreeLetterWindowsLanguageName
- "{0,-31}{1,-47}{2,-25}" -f "TwoLetterISOLanguageName", $myCIintl.TwoLetterISOLanguageName, $myCItrad.TwoLetterISOLanguageName
- ""
- # Compare two strings using myCIintl
- "Comparing [llegar] and [lugar]"
- " With myCIintl.CompareInfo.Compare: {0}" -f $myCIintl.CompareInfo.Compare("llegar", "lugar")
- " With myCItrad.CompareInfo.Compare: {0}" -f $myCItrad.CompareInfo.Compare("llegar", "lugar")
Show-ChineeseParentCulture.ps1
- <#
- .SYNOPSIS
- This script displays the parent culture of each
- specific culture using the Chinese language.
- .DESCRIPTION
- This script looks at each Chineese culture and displays
- the culture name and the parent.
- .NOTES
- File Name : Show-ChineeseParentCulture.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx
- .EXAMPLE
- C:\foo> .\Show-ChineeseParentCulture.ps1
- SPECIFIC CULTURE PARENT CULTURE
- 0x0804 zh-CN Chinese (Simplified, PRC) 0x0004 zh-CHS Chinese (Simplified) Legacy
- 0x0C04 zh-HK Chinese (Traditional, Hong Kong S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy
- 0x1404 zh-MO Chinese (Traditional, Macao S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy
- 0x1004 zh-SG Chinese (Simplified, Singapore) 0x0004 zh-CHS Chinese (Simplified) Legacy
- 0x0404 zh-TW Chinese (Traditional, Taiwan) 0x7C04 zh-CHT Chinese (Traditional) Legacy
- #>
- # Display a header
- "SPECIFIC CULTURE PARENT CULTURE"
- # Determine the specific cultures that use the Chinese language, and displays the parent culture
- ForEach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures)) {
- if ($ci.TwoLetterISOLanguageName -eq "zh")
- {
- $s1 = "0x{0} {1} {2,-40}" -f $ci.LCID.ToString("X4"), $ci.Name, $ci.EnglishName
- $s2 = "0x{0} {1} {2}" -f $ci.Parent.LCID.ToString("X4"), $ci.Parent.Name, $ci.Parent.EnglishName
- "{0}{1}" -f $s1, $s2
- }
- }
Friday, 30 December 2011
Show-MessageBox.ps1
- <#
- .SYNOPSIS
- This script displays a message box and then processes it
- .DESCRIPTION
- This script firsts creates a wscript.shell object and
- invokes the popup method to display a message. The script
- then processes the response to the geroup (timeout, yes, no).
- .NOTES
- File Name : Show-MessageBox.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/x83z1d9f%28VS.84%29.aspx
- .EXAMPLE
- Left as an exercise to the reader!
- #>
- # Create the shell object
- $WshShell = New-Object -Com Wscript.Shell
- # Call the Popup method with a 7 second timeout.
- $Btn = $WshShell.Popup("Do you feel alright?", 7, "Question:", 0x4 + 0x20)
- # Process the response
- switch ($Btn) {
- # Yes button pressed.
- 6 {"Glad to hear you feel alright."}
- # No button pressed.
- 7 {"Hope you're feeling better soon."}
- # Timed out.
- -1 {"Is there anybody out there?"}
- }
Monday, 12 December 2011
Get-PortAndProtocolFromUrl.ps1
- <#
- .SYNOPSIS
- This script strips out a port and protocol number from a URL
- .DESCRIPTION
- This script creates a regular expression reged then uses it to
- match against the URL to get the protocol and port. This is a
- re-write of the MSDN sample.
- .NOTES
- File Name : Get-PortAndProtocolFromUrl.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/63ew9az0.aspx
- .EXAMPLE
- PowerShell> .\Get-PortAndProtocolFromUrl.ps1
- Port : http
- Protocol: 8080
- #>
- # Set URL
- $url = "http://www.contoso.com:8080/letters/readme.html"
- # Create Regex, then match against the URL
- $r = new-object System.Text.RegularExpressions.Regex "^(?<proto>\w+)://[^/]+?:(?<port>\d+)?/"
- $m = $r.Match($url)
- # Print results
- if ($m.Success) {
- "Port : {0}" -f $M.groups["proto"].value
- "Protocol: {0}" -f $M.groups["port"].value
- }
Labels:
Regex,
System.Text.RegularExpressions
Wednesday, 7 December 2011
Confirm-ValidEmailAddress.ps1
- <#
- .SYNOPSIS
- This script validates email addresses based on
- MSFT published Regular Expression. This is a
- re-write with PowerShell of an existing bit of
- MSDN sample code
- .DESCRIPTION
- This script first creates a function to validate
- an email address. It uses a large regex that is
- documented at the MSDN page noted below. The script
- then creates an array of email addreses and then
- validates them against the function and displays
- the results.
- .NOTES
- File Name : Confirm-ValidEmailAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/12/confirm-validemailaddressps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/01escwtf.aspx
- .EXAMPLE
- Valid: david.jones@proseware.com
- Valid: d.j@server1.proseware.com
- Valid: jones@ms1.proseware.com
- Invalid: j.@server1.proseware.com
- Invalid: j@proseware.com9
- Valid: js#internal@proseware.com
- Valid: j_9@[129.126.118.1]
- Invalid: j..s@proseware.com
- Invalid: js*@proseware.com
- Invalid: js@proseware..com
- Invalid: js@proseware.com9
- Valid: j.s@server1.proseware.com
- Valid: tfl@psp.co.uk
- Valid: cuddly.penguin@cookham.net
- #>
- Function IsValidEmail {
- Param ([string] $In)
- # Returns true if In is in valid e-mail format.
- [system.Text.RegularExpressions.Regex]::IsMatch($In,
- "^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
- "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
- } # End of IsValidEmail
- [string[]] $emailAddresses = "david.jones@proseware.com", "d.j@server1.proseware.com",
- "jones@ms1.proseware.com", "j.@server1.proseware.com",
- "j@proseware.com9", "js#internal@proseware.com",
- "j_9@[129.126.118.1]", "j..s@proseware.com",
- "js*@proseware.com", "js@proseware..com",
- "js@proseware.com9", "j.s@server1.proseware.com",
- "tfl@psp.co.uk", "cuddly.penguin@cookham.net"
- ForEach ($emailAddress in $emailAddresses) {
- if (IsValidEmail($emailAddress)) {
- "Valid: {0}" -f $emailAddress
- }
- else {
- "Invalid: {0}" -f $emailAddress
- }
- }
Sunday, 20 November 2011
Show-FileInformation.ps1
- <#
- .SYNOPSIS
- This script displays information returned from the
- file version object.
- .DESCRIPTION
- This script gets, then displays, all the information returned
- from the System.Diagnostics.Fileinfo of Notepad.exe
- .NOTES
- File Name : Show-FileInformation.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://http://pshscripts.blogspot.com
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo_properties.aspx
- .EXAMPLE
- Psh> .\Show-FileInformation.ps1
- File Major Part for C:\Windows\system32\Notepad.exe is: 6
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Display all the file information:
- "FileInfo information for {0}" -f $file
- "Comments : {0}" -f $myFileVersionInfo.Comments
- "Company Name : {0}" -f $myFileVersionInfo.CompanyName
- "FileBuldPart : {0}" -f $myFileVersionInfo.FileBuildPart
- "FileDescription : {0}" -f $myFileVersionInfo.FileDescription
- "FileMajorPart : {0}" -f $myFileVersionInfo.FileMajorPart
- "FileMinorPart : {0}" -f $myFileVersionInfo.FileMinorPart
- "FilePrivatePart : {0}" -f $myFileVersionInfo.FilePrivatePart
- "FileName : {0}" -f $myFileVersionInfo.FileName
- "FileVersion : {0}" -f $myFileVersionInfo.FileVersion
- "InternalName : {0}" -f $myFileVersionInfo.InternalName
- "IsDebug : {0}" -f $myFileVersionInfo.IsDebug
- "IsPatched : {0}" -f $myFileVersionInfo.IsPatched
- "IsPreRelease : {0}" -f $myFileVersionInfo.IsPreRelease
- "IsPrivateBuild : {0}" -f $myFileVersionInfo.IsPrivateBuild
- "IsSpecialBuild : {0}" -f $myFileVersionInfo.IsSpecialBuild
- "Language : {0}" -f $myFileVersionInfo.Language
- "LegalCopyright : {0}" -f $myFileVersionInfo.LegalCopyright
- "LegalTrademarks : {0}" -f $myFileVersionInfo.LegalTrademarks
- "OriginalFilename : {0}" -f $myFileVersionInfo.OriginalFilename
- "PrivateBuild : {0}" -f $myFileVersionInfo.PrivateBuild
- "ProductBuildPart : {0}" -f $myFileVersionInfo.ProductBuildPart
- "ProductMajordPart : {0}" -f $myFileVersionInfo.ProductMajorPart
- "ProductMinorPart : {0}" -f $myFileVersionInfo.ProductMinorPart
- "ProductName : {0}" -f $myFileVersionInfo.ProductName
- "ProductPrivatePart : {0}" -f $myFileVersionInfo.ProductMinorPart
- "ProductVersion : {0}" -f $myFileVersionInfo.ProductVersion
- "SpecialBuild : {0}" -f $myFileVersionInfo.SpecialBuild
Sunday, 13 November 2011
ShowFileDescription.ps1
- <#
- .SYNOPSIS
- This script shows the build description of file version object.
- .DESCRIPTION
- This script is a re-implementation of an MSDN Sample script
- that uses System.Diagnostics.FileVersionInfo to get
- the description of the file.
- .NOTES
- File Name : Show-FileDescription.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/11/showfiledescriptionps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filedescription.aspx
- .EXAMPLE
- Psh> .\Show-FileDescription.ps1
- File description for C:\Windows\system32\Notepad.exe is: Notepad
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Print the Build details name
- "File description for {0} is: {1}" -f $file,$myFileVersionInfo.FileDescription
Show-FileBuildPart.ps1
- <#
- .SYNOPSIS
- This script shows the build number of file version object.
- .DESCRIPTION
- This script is a re-implementation of an MSDN Sample script
- that uses System.Diagnostics.FileVersionInfo to get
- the build identification of the file.
- .NOTES
- File Name : Show-FileBuildPart.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2011/11/show-filebuildpartps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filebuildpart.aspx
- .EXAMPLE
- Psh> .\Show-FileBuildPart.ps1
- File build number for C:\Windows\system32\Notepad.exe is: 6001
- #>
- # Set filename
- $File = [System.Environment]::SystemDirectory + "\Notepad.exe"
- #Get Version information for this file
- $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File)
- # Print the Build details name
- "File build number for {0} is: {1}" -f $file,$myFileVersionInfo.FileBuildPart
Saturday, 12 November 2011
Show-NumberPadding3.ps1
- <#
- .SYNOPSIS
- Shows formatting of double values.
- .DESCRIPTION
- This script, a re-implementation of an MSDN Sample,
- creates a double value then formats it with 5 leading
- zeros.
- .NOTES
- File Name : Show-NumberPadding3.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/dd260048.aspx
- .EXAMPLE
- Psh> Show-NumberPadding3.ps1
- 01053240
- 00103932.52
- 01549230
- 01053240
- 00103932.52
- 01549230
- 9034521202.93
- #>
- $fmt = "00000000.##"
- [int] $intValue = 1053240
- [decimal] $decValue = 103932.52
- [float] $sngValue = 1549230.10873992
- [double] $dblValue = 9034521202.93217412
- # Display the numbers using the ToString method
- $intValue.ToString($fmt)
- $decValue.ToString($fmt)
- $sngValue.ToString($fmt)
- ""
- # Display the numbers using composite formatting
- $formatString = " {0,15:" + $fmt + "}" # right justified
- $formatString -f $intValue
- $formatString -f $decValue
- $formatString -f $sngValue
- $formatString -f $dblValue
Labels:
formatting,
powershell,
PowerShell scripts,
ToString()
Subscribe to:
Posts (Atom)

