Skip to content

Commit

Permalink
Password generator (#8)
Browse files Browse the repository at this point in the history
* Initial simple password generator

* New-PasswordUsingWords

* Whitespace

* Shouldprocess
  • Loading branch information
KevinKvissberg authored Mar 12, 2024
1 parent 4e09982 commit 6fe9085
Show file tree
Hide file tree
Showing 8 changed files with 2,641 additions and 30 deletions.
2,446 changes: 2,446 additions & 0 deletions pewtools/Data/wordlist.txt

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pewtools/Private/converters.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function Convert-DateFormat {
function Convert-DateFormat {
<#
.SYNOPSIS
Converts a date from one format to another.
.DESCRIPTION
This function converts a date from one format to another. The input date format is 'yyyyMMdd' and the output date format is 'yyyy-MM-dd'.
.PARAMETER InputDate
The input date in the format 'yyyyMMdd'.
.EXAMPLE
Convert-DateFormat -InputDate 20210101
#>
Expand Down
10 changes: 5 additions & 5 deletions pewtools/Public/activedirectory-tools.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
function Initialize-GPUpdate {
function Initialize-GPUpdate {
<#
.SYNOPSIS
Use alias gpu for writing the command gpupdate faster.
.DESCRIPTION
This function initializes a Group Policy update with optional parameters such as force, target, and boot.
.PARAMETER noForce
Suppresses the force update if this switch is present.
.PARAMETER target
Specifies the target for the Group Policy update. Possible values are "Computer", "User", or "Both" (default).
.PARAMETER boot
Performs a boot-time Group Policy update if this switch is present.
#>
Expand Down
165 changes: 165 additions & 0 deletions pewtools/Public/generator-utils.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
Function New-Password {
<#
.SYNOPSIS
Generates a random password
.DESCRIPTION
Generates a random password with the specified length and constraints.
.PARAMETER Length
The length of the password
.PARAMETER NoSpecial
If set, the password will not contain special characters
.PARAMETER NoNumbers
If set, the password will not contain numbers
.PARAMETER OnlyNumbers
If set, the password will only contain numbers
.PARAMETER OnlyLowercase
If set, the password will only contain lowercase letters
.PARAMETER OnlyUppercase
If set, the password will only contain uppercase letters
.PARAMETER AllowSimilarChars
If set, the password will contain similar characters (e.g. 1, l, I, 0, O, etc.)
.PARAMETER ListLength
The number of passwords to generate
.EXAMPLE
New-Password -Length 16 -NoSpecial -ListLength 5
#>
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(Position = 0)]
[int]$Length = 12,

[Parameter()]
[switch]$NoSpecial,

[Parameter()]
[switch]$NoNumbers,

[Parameter()]
[switch]$OnlyNumbers,

[Parameter()]
[switch]$OnlyLowercase,

[Parameter()]
[switch]$OnlyUppercase,

[Parameter()]
[switch]$AllowSimilarChars,

[Parameter()]
[int]$ListLength = 1
)

# Check constraints
if ($OnlyNumbers -and $NoNumbers) {
throw "OnlyNumbers and NoNumbers cannot be used together."
}
if ($OnlyLowercase -and $OnlyUppercase) {
throw "OnlyLowercase and OnlyUppercase cannot be used together."
}

# Declare variables
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+/.,<>?;:[]{}-="

# Remove characters based on constraints
if ($NoSpecial) {
$chars = $chars -replace "[\W]", ""
$chars = $chars -replace "[-_]", ""
}
if (!$AllowSimilarChars) {
$chars = $chars -replace "[il1Lo0OvVwW]", ""
}
if ($NoNumbers) {
$chars = $chars -replace "[0-9]", ""
}
if ($OnlyNumbers) {
$chars = "0123456789"
}
elseif ($OnlyUppercase) {
$chars = $chars.ToUpper()
}
elseif ($OnlyLowercase) {
$chars = $chars.ToLower()
}

# Loop through the amount of passwords to generate
for ($i = 0; $i -lt $ListLength; $i++) {
# Declare variables
$password = ""

# Loop through the length of the password
for ($k = 0; $k -lt $Length; $k++) {
# Add a random character from the character list to the password
$password += $chars[(Get-Random -Minimum 0 -Maximum $chars.Length)]
}

# Output the password
if ($PSCmdlet.ShouldProcess("Localhost", "Generate password")) {
Write-Output $password
}
}
}

Function New-PasswordUsingWord {
<#
.SYNOPSIS
Generates a random password using words
.DESCRIPTION
Generates a random password using words from a word list.
.PARAMETER Words
The number of words to use in the password
.PARAMETER ListLength
The number of passwords to generate
.EXAMPLE
New-PasswordWords -Length 5 -ListLength 5
#>
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(Position = 0)]
[ValidateRange(1, [int]::MaxValue)]
[int]$Words = 3,

[Parameter()]
[int]$ListLength = 1,

[Parameter()]
[string]$divider = "-"
)

# Load word list
$wordlist = Get-Content -Path "$PSScriptRoot\..\Data\wordlist.txt"

# Loop through the amount of passwords to generate
for ($i = 0; $i -lt $ListLength; $i++) {
# Declare variables
$password = ""
$wordsToUse = @()

# Loop through the amount of words to use
for ($k = 0; $wordsToUse.count -lt $Words; $k++) {
# Add a random word from the word list to the array
$wordsToUse += $wordlist[(Get-Random -Minimum 0 -Maximum $wordlist.Length)]
}
# Join the words together with the divider
$password = $wordsToUse -join $divider

# Output the password
if ($PSCmdlet.ShouldProcess("Localhost", "Generate password with words")) {
Write-Output $password
}
}
}
34 changes: 17 additions & 17 deletions pewtools/Public/system-tools.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#region SystemInfo Getters
#region SystemInfo Getters
function Get-LocalDetails {
<#
.SYNOPSIS
Retrieves local system details including hostname, username, uptime, domain, system manufacturer, OS install date, network information, default gateway, DNS servers, internet access, and domain access.
.DESCRIPTION
The Get-LocalDetails function retrieves various details about the local system and returns them as an object. The function collects information such as the hostname, username, uptime, domain, system manufacturer, OS install date, network information, default gateway, DNS servers, internet access, and domain access.
.PARAMETER None
This function does not accept any parameters.
.EXAMPLE
Get-LocalDetails
This example demonstrates how to use the Get-LocalDetails function to retrieve local system details.
.OUTPUTS
The function returns an object with the following properties:
- Hostname: The hostname of the local system.
Expand Down Expand Up @@ -87,7 +87,7 @@ Function Get-DiskUsage {
<#
.SYNOPSIS
Gets disk usage information for the local system.
.DESCRIPTION
This function retrieves disk usage information for the local system, including details such as drive letter, free space, total size, and used percentage.
#>
Expand All @@ -114,7 +114,7 @@ function Get-Apps {
<#
.SYNOPSIS
Retrieves information about installed software on a Windows system.
.DESCRIPTION
This function queries the Windows Registry to gather information about installed software,
focusing on the "Uninstall" key in the registry.
Expand Down Expand Up @@ -156,20 +156,20 @@ function Trace-Eventlog {
<#
.SYNOPSIS
Continuously monitors and retrieves new events from the specified event log with optional filtering.
.DESCRIPTION
This function continuously monitors and retrieves new events from the specified event log,
allowing optional filtering by source, message, and entry type.
.PARAMETER LogName
Specifies the name of the event log to monitor. Default is "Application".
.PARAMETER Source
Specifies the event source. Default is "*".
.PARAMETER Message
Specifies a message filter for the events. Default is "*".
.PARAMETER EntryType
Specifies the entry types to include (e.g., "Error", "Warning", "Information", "SuccessAudit", "FailureAudit").
#>
Expand Down Expand Up @@ -255,7 +255,7 @@ function Get-BootHistory {
<#
.SYNOPSIS
Retrieves information about system boot history.
.DESCRIPTION
This function retrieves information about system boot history by querying the System event log for event IDs 1074 and 6005.
It provides details such as timestamp, user, reason/application for the boot, and the associated action.
Expand Down Expand Up @@ -323,14 +323,14 @@ function Get-CertificateExpiry {
<#
.SYNOPSIS
Retrieves information about certificate expiry for the specified target.
.DESCRIPTION
This function retrieves information about certificate expiry for either the local machine or a remote target.
It checks the certificates in the "My" store and provides details such as subject, thumbprint, expiration date, and whether it will expire soon.
.PARAMETER target
Specifies the target machine. If not provided, it checks certificates on the local machine.
.PARAMETER credentials
Specifies the credentials to be used when checking certificates on a remote machine.
#>
Expand Down
4 changes: 2 additions & 2 deletions pewtools/Public/testing-tools.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#region Broad test functions
#region Broad test functions
function Test-ServerHealth {
<#
.SYNOPSIS
Tests the health of the server by checking disk usage, gateway ping, internet connectivity, and DNS resolution.
.DESCRIPTION
This function tests the health of the server by performing various checks such as disk usage, gateway ping, internet connectivity, and DNS resolution.
#>
Expand Down
Binary file modified pewtools/pewtools.psd1
Binary file not shown.
4 changes: 2 additions & 2 deletions pewtools/pewtools.psm1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<#
<#
.SYNOPSIS
PewTools is a collection of PowerShell functions and scripts that I use to make my life easier.
.Description
PewTools is a collection of PowerShell functions and scripts that I use to make my life easier. It is a collection of tools for managing and maintaining a Windows environment.
It uses sub scripts to organize the functions into categories.
It uses sub scripts to organize the functions into categories.
The folder structure is as follows:
- Public
- Contains exported functions and scripts that are intended to be used by the end user
Expand Down

0 comments on commit 6fe9085

Please sign in to comment.