Last active
October 3, 2023 05:37
-
-
Save CodesOfRishi/ff4971791b32c2b217900c7c29298ba6 to your computer and use it in GitHub Desktop.
PowerShell Custom Module
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
# Location: ($env:PSModulePath | ForEach-Object { ([string]$_).Split(";")[0] })\RishiPowerShell | |
# Rishi K. (CodesOfRishi) | |
# Bulk nslookup | |
function Resolve-Host {#{{{ | |
param ( | |
[Parameter(Mandatory=$false)] | |
[string]$OutputSeparator = "`n", | |
[switch]$ChangeList, | |
[switch]$NoClipboard | |
) | |
if ($args[0]) {#{{{ | |
write-host | |
if ($args[0] -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' -And $args[0] -notmatch "[a-zA-Z]") { | |
# input is an IP address | |
$output = (Resolve-DnsName $args[0]).namehost | |
} | |
else { | |
# input is an not an IP address | |
$output = (Resolve-DnsName -Type A $args[0]).ipaddress | |
} | |
if ($output) { | |
# nslooup resolved something | |
$output = "$args - $output" | |
write-host $output | |
Set-Clipboard -Value $output # copy the output to the clipboard | |
write-host | |
} | |
return | |
}#}}} | |
New-Variable -Name 'file_location' -Value 'C:\Users\KIIT\temp\list.txt' -Option Constant | |
if ($ChangeList) { | |
# notepad $file_location | |
Start-Process notepad $file_location -NoNewWindow -Wait | |
} | |
write-host "Performing Nslookup in '$file_location'" -Foregroundcolor Green | |
write-host | |
[string[]]$lookup_list = Get-Content -Path $file_location | |
$output = $null | |
$progress_bar_curr = 0 | |
$progress_bar_max = $lookup_list.length | |
foreach ($i in $lookup_list) { | |
# progress bar start | |
$progress_bar_complete = (($progress_bar_curr / $progress_bar_max) * 100) | |
Write-Progress -Activity "Performing NSLookups" -CurrentOperation "Looking up $i" -Status "$progress_bar_complete% complete" -PercentComplete $progress_bar_complete | |
$progress_bar_curr++ | |
# progress bar end | |
$i = $i.trim() | |
if (!$i) { | |
# found empty line | |
break | |
} | |
if ($i -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' -And $i -notmatch "[a-zA-Z]") { | |
# input is an IP address | |
$resolved_output = (Resolve-DnsName $i).namehost | |
} | |
else { | |
# input is an not an IP address | |
$resolved_output = (Resolve-DnsName -Type A $i).ipaddress | |
} | |
if ($resolved_output) { | |
# nslookup resolved something | |
$output = $output + "$i - " | |
foreach ($j in $resolved_output) { | |
$output = $output + "$j " | |
} | |
$output = $output + "$OutputSeparator" | |
} | |
else { | |
# nslookup didn't resolve anything | |
write-host "$i - " -nonewline | |
write-host "NO RESULT!" -Backgroundcolor Red | |
write-host | |
} | |
} | |
if ($output) { | |
# remove $OutputSeparator from the end of the $output | |
if ($output.Substring($output.Length - $OutputSeparator.Length) -Match "$OutputSeparator") { $output = $output.remove($output.Length - $OutputSeparator.Length, $OutputSeparator.Length) } | |
$output | |
if (!$NoClipboard) { | |
Set-Clipboard -Value $output # copy the output to the clipboard | |
} | |
} | |
}#}}} | |
# Utility function for Sanitization | |
function ConvertSanitize { | |
Param ( | |
[Parameter(Position=0)] | |
[string] | |
$url, | |
[Parameter(Position=1)] | |
[string] | |
$mode = "toggle" | |
) | |
if ($url) { $url=$url.Trim() } | |
if (!$url) { return } | |
function isSanitize {#{{{ | |
Param ( | |
[string]$url | |
) | |
# return ($url -Match '\[\.\]') | |
if ($url -Match '\[\.\]') { return $True } | |
else { return $False } | |
}#}}} | |
function Infect {#{{{ | |
Param ( | |
[string]$url | |
) | |
$url=$url.replace('[.]','.') | |
$url=$url.replace('[:]',':') | |
return $url | |
}#}}} | |
function Sanitize {#{{{ | |
Param ( | |
[string]$url | |
) | |
if ($url -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' -And $url -notmatch "[a-zA-Z]") { | |
# Input is an IP | |
$url=$url -replace "(.*)\.(.*)", '$1[.]$2' | |
} | |
else { | |
$url=$url.replace('.','[.]') | |
$url=$url.replace('[[.]]','[.]') | |
$url=$url.replace(':','[:]') | |
$url=$url.replace('[[:]]','[:]') | |
} | |
return $url | |
}#}}} | |
if (($mode -Match "infect") -Or ($mode -Match "inf")) { | |
if (isSanitize($url)) { $url = Infect($url) } | |
} | |
elseif (($mode -Match "sanitize") -Or ($mode -Match "san")) { | |
if (!(isSanitize($url))) { $url = Sanitize($url) } | |
} | |
elseif (($mode -Match "toggle") -Or ($mode -Match "tog")) { | |
if (isSanitize($url)) { | |
# Already sanitized | |
$url = Infect($url) | |
} | |
else { | |
# Needs to sanitize | |
$url = Sanitize($url) | |
} | |
} | |
else { | |
# write-error -Message "ERROR: Invalid Mode!" -Category InvalidArgument | |
throw "ERROR: Invalid Mode!" | |
} | |
return $url | |
} | |
# URL/IPv4 address (Un)Sanitization | |
function Format-Sanitize { | |
param ( | |
[switch]$Bulk, | |
[switch]$NoClipboard, | |
[switch]$ChangeList, | |
[Parameter(Mandatory=$false)] | |
[string]$OutputSeparator = "`n", | |
[Parameter(Mandatory=$false)] | |
[ValidateSet('toggle', 'tog', 'sanitize', 'san', 'infect', 'inf')] | |
[string]$Mode = "toggle", | |
[Parameter(Mandatory=$false)] | |
[string]$Url = $null | |
) | |
if ($Url -And $Bulk) {#{{{ | |
write-host "NOTE: '-Url' is not supposed to be used with '-Bulk'" -ForegroundColor DarkYellow | |
write-host | |
} | |
elseif ($Bulk) { | |
New-Variable -Name 'file_location' -Value 'C:\Users\KIIT\temp\list.txt' -Option Constant | |
if ($ChangeList) { | |
# notepad $file_location | |
Start-Process notepad $file_location -NoNewWindow -Wait | |
} | |
$outpout = $null | |
[string[]]$lookup_list = Get-Content -Path $file_location | |
if (!$NoClipboard) { $sanitize_start = $false } | |
$output = $NULL | |
foreach ($i in $lookup_list) { | |
$i = ConvertSanitize $i $Mode | |
if (!$i) { break } | |
if ($output) { | |
$output = $output + $i + "$OutputSeparator" | |
} | |
else { | |
$output = $i + "$OutputSeparator" | |
} | |
} | |
if ($output) { | |
# remove $OutputSeparator from the end of the $output | |
if ($output.Substring($output.Length - $OutputSeparator.Length) -Match "$OutputSeparator") { $output = $output.remove($output.Length - $OutputSeparator.Length, $OutputSeparator.Length) } | |
$output | |
if (!$NoClipboard) { | |
Set-Clipboard -Value $output # copy the output to the clipboard | |
} | |
} | |
return | |
} | |
elseif ($ChangeList) { | |
New-Variable -Name 'file_location' -Value 'C:\Users\KIIT\temp\list.txt' -Option Constant | |
# notepad $file_location | |
Start-Process notepad $file_location -NoNewWindow -Wait | |
return | |
}#}}} | |
if ($NoClipboard -Or $OutputSeparator -Or $Mode) { | |
write-host "NOTE: '-NoClipboard', '-OutputSeparator' and '-Mode' works with only '-Bulk'!" -ForegroundColor DarkYellow | |
write-host | |
} | |
New-Variable -Name url_provided -Value $True | |
while (1) {#{{{ | |
if (!($Url)) { | |
$Url = $(write-host "Enter URL/IP >> " -nonewline -ForegroundColor Cyan; Read-Host) | |
$url_provided = $False | |
} | |
$Url = ConvertSanitize $Url | |
if ($Url) { $Url = $Url.Trim() } | |
if (!$Url) { continue } | |
if ($Url -match '\[\.\]') { | |
write-host | |
write-host "SANITIZED ->" -nonewline -Backgroundcolor DarkGreen | |
write-host " $Url" | |
write-host | |
} | |
` else { | |
write-host | |
write-host "UN-SANITIZED ->" -nonewline -Backgroundcolor DarkRed | |
write-host " $Url" | |
write-host | |
} | |
Set-Clipboard -Value $Url # copy the URL/IP to the clipboard | |
if ($url_provided) { break } | |
$Url = $null | |
}#}}} | |
} | |
# Time Zone | |
# Set-TimeZone -Id "India Standard Time" | |
# Set-TimeZone -Id UTC | |
# Set-TimeZone -Id "Eastern Standard Time" | |
function Set-Time_Zone { | |
param ( | |
[Parameter(Mandatory=$false)] | |
[ValidateSet('IST', 'India Standard Time', 'EST', 'Eastern Standard Time', 'UTC', 'Coordinated Universal Time')] | |
[string]$Zone = "Eastern Standard Time" | |
) | |
if ($Zone -Match "EST" -Or $Zone -Match "Eastern Standard Time") { | |
write-host "Changing the System's time zone to Eastern Standard Time." -Foregroundcolor Yellow | |
Set-TimeZone -Id "Eastern Standard Time" | |
} | |
elseif ($Zone -Match "IST" -Or $Zone -Match "India Standard Time") { | |
write-host "Changing the System's time zone to India Standard Time." -Foregroundcolor Yellow | |
Set-TimeZone -Id "India Standard Time" | |
} | |
elseif ($Zone -Match "UTC" -Or $Zone -Match "Coordinated Universal Time") { | |
write-host "Changing the System's time zone to Coordinated Universal Time." -Foregroundcolor Yellow | |
Set-TimeZone -Id UTC | |
} | |
else { | |
write-host "Changing the System's time zone to $Zone." -Foregroundcolor Yellow | |
Set-TimeZone -Id "$Zone" | |
} | |
} | |
Set-Time_Zone -Zone "EST" | |
Set-Alias -Name rh -Value Resolve-Host | |
Set-Alias -Name fs -Value Format-Sanitize | |
Set-Alias -Name tz -Value Set-Time_Zone | |
Export-ModuleMember -Function Resolve-Host, Format-Sanitize, Set-Time_Zone -Alias rh, fs, tz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment