function Get-Md5Hash
{
<#
.SYNOPSIS
This function returns the Md5 Hash for a given file.
.DESCRIPTION
This function returns the Md5 Hash for a given file.
You can Pipe in files or just use one at a time.
.EXAMPLE
PS C:\> $ReturnedHash = Get-Md5Hash "c:\test\MyFile.txt"
.EXAMPLE
PS C:\> @("C:\TEST\TestFile1.txt","C:\TEST\TestFile2.txt","C:\TEST\TestFile3.txt") | Get-Md5Hash
.INPUTS
A file name/location.
.OUTPUTS
The Md5 Hash of the given file is returned.
.NOTES
Function Name: Get-Md5Hash
Original Author: Norman Skinner (normans)
Original Created on: 09/26/2013
Version: 1.0.0.0
HISTORY:
===========#==============#======================================
Date | User | Description
-----------+--------------+--------------------------------------
09/26/2013 | normans | Created script
-----------+--------------+--------------------------------------
| |
-----------+--------------+--------------------------------------
| |
-----------+--------------+--------------------------------------
#>
param
(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string]
# This the file for getting the Md5 hash.
$File
)
PROCESS
{
$MD5object = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$Md5Hash = [System.BitConverter]::ToString($MD5object.ComputeHash([System.IO.File]::ReadAllBytes($File)))
return $Md5Hash
}
}
Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts
Thursday, September 26, 2013
MD5 Hash function
Had to get the MD5 Hash on some files and needed a light weight script. So here it is:
Wednesday, February 22, 2012
Check if Running As Administrator
Have you ever had a script that requires it be run in “Run As Administrator” mode and needed a function to check that? When I run into this I use the following function:
function Confirm-RunningAsAdministrator
{
<#
.SYNOPSIS
This function will return a bool, "True" if running as administrator,
"False" if not.
.DESCRIPTION
You can use this script to check is you are currently running as
administrator. It is best used when your script needs to verify
that this is the case.
.EXAMPLE
if (Confirm-RunningAsAdministrator)
{
Write-Host "Running as an administrator."
}
else
{
Write-Host "NOT running as an administrator."
}
-----------------------------------------------------------
If running as an administrator, this would produce:
Running as an administrator.
.INPUTS
None.
.OUTPUTS
[bool] - "True" if running as administrator, "False" if not.
.NOTES
Original Function name: Confirm-RunningAsAdministrator.ps1
Original Author: Norman Skinner
Original Created on: 12/05/2012
HISTORY:
===========#================#=========#============================
Date | User | Version | Description
-----------+----------------+---------+----------------------------
12/05/2011 | Norman Skinner | 1.0.0.0 | Created script
-----------+----------------+---------+----------------------------
| | |
-----------+----------------+---------+----------------------------
#>
$WinIdentity = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal($WinIdentity)
$AdministratorRole = [Security.Principal.WindowsBuiltInRole]::Administrator
return $currentPrincipal.IsInRole($AdministratorRole)
}
Tuesday, February 21, 2012
Write-NoteStart
Once in a while I have to output information from a script that will say what I am doing and then if it completed or failed. In most cases it would look horrible on the screen. Like so:
Checking network connection...completed.
Checking file...completed.
Writing network information to file...completed.
This bugged me to no end; so I wrote the “Write-NoteStart” function to make it easier for me to sleep at night. When using this function the output looks more like this:
Checking network connection.............completed.
Checking file...........................completed.
Writing network information to file.....completed.
Here is the function:
Checking network connection...completed.
Checking file...completed.
Writing network information to file...completed.
This bugged me to no end; so I wrote the “Write-NoteStart” function to make it easier for me to sleep at night. When using this function the output looks more like this:
Checking network connection.............completed.
Checking file...........................completed.
Writing network information to file.....completed.
Here is the function:
function Write-NoteStart
{
<#
.SYNOPSIS
This function will place periods after writing a string and not write a new line.
.DESCRIPTION
This script is used to produce dots/periods after a given string for aligning
results. You can adjust the position it should add dots up to.
.EXAMPLE
Write-NoteStart "Checking status" -DotTo 40
Write-Host "completed."
Write-NoteStart "Checking your connection" -DotTo 40
Write-Host "completed."
-----------------------------------------------------------
This would produce:
Checking status.........................completed.
Checking your connection................completed.
.INPUTS
A string and an int.
.OUTPUTS
Write-Host to the screen with your string followed by periods.
.NOTES
Original Function name: Write-NoteStart.ps1
Original Author: Norman Skinner
Original Created on: 02/01/2012
Version: 1.0.0.0
#>
param (
[string]
# The string for the start of the line.
$Note = "No data",
[int]
# What line position to run dots to.
$DotTo = 60
)
if ($Note.Length -lt $DotTo)
{
Write-Host $Note -NoNewline
for ($i = $Note.Length;$i -lt $DotTo; $i++)
{
Write-Host "." -NoNewline
}
}
else
{
Write-Host $Note
}
}
Sunday, February 19, 2012
PowerShell: Try, Catch, and Finally
This is the example I like to refer to for my PowerShell try/catch/finally blocks.
###---------------------------------------------
### A try/catch/finally example.
###---------------------------------------------
try
{
Write-Host "Try this."
$Result = 1/$Zero
}
catch [System.DivideByZeroException]
{
Write-Host "Caught divid by zero exception."
}
catch [System.SystemException]
{
# This catch will catch every thing else.
Write-Host "Caught base exception."
}
finally
{
Write-Host "Finally this."
}
Subscribe to:
Posts (Atom)