Created
January 21, 2015 17:06
-
-
Save Paebbels/2e918b3b0150ce48803e to your computer and use it in GitHub Desktop.
My PowerShell profile scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save this file as: C:\Users\<Username>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 | |
$Executables = @{ | |
'Notepad++'="C:\Program Files (x86)\Notepad++\notepad++.exe" | |
} | |
Write-Host "--------------------------------------------------------------------------------" | |
if (Test-Path $Executables['Notepad++']) | |
{ # function Edit | |
# ------------------------------------------------------------------------------ | |
<# | |
.SYNOPSIS | |
Opens a file. | |
.DESCRIPTION | |
Opens a file with Notepad++ | |
.PARAMETER Path | |
Path to a file which should be opened. | |
.EXAMPLE | |
edit myfile.txt | |
#> | |
function Edit([Parameter(Mandatory=$True,Position=1)][string]$Path) | |
{ & $Executables['Notepad++'] "$Path" } | |
} | |
# function WaitForAnyKey | |
# ------------------------------------------------------------------------------ | |
<# | |
.SYNOPSIS | |
Waits for a key press event. | |
.DESCRIPTION | |
Prints a messsage to press any key to continue. | |
.PARAMETER -NoMessage | |
Disable message text | |
.EXAMPLE | |
WaitForAnyKey | |
#> | |
function WaitForAnyKey([Parameter(Mandatory=$True,Position=1)][string]$NoMessage) | |
{ if (-not $NoMessage) | |
{ Write-Host "Press any key to continue..." } | |
[void]($Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")) | |
$Host.UI.RawUI.FlushInputBuffer() | |
} | |
# function Get-HexDump | |
# ------------------------------------------------------------------------------ | |
<# | |
.SYNOPSIS | |
Outputs the contents of a file as a hex dump. | |
.DESCRIPTION | |
ToDo | |
.PARAMETER Path | |
Path to the file, which should be formatted as hex dump. | |
#> | |
function Get-HexDump([parameter(Position=0,Mandatory=$TRUE)][String] $Path) | |
{ if ( -not (Test-Path -LiteralPath $Path) ) | |
{ Write-Error "Path '$Path' not found." -Category ObjectNotFound | |
exit | |
} | |
$item = Get-Item -LiteralPath $Path -Force | |
if ( -not ($? -and ($item -is [System.IO.FileInfo]))) | |
{ Write-Error "'$Path' is not a file in the file system." -Category InvalidType | |
exit | |
} | |
if ($item.Length -gt [UInt32]::MaxValue) | |
{ Write-Error "'$Path' is too large." -Category OpenError | |
exit | |
} | |
# configure function here | |
$BytesPerLine = 32 | |
# local variables | |
[UInt32] $BufferSize = 64 * 1kB | |
[UInt32] $FileOffset = 0 | |
try | |
{ $Stream = [System.IO.File]::OpenRead($item.FullName) | |
$Buffer = New-Object Byte[] $BufferSize | |
Write-Output | |
Write-Output "Address 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F Text" | |
Write-Output "--------------------------------------------------------------------------------------------------------------------------------------------" | |
while ($stream.Position -lt $stream.Length) | |
{ # Read BufferSize bytes into Buffer | |
$BytesRead = $stream.Read($Buffer, 0, $BufferSize) | |
for ($LineNumber = 0; $LineNumber -lt [Math]::Floor($BytesRead / $BytesPerLine); $LineNumber++) | |
{ $Line = $Buffer[($LineNumber * $BytesPerLine)..(($LineNumber * $BytesPerLine) + $BytesPerLine - 1)] | |
# format hexdump | |
$HexOutput = "{0:X8} {01:X2} {02:X2} {03:X2} {04:X2} {05:X2} {06:X2} {07:X2} {08:X2} {09:X2} {10:X2} {11:X2} {12:X2} {13:X2} {14:X2} {15:X2} {16:X2} {17:X2} {18:X2} {19:X2} {20:X2} {21:X2} {22:X2} {23:X2} {24:X2} {25:X2} {26:X2} {27:X2} {28:X2} {29:X2} {30:X2} {31:X2} {32:X2}" -f (, $FileOffset + $Line) | |
# format normal text | |
$TextOutput = "" | |
foreach ($Byte in $Line) | |
{ if (($Byte -ge 32) -and ($byte -le 126)) | |
{ $TextOutput += [Char] $Byte } | |
else | |
{ $TextOutput += "." } # unprintable char | |
} | |
Write-Output ("{0} {1}" -f $HexOutput,$TextOutput) | |
$FileOffset += $BytesPerLine | |
} | |
# Process bytes from end of file when file size not multiple of $bytesPerLine. | |
if ($BytesRead % $BytesPerLine -ne 0 ) | |
{ $Line = $Buffer[($LineNumber * $BytesPerLine)..($BytesRead - 1)] | |
$HexOutput = "{0:X8} " -f $FileOffset | |
$TextOutput = "" | |
foreach ($Byte in $Line) | |
{ $HexOutput += "{0:X2} " -f $Byte | |
if (($Byte -ge 32) -and ($byte -le 126)) | |
{ $TextOutput += [Char] $Byte } | |
else | |
{ $TextOutput += "." } # unprintable char | |
} | |
# PadRight needed to align the output. | |
Write-Output ("{0} {1}" -f $HexOutput.PadRight(106), $TextOutput) | |
} | |
Write-Progress -activity "Get-HexDump" -Status ("Dumping file '{0}'" -f $item.FullName) -PercentComplete (($FileOffset / $stream.Length) * 100) | |
} # while | |
} # try | |
catch [System.Management.Automation.MethodInvocationException] | |
{ throw $_ } | |
finally | |
{ $stream.Close() } | |
} # function | |
function Get-PSVersion() | |
{ if (-not (Test-Path variable:global:PSVersionTable)) { return "1.0" } | |
return $PSVersionTable.PSVersion.ToString() | |
} # function | |
# Load extensions | |
# ------------------------------------------------------------------------------ | |
Write-Host "Loading PowerShell Community Extensions (PSCX - Version 3.1)... " -NoNewline | |
if ((Get-Module -ListAvailable | Where {$_.Name -like "PSCX"}).Version -ge "3.1.0.0") | |
{ Import-Module Pscx | |
Write-Host "[Done]" -ForegroundColor Green | |
} | |
else | |
{ Write-Host "[FAILED]" -ForegroundColor RED } | |
# load file with an Get-Passwort function | |
. ($PSScriptRoot + "\New-Password.ps1") | |
# register aliases | |
# ------------------------------------------------------------------------------ | |
Write-Host "Registering more aliases... " -NoNewline | |
Set-Alias ll Get-ChildItem | |
if (Test-Path $Executables['Notepad++']) {Set-Alias npp Edit } | |
Write-Host "[Done]" -ForegroundColor Green | |
Write-Host "--------------------------------------------------------------------------------" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-Password { | |
<# | |
.Synopsis | |
Generates one or more complex passwords designed to fulfill the requirements for Active Directory | |
.DESCRIPTION | |
Generates one or more complex passwords designed to fulfill the requirements for Active Directory | |
.EXAMPLE | |
New-Password | |
C&3SX6Kn | |
Will generate one password with a length between 8 and 12 chars. | |
.EXAMPLE | |
New-Password -MinPasswordLength 8 -MaxPasswordLength 12 -Count 4 | |
7d&5cnaB | |
!Bh776T"Fw | |
9"C"RxKcY | |
%mtM7#9LQ9h | |
Will generate four passwords, each with a length of between 8 and 12 chars. | |
.EXAMPLE | |
New-Password -InputStrings abc, ABC, 123 -PasswordLength 4 | |
3ABa | |
Generates a password with a length of 4 containing atleast one char from each InputString | |
.OUTPUTS | |
[String] | |
.NOTES | |
Written by Simon Wåhlin, blog.simonw.se | |
I take no responsibility for any issues caused by this script. | |
.FUNCTIONALITY | |
Generates random passwords | |
.LINK | |
http://blog.simonw.se/powershell-generating-random-password-for-active-directory/ | |
#> | |
[CmdletBinding(DefaultParameterSetName='FixedLength',ConfirmImpact='None')] | |
[OutputType([String])] | |
Param | |
( | |
# Specifies minimum password length | |
[Parameter(Mandatory=$false, | |
ParameterSetName='RandomLength')] | |
[ValidateScript({$_ -gt 0})] | |
[Alias("Min")] | |
[int]$MinPasswordLength = 8, | |
# Specifies maximum password length | |
[Parameter(Mandatory=$false, | |
ParameterSetName='RandomLength')] | |
[ValidateScript({ | |
if($_ -ge $MinPasswordLength){$true} | |
else{Throw 'Max value cannot be lesser than min value.'}})] | |
[Alias("Max")] | |
[int]$MaxPasswordLength = 12, | |
# Specifies a fixed password length | |
[Parameter(Mandatory=$false, | |
ParameterSetName='FixedLength')] | |
[ValidateRange(1,[int]::MaxValue)] | |
[int]$PasswordLength = 20, | |
# Specifies an array of strings containing charactergroups from which the password will be generated. | |
# At least one char from each group (string) will be used. | |
[String[]]$InputStrings = @('abcdefghijkmnopqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789'), | |
# Specifies number of passwords to generate. | |
[ValidateRange(1,[int]::MaxValue)] | |
[int]$Count = 1 | |
) | |
Begin { | |
Function Get-Seed{ | |
# Generate a seed for randomization | |
$RandomBytes = New-Object -TypeName 'System.Byte[]' 4 | |
$Random = New-Object -TypeName 'System.Security.Cryptography.RNGCryptoServiceProvider' | |
$Random.GetBytes($RandomBytes) | |
[BitConverter]::ToUInt32($RandomBytes, 0) | |
} | |
} | |
Process { | |
For($iteration = 1;$iteration -le $Count; $iteration++){ | |
$Password = @{} | |
# Create char arrays containing groups of possible chars | |
[char[][]]$CharGroups = $InputStrings | |
# Create char array containing all chars | |
$AllChars = $CharGroups | ForEach-Object {[Char[]]$_} | |
# Set password length | |
if($PSCmdlet.ParameterSetName -eq 'RandomLength') | |
{ | |
if($MinPasswordLength -eq $MaxPasswordLength) { | |
# If password length is set, use set length | |
$PasswordLength = $MinPasswordLength | |
} | |
else { | |
# Otherwise randomize password length | |
$PasswordLength = ((Get-Seed) % ($MaxPasswordLength + 1 - $MinPasswordLength)) + $MinPasswordLength | |
} | |
} | |
# Randomize one char from each group | |
foreach($Group in $CharGroups) { | |
if($Password.Count -lt $PasswordLength) { | |
$Password.Add((Get-Seed),$Group[((Get-Seed) % $Group.Count)]) | |
} | |
} | |
# Fill out with chars from $AllChars | |
for($i=$Password.Count;$i -lt $PasswordLength;$i++) { | |
$Password.Add((Get-Seed),$AllChars[((Get-Seed) % $AllChars.Count)]) | |
} | |
Write-Output -InputObject $(-join$password.Values) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment