- <#
- .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?"}
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Friday, 30 December 2011
Show-MessageBox.ps1
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
- }
- }
Subscribe to:
Posts (Atom)