- <#
- .SYNOPSIS
- This script uses the SHA1 Crypto Provider to hash a file
- .DESCRIPTION
- This script uses SHA1 Cryptoprovider to hash a file and print the hash. A second
- hash is created of the same file, but with a space added. The file I picked is a
- large text file I keep in c:\foo for uses like this!
- .NOTES
- File Name : Get-Sha1HashFile.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- Posted to TechEd Sample Gallery at:
- .EXAMPLE
- PSH [C:\foo]: .\Get-Sha1HashFile.ps1
- Hashing : C:\foo\gd.txt
- Results in : 760bb67356560232
- Hashing : C:\foo\gd.txt + space
- Results in : 4f7b4902168bba7d
- #>
- # Create Input Data
- $file = (ls c:\foo\*.txt | sort -descending length)[0] # get largest file
- $filestr = Get-Content $file
- $filebytes1 = [System.Text.UnicodeEncoding]::Unicode.getbytes($files)
- $filebytes2 = [System.Text.UnicodeEncoding]::Unicode.getbytes($files+" ")
- # Create a New SHA1 Crypto Provider
- $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
- # Now hash that
- $result1 = $sha.ComputeHash($filebytes1)
- "Hashing : {0}" -f $file.FullName
- "Results in : {0}" -f [system.BitConverter]::ToUint64($result1,0).tostring("x")
- $result2 = $sha.ComputeHash($filebytes2)
- "Hashing : {0}" -f " $($file.FullName) + space"
- "Results in : {0}" -f [system.BitConverter]::ToUint64($result2,0).tostring("x")
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Monday, 16 August 2010
Get-Sha1HashFile.ps1
Friday, 13 August 2010
Get-Sha1Hash.ps1
- <#
- .SYNOPSIS
- This script uses the SHA1 Crypto Provider to hash a string
- .DESCRIPTION
- This script creates 2 strings, and a SHA1 Crypto Provider.
- The script then hashes the strings and displays the results.
- .NOTES
- File Name : Get-Sha1Hash.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 at:
- http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1cryptoserviceprovider.aspx
- Posted to TechEd Sample Gallery at:
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.Security.Crpytography\Get-Sha1Hash.ps1'
- Hashing : This is a string to hash
- Results in : z→???_r♠W??o???]rv??
- Hashing : this is a string to hash
- Results in : ???☺$?Z??♀???????U?'
- #>
- # Create Input Data
- $enc = [system.Text.Encoding]::ASCII
- $string1 = "This is a string to hash"
- $string2 = "this is a string to hash"
- $data1 = $enc.GetBytes($string1)
- $data2 = $enc.GetBytes($string2)
- # Create a New SHA1 Crypto Provider
- $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
- # Now hash and display results
- $result1 = $sha.ComputeHash($data1)
- "Hashing : {0}" -f $string1
- "Results in : {0}" -f $enc.Getstring($result1)
- ""
- $result2 = $sha.ComputeHash($data2)
- "Hashing : {0}" -f $string2
- "Results in : {0}" -f $enc.Getstring($result2)
Thursday, 12 August 2010
Get-DnsRegisteredServers.ps1
- <#
- .SYNOPSIS
- This script gets the reverse lookup zone from a DNS Server and
- displays all the systems registered
- .DESCRIPTION
- This script first gets the reverse lookup zone from a DNS Server (i.e.
- all the computers that have used the DNS server to register!). The
- script then displays the FQDN, IP Address and Timestamp.
- .NOTES
- File Name : Get-DnsRegisteredServers.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- This script posted to TechNet Scripting Gallery
- http://gallery.technet.microsoft.com/ScriptCenter/en-us/28e8b98e-565b-40be-ba2c-1134341bb555
- .EXAMPLE
- PSH [C:\foo]: .Get-DnsRegisteredNames.ps1
- Computers reverse registered on DNS Server: Cookham1
- 10.10.1.42 superman.cookham.net. 3590550
- 10.10.1.105 batman.cookham.net. 3582112
- 10.10.1.109 jeeves.cookham.net. 3586452
- 10.10.1.111 supergirl.cookham.net. 3590550
- 10.10.1.114 future.cookham.net. 3589209
- 10.10.1.131 bladerunner.cookham.net. 3587817
- 10.10.1.142 wonderwoman.kapoho.net. 3590551
- #>
- $dns = "Cookham1"
- $records = get-wmiobject -class MicrosoftDNS_PTRType -namespace root\MicrosoftDNS -computer $dns
- "Computers reverse registered on DNS Server: $DNS"
- # Loop through and display results
- foreach ($record in $records) {
- # Get owner name and ip address string
- $on = $record.ownerName.split(".")
- $ownerip = $on[3] + "." + $on[2] + "." + $on[1] + "." + $on[0]
- # Display details
- "{0, -15} {1,-40} {2,-10} " -f $ownerip, $record.ptrdomainname, $record.timestamp
- }
Labels:
MicrosoftDNS,
powershell,
PowerShell scripts,
wmi
Wednesday, 11 August 2010
Get-EncryptedAesString.ps1
- <#
- .SYNOPSIS
- This script encrypts then decrypts a string using AES
- .DESCRIPTION
- This script re-implements an MSDN sample that first
- encrypts a string then decrypts it. The crypto is done
- using AES. Running this script multiple times will result
- in differently encrypted quotes since a new key/IV is
- generated each time you run the script.
- .NOTES
- File Name : Get-AesEnctyptedString.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/08/get-encryptedaesstringps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/09d0kyb3.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-AesEncryptedString.ps1
- Quote:
- Things may come to those who wait, but only the things left by those who hustle. -- Abraham Lincoln
- Encrypted Quote:
- zp4gD/Mmu01Msr3V8d94Kvwut4ClaW+f1HyohT+QjpFN5FVNhyLcQIgia4iD2TcsK/SpLcm5cOi1/KM+d9ENeU2Lkn8fOZHpMnklUrABGDoM1BRQDfWNcVbMIOA4IUY
- zLoJ5huHKhnyPA2fobGouW33HOpONI0zR3KS4RdtD3M3QQsqHW2+QOkE4/Ls/5gCSbcTuP2FDkk3J9b+1XgqzJZx0jZIBl+moUcqA33xQdu5bGhwg1E8sk2oEU8AQyy
- XcRrS/h8vTLve8c0Tpj4f2Vg==
- Decrypted Quote:
- Things may come to those who wait, but only the things left by those who hustle. -- Abraham Lincoln
- #>
- # Best Practice!
- Set-StrictMode -version 2
- # Two helper functions
- function EncryptString{
- param( $SymAlg,
- [string] $inString
- )
- $inBlock = [System.Text.UnicodeEncoding]::Unicode.getbytes($instring)
- $xfrm = $symAlg.CreateEncryptor()
- $outBlock = $xfrm.TransformFinalBlock($inBlock, 0, $inBlock.Length);
- return $outBlock;
- }
- function DecryptBytes {
- param ($SymAlg,
- $inBytes
- )
- $xfrm = $symAlg.CreateDecryptor();
- $outBlock = $xfrm.TransformFinalBlock($inBytes, 0, $inBytes.Length)
- return [System.Text.UnicodeEncoding]::Unicode.GetString($outBlock)
- }
- # main script
- $Quote = "Things may come to those who wait, but only the " +
- "things left by those who hustle. -- Abraham Lincoln"
- "Quote:";$Quote;""
- # Generate CSP, Key and IV
- $AesCSP = New-Object System.Security.Cryptography.AesCryptoServiceProvider
- $AesCSP.GenerateKey()
- $AesCSP.GenerateIV()
- # Encrypt quote
- $EncQuote = EncryptString $aesCSP $quote
- "Encrypted Quote:"
- [System.Convert]::ToBase64String($encQuote)
- ""
- # Now Decrypt
- "Decrypted Quote:"
- DecryptBytes $AesCSP $EncQuote
Tuesday, 10 August 2010
Convert-Date.ps1
- <#
- .SYNOPSIS
- This script manipulates a time using TimeZone info methods.
- .DESCRIPTION
- This script re-implements an MSDN sample using PowerShell. The script first
- creates a DateTime object then it converts it to Universal, UTC, Pacific and 'local' time.
- .NOTES
- File Name : Convert-Date.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/08/convert-dateps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'E:\PowerShellScriptLib\System.TimeZoneINfo\Convert-date.ps
- Date : 3/21/2006 02:00:00 AM
- Local Time zone: GMT Standard Time
- In Universal time: 3/21/2006 02:00:00 AM
- In UTC : 3/21/2006 02:00:00 AM
- In Local TZ: : 3/21/2006 02:00:00 AM
- In Pacific TZ : 3/21/2006 10:00:00 AM
- #>
- # Create date object
- $date1 = New-Object System.DateTime 2006, 3, 21, 2, 0, 0
- # Display date and local time zone then show that time in other time zones
- "Date : {0}" -f $date1
- "Local Time zone: {0}" -f ([System.TimeZoneInfo]::Local).id
- " In Universal time: {0}" -f $date1.ToUniversalTime()
- " In UTC : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1)
- " In Local TZ: : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, ([System.TimeZoneInfo]::Local))
- $tz = [System.TimeZoneInfo]::FindSystemTimeZoneById("Pacific Standard Time");
- " In Pacific TZ : {0}" -f [System.TimeZoneInfo]::ConvertTimeToUtc($date1, $tz)
Labels:
powershell,
PowerShell scripts,
System.TimeZoneInfo
Monday, 9 August 2010
Divide-BigInteger.ps1
- <#
- .SYNOPSIS
- This script divides big integers using .NET Framework.
- .DESCRIPTION
- This script reimplements an MSDN Sample script using powershell. The
- script creates a dividor and an arry of dividends, then performs
- division operations several ways.
- .NOTES
- File Name : Divide-BigInteger.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .NET Framework 4.0 or higher
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.divide.aspx
- .EXAMPLE
- PSH [C:\foo]: . 'C:\Users\tfl\AppData\Local\Temp\Untitled7.ps1'
- Dividend: 680,564,693,277,057,719,623,408,366,969,033,850,880
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 7
- Using Division operator: 7
- Using DivRem method: 7, remainder 85,070,551,165,415,408,691,630,012,479,406,342,137
- Dividend: 90,612,345,123,875,509,091,827,560,007,100,099
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 0
- Using Division operator: 0
- Using DivRem method: 0, remainder 90,612,345,123,875,509,091,827,560,007,100,099
- Dividend: 1
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 0
- Using Division operator: 0
- Using DivRem method: 0, remainder 1
- Dividend: 19,807,040,619,342,712,359,383,728,129
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 0
- Using Division operator: 0
- Using DivRem method: 0, remainder 19,807,040,619,342,712,359,383,728,129
- Dividend: 85,070,591,730,234,615,847,396,907,784,232,501,250
- Divisor: 85,070,591,730,234,615,847,396,907,784,232,501,249
- Results:
- Using Divide method: 1
- Using Division operator: 1
- Using DivRem method: 1, remainder 1
- #>
- # Add namespace
- $r = [system.Reflection.Assembly]::LoadWithPartialName("System.Numerics")
- #Create a big integer divisor and an array of dividends
- $divisor = [system.numerics.BigInteger]::pow([Int64]::MaxValue, 2)
- $dividends = @()
- $dividends += [system.numerics.BigInteger]::Multiply(([system.numerics.BigInteger] [system.Single]::MaxValue), 2)
- $dividends += [system.numerics.BigInteger]::Parse("90612345123875509091827560007100099")
- $dividends += [system.numerics.BigInteger]::One
- $dividends += [system.numerics.BigInteger]::Multiply([Int32]::MaxValue, [Int64]::MaxValue)
- $dividends += $divisor + [system.numerics.BigInteger]::One
- #Divide each dividend by divisor in three different ways
- foreach ($dividend in $dividends) {
- "Dividend: {0:N0}" -f $dividend
- "Divisor: {0:N0}" -f $divisor
- "Results:"
- " Using Divide method: {0:N0}" -f [system.Numerics.BigInteger]::Divide($dividend, $divisor)
- " Using Division operator: {0:N0}" -f ($dividend/$divisor)
- $remainder = [system.Numerics.BigInteger]::Zero
- $quotient = [system.numerics.BigInteger]::DivRem($dividend, $divisor, [ref] $remainder)
- " Using DivRem method: {0:N0}, remainder {1:N0}" -f $quotient, $remainder
- ""
- }
Sunday, 8 August 2010
Get-DriveInfo.ps1
- <#
- .SYNOPSIS
- This script gets and displays basic information about drives on a local system.
- .DESCRIPTION
- This script uses the System.Io.DriveInfo's GetDrives method to get drive
- info which is then displayed. This is a re-implementation of an MSDN
- sample.
- .NOTES
- File Name : Get-DriveInfo.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/07/get-driveinfops1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
- http://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-DriveInfo.ps1'
- Drive C:\
- File type: Fixed
- Volume label:
- File system: NTFS
- Available space to current user: 6.75 gb
- Total available space: 6.75 gb
- Total size of drive: 48.828 gb
- Drive D:\
- File type: Fixed
- Volume label:
- File system: FAT32
- Available space to current user: 1.81 gb
- Total available space: 1.81 gb
- Total size of drive: 2.003 gb
- <ETC...>
- #>
- # Get Drive Info
- $allDrives = [system.Io.DriveInfo]::GetDrives()
- # Now display details
- foreach ($d in $allDrives) {
- "Drive {0}" -f $d.Name
- " File type: {0}" -f $d.DriveType
- if ($d.IsReady) {
- " Volume label: {0}" -f $d.VolumeLabel
- " File system: {0}" -f $d.DriveFormat
- $fs = $d.AvailableFreeSpace/1gb
- $tfs = $d.TotalFreeSpace/1gb
- $TS = $d.TotalSize/1gb
- " Available space to current user:{0, 15:n2} gb" -f $fs
- " Total available space: {0, 15:n2} gb" -f $tfs
- " Total size of drive: {0, 15:n3} gb" -f $ts
- }
- }
Labels:
powershell,
PowerShell scripts,
System.Io.DriveInfo
Wednesday, 4 August 2010
Get-MultiplyBigInteger.ps1
- <#
- .SYNOPSIS
- This script re-implements this MSDN Sample of
- multiplying a big integer.
- .DESCRIPTION
- This script first tries and fails to multiple a pair of large integers. The
- script catches the error and then used BigInteger.Multiply to multiply
- the two big itegers.
- .NOTES
- File Name : Get-MultiplyBigInteger.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 at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.multiply.aspx
- .EXAMPLE
- PSH [C:\foo]: .Get-MultiplyBigInteger.ps1
- Too big for long, try biginteger
- 12,193,263,111,263,526,900
- #>
- # Add System.Numerics namespace
- $r=[system.Reflection.Assembly]::LoadWithPartialName("System.Numerics")
- # Two big numbers
- $number1 = 1234567890
- $number2 = 9876543210
- # Try normal [long] then catch error and do biginteger
- try
- {
- [long] $product = $number1 * $number2
- }
- catch
- { "Too big for long, try biginteger"
- $product = New-Object System.Numerics.BigInteger
- $product = [System.Numerics.BigInteger]::Multiply($number1, $number2)
- $product.ToString("N0")
- }
Monday, 2 August 2010
Get-ConvertedTime.ps1
- <#
- .SYNOPSIS
- This script converts time using ConvertTime method.
- .DESCRIPTION
- This script re-implements an MSDN sample.
- .NOTES
- File Name : Get-ConvertedTime.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/08/get-convertedtimeps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/bb382835.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ConvertedTime.ps1
- Local time zone: (GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London
- Converted 1/1/2010 12:01:00 AM Unspecified to 12/31/2009 07:01:00 PM
- Converted 1/1/2010 12:01:00 AM Utc to 12/31/2009 07:01:00 PM
- Converted 1/1/2010 12:01:00 AM Local to 12/31/2009 07:01:00 PM
- Converted 11/6/2010 11:30:00 PM Unspecified to 11/6/2010 07:30:00 PM
- Converted 11/7/2010 02:30:00 AM Unspecified to 11/6/2010 10:30:00 PM
- #>
- #Create an array of times to convert
- [DateTime[]] $times = (New-Object system.dateTime 2010, 1, 1, 0, 1, 0),
- (New-Object system.DateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Utc)),
- (New-Object system.dateTime(2010, 1, 1, 0, 1, 0, [System.DateTimeKind]::Local)),
- (New-Object system.DateTime(2010, 11, 6, 23, 30, 0)),
- (New-Object system.DateTime(2010, 11, 7, 2, 30, 0) );
- # Retrieve the time zone for Eastern Standard Time (U.S. and Canada).
- try {
- $Est = [System.TimeZoneInfo]::FindSystemTimeZoneById("Eastern Standard Time");
- }
- catch {
- "Unable to retrieve the Eastern Standard time zone."
- return;
- }
- # Display the current time zone name.
- "Local time zone: {0}`n" -f [system.TimeZoneInfo]::Local.DisplayName
- # Convert and display each time in the $times array
- foreach ($timeToConvert in $times) {
- $TargetTime = [System.TimeZoneInfo]::ConvertTime($TimeToConvert, $Est)
- "Converted {0} {1} to {2}" -f $timeToConvert,$timeToConvert.Kind, $targetTime
- }
Labels:
ConvertTime,
powershell,
PowerShell scripts,
system.datetime
Sunday, 1 August 2010
Get-BigIntegerProperties.ps1
- <#
- .SYNOPSIS
- This script displays dynamic properties of a BigInteger
- .DESCRIPTION
- This script demonstates the properties on an instance of BigInteger
- .NOTES
- File Name : Get-BigIntegerProperties.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .NET Framework 4
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.numerics.biginteger_properties.aspx
- .EXAMPLE
- PSH [c:\foo]: .\Get-BigIntegerProperties.ps1
- Big Integer from 4096:
- IsPowerOfTwo : True
- IsZero : False
- IsOne : False
- IsEven : True
- Sign : 1
- #>
- # Add the .NET Version 4 System.Numerics.DLL
- Add-Type -Path "C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Numerics.dll"
- # Create a big integer then display it's key properties
- $BigInt = New-object System.Numerics.BigInteger 4096
- "Big Integer from 4096:"
- $BigInt | fl *
Labels:
.Net 4.0,
BigInteger,
powershell,
PowerShell scripts,
System.Numerics
Subscribe to:
Posts (Atom)