- <#
- .SYNOPSIS
- Counts the Grateful Dead shows in my archives
- .DESCRIPTION
- This script looks at my GD archive, and uses folder names to
- determine show type (aud, sbd, etc), and checks to see what
- shows have checked MD5s
- .NOTES
- File Name : count-gdshows.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://pshscripts.blogspot.com/2009/01/count-gdshowsps1.html
- .EXAMPLE
- PS C:\foo> count-gdshows.ps1
- Count.ps1 - v 2.0.1
- +---------------------------+
- ! Dead Show Base : M:\gd !
- +---------------------------+
- Grateful Dead Show Summary
- --------------------------
- Total shows: 1146
- Soundboards: 800
- Auds : 70
- Unknown : 29
- Partial : 9
- Broken : 4
- MD5's check: 489 (42.67 %)
- #>
- ###
- # Start of Archive
- ##
- # Constants:
- # $GDDiskRoot - where to find shows
- # $DeadShowBase - folder at top of gd shows
- $GDDiskRoot = "M:"
- $DeadShowBase = $GDDiskRoot + "\gd"
- # Announce Ourselves
- "Count.ps1 - v 2.0.1"
- "+---------------------------+"
- "! Dead Show Base : $DeadShowBase !"
- "+---------------------------+"
- ""
- # Count the Dead shows
- $Dir= ls $DeadShowBase | where {$_.psiscontainer}
- $DeadShows=$Dir.count
- if ($DeadSHows -le 0) {"no shows found - check constants"}
- #Create subsets based on names of the folders
- $deadsbds=$dir | where {$_.name -match ".sbd" }
- $deadbrkn=$dir | where {$_.name -match "broken" }
- $deadpart=$dir | where {$_.name -match "partial" }
- $deadauds=$dir | where {$_.name -match ".aud" }
- $deadunkw=$dir | where {$_.name -Match ".unk"}
- #and see how many have the md5ok's file?
- $DeadMD5Checked=0
- foreach ($d in $dir)
- {
- $sn=$d.fullname + "\md5check_ok"
- $md5ok= ls $sn -ea silentlycontinue
- if ($md5ok )
- {$DeadMD5Checked++}
- }
- #Display results
- "Grateful Dead Show Summary"
- "--------------------------"
- "Total shows: $deadshows"
- "Soundboards: $($deadsbds.count)"
- "Auds : $($deadauds.count)"
- "Unknown : $($deadunkw.count)"
- "Partial : $($deadpart.count)"
- "Broken : $($deadbrkn.count)"
- $DeadPctChecked=($DeadMD5checked/$DeadShows).tostring("P")
- "MD5's check: $DeadMD5checked ($DeadPctChecked)"
- ""
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Saturday, 31 January 2009
Count-GDShows.ps1
Labels:
Grateful Dead,
powershell,
PowerShell scripts
Thursday, 29 January 2009
Get-IPAddress.ps1
- <#
- .SYNOPSIS
- Gets and displays the IP address of a computer
- .DESCRIPTION
- This script uses Win32_NetworkAdapterConfiguration to
- obtain, then display, a system's IP addresses.
- NB: this only works on XP or later versions of Windows.
- .NOTES
- File Name : Get-IPAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- Script postesd to:
- http://www.pshscripts.blogspot.com
- MSDN Sample at:
- http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
- .EXAMPLE
- [ps] c:\foo> .\Get-IPAddress.ps1
- IP Address : 10.10.1.115
- IP Address : fe80::3953:f67b:2f1c:1323
- IP Address : 10.10.1.120
- IP Address : fe80::d8ed:afe2:2a97:a596
- 4 IP addresses found on this system
- #>
- ##
- # Start of Script
- ##
- # Get Networking Adapter Configuration
- $Computer = "."
- $IPconfigset = Get-WmiObject Win32_NetworkAdapterConfiguration
- # Iterate and get IP address
- $count = 0
- foreach ($IPConfig in $IPConfigSet) {
- if ($Ipconfig.IPaddress) {
- foreach ($addr in $Ipconfig.Ipaddress) {
- "IP Address : {0}" -f $addr;
- $count++
- }
- }
- }
- if ($count -eq 0) {"No IP addresses found"}
- else {"$Count IP addresses found on this system"}
- #End of Script
Wednesday, 28 January 2009
Get-TimeZoneInfo.ps1
- <#
- .SYNOPSIS
- Gets and displays time zone information via WMI
- .DESCRIPTION
- This script uses the Win32_TimeZone class to obtain, then
- display time zone information for a computer.
- This script is a MSDN sample recoded in PowerShell
- .NOTES
- File Name : Get-TimeZoneInfo.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Script posted to:
- http://www.pshscripts.blogspot.com/2009/01/get-timezoneinfops1.html
- MSDN Sample at:
- http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
- .EXAMPLE
- [ps] c:\foo> .\Get-TimeZoneInfo.ps1
- Time zone information on computer "Win7"
- Time Zone Description : (UTC) Dublin, Edinburgh, Lisbon, London
- Daylight Name : GMT Daylight Time
- Standard Name : GMT Standard Time
- #>
-
- ###
- # Start of Script
- ##
-
- # Get Time Zone on a computer
- $Computer = "."
- $timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $computer
-
- # Display details
- if ($computer -eq ".") {$computer = Hostname}
- "Time zone information on computer `"{0}`"" -f $computer
- "Time Zone Description : {0}" -f $timezone.Description
- "Daylight Name : {0}" -f $timezone.DaylightName
- "Standard Name : {0}" -f $timezone.StandardName
- #End of Script
Monday, 26 January 2009
Get-WMILocalTime.ps1
- <#
- .SYNOPSIS
- Gets time from WMI and displays it
- .DESCRIPTION
- This script gets Win32_LocalTime and then displays
- the details.
- .NOTES
- File Name : Get-WMILocalTime.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Script posted to:
- http://pshscripts.blogspot.com/2009/01/get-wmilocaltimeps1.html
- MSDN Sample at:
- http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx
- .EXAMPLE
- [ps] c:\foo> .\Get-WMILocaltimeps1
- Day : 26
- Day Of Week : 1
- Hour : 20
- Minute : 59
- Month : 1
- Quarter : 1
- Second : 53
- Week In Month: 5
- Year : 2009
- #>
- ###
- # Start of Script
- ##
- # Speficy computer and get Local Time
- $Computer = "."
- $times = Get-WmiObject Win32_LocalTime -computer $computer
- # Now display the result
- Foreach ($time in $times) {
- "Day : {0}" -f $Time.Day
- "Day Of Week : {0}" -f $Time.DayOfWeek
- "Hour : {0}" -f $Time.Hour
- "Minute : {0}" -f $Time.Minute
- "Month : {0}" -f $Time.Month
- "Quarter : {0}" -f $Time.Quarter
- "Second : {0}" -f $time.Second
- "Week In Month: {0}" -f $Time.WeekInMonth
- "Year : {0}" -f $Time.Year
- }
- # End of Script
Labels:
powershell,
PowerShell scripts,
Win32_LocalTime,
wmi
Sunday, 25 January 2009
Show-TryCatchFinally2.ps1
- <#
- .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()
- }
- }
Friday, 23 January 2009
Get-ErrorDetails.ps1
- <#
- .SYNOPSIS
- Shows detals of an $Error record for an exception
- .DESCRIPTION
- Creates an exception then shows how to find out details.
- .NOTES
- File Name : Get-ErrorDetails.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://pshscripts.blogspot.com/2009/01/get-errordetailsps1.html
- .EXAMPLE
- PS C:\foo> .\Get-Errordetails.ps1'
- Output - left as an exercise for the reader
- #>
- ###
- # Start of Script
- ###
- # Create divide by zero error
- try {
- $zero = 0
- 1/$zero # will throw an exception
- }
- catch [System.DivideByZeroException] {
- "Exception caught in catch block"
- $err = $Error[0]
- # display $err contents ($error[0])
- "Contents of Error[0]"
- $Err | fl * -Force
- ""
- # now - show exception
- "Exception details:"
- $err.Exception | fl * -Force
- ""
- # To get exception try:
- "Exception:"
- $err.Exception.InnerException | fl * -Force
- ""
- # To get actual exception in this case:
- "Specific exception name:"
- $err.Exception.InnerException.tostring().split(":")[0]
- }
- catch {
- "Some other error occurred"
- $Error[0]
- }
- # End of script
Thursday, 22 January 2009
Throw-Exception.ps1
- <#
- .SYNOPSIS
- Shows throwing an exception in a function, caught in caller.
- .DESCRIPTION
- This is a re-written MSDN Sample
- .NOTES
- File Name : throw-exception.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Original script at:
- http://pshscripts.blogspot.com/2009/01/throw-exceptionps1.html
- MSDN Sample at:
- http://msdn.microsoft.com/en-us/library/1ah5wsex.aspx
- .EXAMPLE
- PS C:\foo> .\Throw-Exception.ps1
- Num[0] - Result = 300
- Trying to get Num[4]
- In catch block
- Error caught: System.IndexOutOfRangeException
- #>
- ###
- # Start of script
- ###
- # Helper function
- function GetNumber{
- param ($index)
- $nums = 300, 600, 900
- if ($index -gt $nums.Length) {
- Throw [system.IndexOutOfRangeException]
- }
- else {
- $nums[$index]
- }
- }
- # Start of main script
- # Get one that works...
- $result = GetNumber 0
- "Num[0] - Result = {0}" -f $result
- # Now try and catch the following
- try {
- "Trying to get Num[4]"
- $result = GetNumber 4
- }
- Catch {
- "In catch block"
- "Error caught: {0}" -f $Error[0]
- }
Labels:
powershell,
PowerShell scripts,
throw
Wednesday, 21 January 2009
Get-TryCatchFinally.ps1
- <#
- .SYNOPSIS
- Shows Try/Catch/Finally using Powershell
- .DESCRIPTION
- This is an MSDN Sample, re-written in PowerShell
- .NOTES
- File Name : get-trycatchfinally.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Original script posted to:
- http://pshscripts.blogspot.com/2009/01/get-trycatchfinallyps1.html
- MSDN Sample at:
- http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx
- .EXAMPLE
- PS C:\foo> .\Get-TryCatchFinally.ps1
- Error in conversion
- Error record: Cannot convert value "Some string" to type "System.Int32". Error: "Input string was not in a correct format."
- $i = 123
- $i = System.Int32
- #>
- ###
- # Start of script
- ###
- # Create some explicitly typed values
- [int] $i = 123
- [string] $s = "Some string"
- [object] $o = $s
- # Now try to convert an object into an integer (which will fail)
- try {
- # Invalid conversion; $o contains a string not an int
- $i = [int] $o
- }
- # catch the error and display
- catch {
- "Error in conversion"
- "Error record: {0}" -f $Error[0]
- }
- # Clean up
- finally {
- "`$i = {0}" -f $i
- "`$i = {0}" -f $i.gettype()
- }
- # End of Script
Labels:
catch,
finally,
powershell,
PowerShell scripts,
try
Tuesday, 20 January 2009
100 Scripts So Far
I’ve been posting PowerShell scripts for just over 6 months. Traffic to this blog has been growing slowly – and for a niche blog rather nicely.
Yesterday, I hit 100 scripts in the archive. You can download these scripts from here. Enjoy!
Technorati Tags: PowerShell Scripts
Monday, 19 January 2009
Get-LeapYear.ps1
- <#
- .SYNOPSIS
- Demonstrates use of System.Datetime to determine leap year
- .DESCRIPTION
- This script creates four date objects and checks to see if
- the date is a leap year. The last object is today's date.
- .NOTES
- File Name : Get-LeapYear.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PS c:\foo> .\Get-LeapYear.ps1
- 2000 is a leap year: True
- 2002 is a leap year: False
- 2004 is a leap year: True
- 2009 is a leap year: False
- #>
- ##
- # Start of script
- ##
- # Create three specific date objects, plus today
- $d1 = [system.datetime] "Jan 1 2000"
- $d2 = [System.datetime] "Jan 1 2002"
- $d3 = [System.datetime] "Jan 1 2004"
- $d4 = get-date
- # Are they leap years?
- "{0} is a leap year: {1}" -f $d1.year,([system.datetime]::isleapyear($d1.year))
- "{0} is a leap year: {1}" -f $d2.year,([system.datetime]::isleapyear($d2.year))
- "{0} is a leap year: {1}" -f $d3.year,([system.datetime]::isleapyear($d3.year))
- "{0} is a leap year: {1}" -f $d4.year,([system.datetime]::isleapyear($d4.year))
- # End Script
Labels:
powershell,
PowerShell scripts,
system.datetime
Sunday, 18 January 2009
Get-CountOfJerryShows.ps1
- <#
- .SYNOPSIS
- Counts Jerry Garcia live shows on q: drive
- .DESCRIPTION
- I store Jerry Garcia live shows on my Q: drive. This
- script counts the shows by year, and gives a total.
- .NOTES
- File Name : get-countofjerryshows.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PS C:\foo> .\get-countofjerryshows
- in: jg_1972_project, 9 shows
- in: jg_1973_project, 39 shows
- in: jg_1974_project, 40 shows
- in: jg_1975_project, 34 shows
- in: jg_1976_project, 52 shows
- in: jg_1977_project, 53 shows
- in: jg_1978_project, 27 shows
- in: jg_1979_project, 31 shows
- in: jg_1980_project, 56 shows
- in: jg_1982_project, 84 shows
- in: jg_1983_project, 3 shows
- in: jg_1984_project, 76 shows
- in: jg_1985_project, 22 shows
- in: jg_1986_project, 25 shows
- in: jg_1989_project, 32 shows
- in: jg_1990_project, 1 shows
- in: jg_1991_project, 64 shows
- in: jg_1992_project, 26 shows
- in: jg_1993_project, 5 shows
- in: jg_1994_project, 36 shows
- in: jg_1995_project, 19 shows
- 734 shows in total
- #>
- ##
- # Start of Script
- ##
- # Get starting point and move there
- $jerry = "q:\Jerry Garcia"
- cd $jerry
- # Get sub-dirs that contain shows
- $dirs = ls | where {$_.Name -match "jg_"}
- # Now iterate and count
- $total = 0
- foreach ($dir in $dirs) {
- $shows = ls $dir.FullName
- if ($shows.count) {$count=$shows.count} else {$count = 1}
- "In: {0,-15}, {1,4} shows" -f $dir.Name, $count
- $total += $count
- }
- "{0} shows in total" -f $total
Labels:
Jerry Garcia,
powershell,
PowerShell scripts
Send-BCCMail.ps1
- <#
- .SYNOPSIS
- Send mail to BCC using PowerShell
- .DESCRIPTION
- This script is a re-developed MSDN Sample using PowerShell. It creates
- an email message then sends it with a BCC.
- .NOTES
- File Name : Send-BCCMail.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Original Sample Posted to
- hthttp://pshscripts.blogspot.com/2009/01/send-bccmailps1.html
- MSDN Sample and details at:
- http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddresscollection.aspx
- .EXAMPLE
- PS C:\foo> .\Send-BCCMail.ps1
- Sending an e-mail message to The PowerShell Doctor and "Thomas Lee" <tfl@reskit.net>
- #>
- ###
- # Start Script
- ###
- # Create from, to, bcc and the message strucures
- $From = New-Object system.net.Mail.MailAddress "tfl@cookham.net", "Thomas Lee"
- $To = new-object system.net.mail.Mailaddress "doctordns@gmail.com", "The PowerShell Doctor"
- $Bcc = New-Object system.Net.Mail.Mailaddress "tfl@reskit.net", "Thomas Lee"
- $Message = New-Object system.Net.Mail.MailMessage $From, $To
- # Populate message
- $Message.Subject = "Using the SmtpClient class and PowerShell."
- $Message.Body = "Using this feature, you can send an e-mail message from an"
- $Message.Body += "application very easily. `nEven better, you do it with PowerShell!"
- # Add BCC
- $Message.Bcc.Add($bcc);
- # Create SMTP Client
- $Server = "localhost"
- $Client = New-Object system.Net.Mail.SmtpClient $server
- $Client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
- "Sending an e-mail message to {0} and {1}" -f $to.DisplayName, $Message.Bcc.ToString()
- # send the message
- try {
- $Client.Send($message);
- }
- catch {
- "Exception caught in Send-BCCMail.ps1: {0}" -f $Error[0]
- }
Labels:
powershell,
PowerShell scripts,
system.net.mail
Tuesday, 13 January 2009
Get-UpTime.ps1
- <#
- .SYNOPSIS
- Demonstrates uptime using WMI
- .DESCRIPTION
- This script used Win32_ComputerSystem to determine how long your system
- has been running. This is a rewrite/improvement of sample 3 at
- http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx.
- .NOTES
- File Name : Get-UpTime.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Script Posted to:
- http://www.pshscripts.blogspot.com
- Original sample posted at:
- http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx
- .EXAMPLE
- PS c:\foo> .\Get-UpTime.Ps1
- System Up for: 1 days, 8 hours, 40.781 minutes
- #>
- ##
- # Start of script
- ##
- # Helper Function - convert WMI date to TimeDate object
- function WMIDateStringToDate($Bootup) {
- [System.Management.ManagementDateTimeconverter]::ToDateTime($Bootup)
- }
- # Main script
- $Computer = "." # adjust as needed
- $computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer
- foreach ($system in $computers) {
- $Bootup = $system.LastBootUpTime
- $LastBootUpTime = WMIDateStringToDate($Bootup)
- $now = Get-Date
- $Uptime = $now - $lastBootUpTime
- $d = $Uptime.Days
- $h = $Uptime.Hours
- $m = $uptime.Minutes
- $ms= $uptime.Milliseconds
- "System Up for: {0} days, {1} hours, {2}.{3} minutes" -f $d,$h,$m,$ms
- }
- # End script
Sunday, 11 January 2009
Get-Screensaver.ps1
- <#
- .SYNOPSIS
- Determine if a computer screen saver requires a password.
- .DESCRIPTION
- This script is a re-write of script 2 on the MSDN site (see
- below for link). This script also displays the user name for
- each desktop and the screen saver executable.
- .NOTES
- File Name : Get-Screensaver.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Script published to:
- http://www.pshscripts.blogspot.com
- Adapted from MSDN:
- http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx
- .EXAMPLE
- PS c:\foo> .\Get-Screensaver.ps1
- 5 desktops found as follows
- Desktop : NT AUTHORITY\SYSTEM
- Screen Saver : logon.scr
- Secure : False
- Desktop : NT AUTHORITY\LOCAL SERVICE
- Screen Saver : %SystemRoot%\System32\logon.scr
- Secure : False
- Desktop : NT AUTHORITY\NETWORK SERVICE
- Screen Saver : %SystemRoot%\System32\logon.scr
- Secure : False
- Win
- Desktop : Cookham\tfl
- Screen Saver : %Systemroot%\tflscreensaver.scr
- Secure : False
- Desktop : .DEFAULT
- Screen Saver : logon.scr
- Secure : False
- #>
- ##
- # Start of script
- ##
- $Computer = "."
- $Desktops = Get-WMIObject -class Win32_Desktop -ComputerName $computer
- "{0} desktops found as follows" -f $desktops.count
- foreach ($desktop in $desktops) {
- "Desktop : {0}" -f $Desktop.Name
- "Screen Saver : {0}" -f $desktop.ScreensaverExecutable
- "Secure : {0} " -f $desktop.ScreenSaverSecure
- ""
- }
Labels:
powershell,
PowerShell scripts,
Win32_Desktop,
wmi
Saturday, 10 January 2009
Get-Hash2.ps1
- <#
- .SYNOPSIS
- Creates a Hash of a file and returns the hash
- .DESCRIPTION
- Uses System.Security.Cryptography.HashAlgorithm and members to create
- the hash. This script improves on:
- http://pshscripts.blogspot.com/2009/01/get-hashpsm1.html by using
- .NOTES
- File Name : Get-Hash1.PSM1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- Thanks to the #PowerShell Twitter Posse (PTP) for help figuring out
- the –verbose parameter. And thanks to the PTP for comments on the
- earlier version of this script, which now uses a file stream as
- input to the hash alghorithm.
- .LINK
- Posted to : http://pshscripts.blogspot.com/2009/01/get-hash2ps1.html
- Based on : http://tinyurl.com/aycszb written by Bart De Smet
- An improvement of : http://pshscripts.blogspot.com/2009/01/get-hashpsm1.html
- .PARAMETER Algorithm
- The name of one of the hash Algorithms defined at
- http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.aspx
- .PARAMETER File
- The name of a file to provide a hash for.
- .PARAMETER Verbose
- if specified, this script will produce chattier output.
- .EXAMPLE
- PS C:\foo> ls *.txt | where {!$_.psiscontainer}| c:\foo\Get-Hash2.ps1 sha1 -verbose
- OK, I'll be chatty
- The sha1 hash of file C:\foo\asciifile.txt is: "55055a5c8eeb3af7fa6d426314578ee1d56df016"
- The sha1 hash of file C:\foo\log.txt is: "575f4b35e3cadb9b273095fc463bd43e9a3f5774"
- The sha1 hash of file C:\foo\sites.txt is: "8ce6663cd2b64a513cf18a831843afd98e190764"
- The sha1 hash of file C:\foo\test.txt is: "a2f26abbeeb4e6846e159ba506e07cae5496d458"
- The sha1 hash of file C:\foo\test2.txt is: "9b1baaa9077a3691f8e2685d81ffa24cdd73f71d"
- The sha1 hash of file C:\foo\unicodefile.txt is: "094ef2696d9eb3374e657eb7c227ff4c36cd0cb9"
- .EXAMPLE
- PS C:\foo> ls *.txt | where {!$_.psiscontainer}| c:\foo\Get-Hash2.ps1 sha1
- 55055a5c8eeb3af7fa6d426314578ee1d56df016
- 575f4b35e3cadb9b273095fc463bd43e9a3f5774
- 8ce6663cd2b64a513cf18a831843afd98e190764
- a2f26abbeeb4e6846e159ba506e07cae5496d458
- 9b1baaa9077a3691f8e2685d81ffa24cdd73f71d
- 094ef2696d9eb3374e657eb7c227ff4c36cd0cb9
- .EXAMPLE
- PS C:\foo> Get-Hash md5 asciifile.txt -verbose
- OK, I'll be chatty
- The md5 hash of file c:\foo\asciifile.txt is: "06f51e7bfced5c0623eec5f72e0999d6"
- .EXAMPLE
- PS C:\foo> .\get-hash2 md5 c:\foo\asciifile.txt
- 06f51e7bfced5c0623eec5f72e0999d6
- #>
- #[CmdletBinding()]
- param (
- [Parameter(Position=0, mandatory=$true)]
- [string] $Algorithm,
- [Parameter(Position=1, mandatory=$true, valuefrompipeline=$true)]
- [string] $File
- )
- Begin {
- if ($VerbosePreference.Value__ -eq 0) {$verbose=$false} else {$verbose=$true}
- if ($Verbose) {"OK, I'll be chatty"}
- }
- Process {
- if ($VerbosePreference.Value__ -eq 0) {$verbose=$false} else {$verbose=$true}
- # Get the alghorthm object
- $Algo=[System.Security.Cryptography.HashAlgorithm]::Create($algorithm)
- if ($Algo){
- # Open the file
- $Filemode = [System.IO.FileMode]::Open
- $Fs = New-Object System.Io.Filestream $File, $Filemode
- if ($fs.length -gt 0) {
- # Now compute hash
- $Hash = $Algo.ComputeHash($Fs)
- $Hashstring =""
- foreach ($byte in $hash) {$hashstring += $byte.tostring("x2")}
- # pass hash string on
- if ($verbose){
- "The {0} hash of file {1} is: `"{2}`"" -f $algorithm, $file, $hashstring
- ""
- }
- else {
- $Hashstring
- }
- }
- else {
- if ($verbose) {"File {0} can not be hashed" -f $file ; ""}
- }
- $fs.close()
- }
- else {"Algorithm {0} not found" -f $algorithm}
- }
Share this post : |
Subscribe to:
Posts (Atom)