Skip to content

Commit

Permalink
code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasholla committed Jun 24, 2024
1 parent da1f526 commit 9965864
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 164 deletions.
12 changes: 4 additions & 8 deletions Scripts/PowerShellHere.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\.ps1))
{
if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\.ps1)) {
New-Item -Path Registry::HKEY_CLASSES_ROOT\.ps1 -Force
}

if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1))
{
if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1)) {
New-Item -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1 -Force
}
New-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1 -Name EditFlags -Type DWord -Value 131072 -Force
New-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1 -Name FriendlyTypeName -Type ExpandString -Value "@`"%systemroot%\system32\windowspowershell\v1.0\powershell.exe`",-103" -Force

if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon))
{
if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon)) {
New-Item -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon -Force
}
New-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon -Name "(Default)" -Type String -Value "`"C:\Windows\System32\WindowsPowerShell\v1.0\powershell_ise.exe`",1" -Force

if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs\Command))
{
if (-not (Test-Path -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs\Command)) {
New-Item -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs\Command -Force
}
New-ItemProperty -Path Registry::HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs -Name HasLUAShield -Type String -Value "" -Force
Expand Down
20 changes: 12 additions & 8 deletions Scripts/hw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ function GetCPUTemperature {
[int]$IntTemp = Get-Content "/sys/class/thermal/thermal_zone0/temp"
$temp = [math]::round($IntTemp / 1000.0, 1)
}
} else {
}
else {
$objects = Get-WmiObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
foreach ($object in $objects) {
$highPrec = $object.HighPrecisionTemperature
Expand Down Expand Up @@ -77,13 +78,16 @@ $socket = "$($details.SocketDesignation) socket, "
$celsius = GetCPUTemperature
if ($celsius -eq 99999.9) {
$temp = "no temp"
} elseif ($celsius -gt 50) {
}
elseif ($celsius -gt 50) {
$temp = "$($celsius)°C"
$status = "⚠️"
} elseif ($celsius -lt 0) {
}
elseif ($celsius -lt 0) {
$temp = "$($celsius)°C"
$status = "⚠️"
} else {
}
else {
$temp = "$($celsius)°C"
}
foreach ($cpu in $cpus) {
Expand Down Expand Up @@ -135,10 +139,10 @@ foreach ($vol in $volumes) {

# Firewall status for all profiles (Domain, Private, Public)
$firewallStatus = Get-NetFirewallProfile | ForEach-Object {
[PSCustomObject]@{
Profile = $_.Name
Enabled = $_.Enabled
}
[PSCustomObject]@{
Profile = $_.Name
Enabled = $_.Enabled
}
}
Write-Host "Firewall: " -ForegroundColor Yellow
$firewallStatus | Format-Table -Property Profile, Enabled -AutoSize
Expand Down
82 changes: 42 additions & 40 deletions Scripts/ip-change.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,52 @@ function ConvertTo-PrefixLength {
return $prefixLength
}

function ipchangefun{
# Display network adapters to the user
Write-Host "Available Network Adapters and their Interface Indexes:"
Get-NetAdapter | Format-Table Name, InterfaceIndex, Status

# Prompt for Interface Index and validate
$interfaceIndex = $null
do {
$interfaceIndex = Read-Host "Enter the Interface Index from the list above"
if (-not $interfaceIndex -or $interfaceIndex -eq '') {
Write-Host "Interface Index cannot be empty, please enter a valid number."
function ipchangefun {
# Display network adapters to the user
Write-Host "Available Network Adapters and their Interface Indexes:"
Get-NetAdapter | Format-Table Name, InterfaceIndex, Status

# Prompt for Interface Index and validate
$interfaceIndex = $null
do {
$interfaceIndex = Read-Host "Enter the Interface Index from the list above"
if (-not $interfaceIndex -or $interfaceIndex -eq '') {
Write-Host "Interface Index cannot be empty, please enter a valid number."
}
if (-not (Get-NetAdapter | Where-Object InterfaceIndex -eq $interfaceIndex)) {
Write-Host "Invalid Interface Index entered. Please enter a number from the list above."
$interfaceIndex = $null
}
} while (-not $interfaceIndex)

$ipAddress = Read-Host "Enter the new IP Address"
$subnetMask = Read-Host "Enter the Subnet Mask"

# Check if subnet mask is empty and set default value
if ([string]::IsNullOrWhiteSpace($subnetMask)) {
$subnetMask = "255.255.255.0"
}
if (-not (Get-NetAdapter | Where-Object InterfaceIndex -eq $interfaceIndex)) {
Write-Host "Invalid Interface Index entered. Please enter a number from the list above."
$interfaceIndex = $null
}
} while (-not $interfaceIndex)

$ipAddress = Read-Host "Enter the new IP Address"
$subnetMask = Read-Host "Enter the Subnet Mask"

# Check if subnet mask is empty and set default value
if ([string]::IsNullOrWhiteSpace($subnetMask)) {
$subnetMask = "255.255.255.0"
}
# Convert the subnet mask to prefix length
$prefixLength = ConvertTo-PrefixLength -SubnetMask $subnetMask

# Convert the subnet mask to prefix length
$prefixLength = ConvertTo-PrefixLength -SubnetMask $subnetMask

# Remove the existing IP address
try {
Get-NetIPAddress -InterfaceIndex $interfaceIndex | Remove-NetIPAddress -Confirm:$false
Write-Host "Existing IP Address(es) removed."
} catch {
Write-Error "Failed to remove existing IP Address. Error: $_"
}
# Remove the existing IP address
try {
Get-NetIPAddress -InterfaceIndex $interfaceIndex | Remove-NetIPAddress -Confirm:$false
Write-Host "Existing IP Address(es) removed."
}
catch {
Write-Error "Failed to remove existing IP Address. Error: $_"
}

# Set the IP Address
try {
New-NetIPAddress -InterfaceIndex $interfaceIndex -IPAddress $ipAddress -PrefixLength $prefixLength
Write-Host "IP Address has been changed successfully."
} catch {
Write-Error "Failed to change IP Address. Error: $_"
}
# Set the IP Address
try {
New-NetIPAddress -InterfaceIndex $interfaceIndex -IPAddress $ipAddress -PrefixLength $prefixLength
Write-Host "IP Address has been changed successfully."
}
catch {
Write-Error "Failed to change IP Address. Error: $_"
}
}

ipchangefun
133 changes: 68 additions & 65 deletions Scripts/list-weather.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,71 @@

function GetDescription([string]$text) {
switch ($text) {
"Blizzard" { return "❄️ blizzard ⚠️" }
"Blowing snow" { return "❄️ blowing snow ⚠️" }
"Clear" { return "🌙 clear" }
"Cloudy" { return "☁️ cloudy" }
"Fog" { return "🌫 fog" }
"Freezing fog" { return "🌫 freezing fog" }
"Heavy rain" { return "💧 heavy rain ⚠️" }
"Heavy snow" { return "❄️ heavy snow ⚠️" }
"Light drizzle" { return "💧 light drizzle" }
"Light freezing rain" { return "💧 light freezing rain ⚠️" }
"Light rain" { return "💧 light rain" }
"Light rain shower" { return "💧 light rain shower" }
"Light sleet" { return "❄️ light sleet" }
"Light sleet showers" { return "❄️ light sleet showers" }
"Light snow" { return "❄️ light snow" }
"Light snow showers" { return "❄️ light snow showers" }
"Moderate or heavy freezing rain"{return "💧 moderate or heavy freezing rain ⚠️" }
"Moderate or heavy sleet" { return "❄️ moderate or heavy sleet ⚠️" }
"Moderate or heavy rain shower" { return "💧 moderate or heavy rain shower ⚠️" }
"Moderate or heavy rain in area with thunder" { return "💧 moderate or heavy rain in area with thunder ⚠️" }
"Moderate or heavy snow showers"{ return "❄️ moderate or heavy snow showers ⚠️" }
"Moderate or heavy snow in area with thunder" { return "❄️ moderate or heavy snow in area with thunder ⚠️" }
"Moderate rain" { return "💧 moderate rain" }
"Moderate rain at times" { return "💧 moderate rain at times" }
"Moderate snow" { return "❄️ moderate snow" }
"Mist" { return "🌫 misty" }
"Overcast" { return "☁️ overcast" }
"Partly cloudy" { return "⛅️partly cloudy" }
"Patchy heavy snow" { return "❄️ patchy heavy snow ⚠️" }
"Patchy light drizzle" { return "💧 patchy light drizzle" }
"Patchy light rain" { return "💧 patchy light rain" }
"Patchy light rain in area with thunder" { return "💧 patchy light rain in area with thunder" }
"Patchy light rain with thunder" { return "💧 patchy light rain with thunder" }
"Patchy light snow" { return "❄️ patchy light snow" }
"Patchy moderate snow" { return "❄️ patchy moderate snow" }
"Patchy rain possible" { return "💧 patchy rain possible" }
"Patchy rain nearby" { return "💧 patchy rain nearby" }
"Patchy sleet nearby" { return "❄️ patchy sleet nearby" }
"Patchy snow possible" { return "❄️ patchy snow possible" }
"Sunny" { return "☀️ sunny" }
"Thundery outbreaks possible" { return "⚡️thundery outbreaks possible" }
"Thundery outbreaks in nearby" { return "⚡️thundery outbreaks in nearby" }
default { return $text }
"Blizzard" { return "❄️ blizzard ⚠️" }
"Blowing snow" { return "❄️ blowing snow ⚠️" }
"Clear" { return "🌙 clear" }
"Cloudy" { return "☁️ cloudy" }
"Fog" { return "🌫 fog" }
"Freezing fog" { return "🌫 freezing fog" }
"Heavy rain" { return "💧 heavy rain ⚠️" }
"Heavy snow" { return "❄️ heavy snow ⚠️" }
"Light drizzle" { return "💧 light drizzle" }
"Light freezing rain" { return "💧 light freezing rain ⚠️" }
"Light rain" { return "💧 light rain" }
"Light rain shower" { return "💧 light rain shower" }
"Light sleet" { return "❄️ light sleet" }
"Light sleet showers" { return "❄️ light sleet showers" }
"Light snow" { return "❄️ light snow" }
"Light snow showers" { return "❄️ light snow showers" }
"Moderate or heavy freezing rain" { return "💧 moderate or heavy freezing rain ⚠️" }
"Moderate or heavy sleet" { return "❄️ moderate or heavy sleet ⚠️" }
"Moderate or heavy rain shower" { return "💧 moderate or heavy rain shower ⚠️" }
"Moderate or heavy rain in area with thunder" { return "💧 moderate or heavy rain in area with thunder ⚠️" }
"Moderate or heavy snow showers" { return "❄️ moderate or heavy snow showers ⚠️" }
"Moderate or heavy snow in area with thunder" { return "❄️ moderate or heavy snow in area with thunder ⚠️" }
"Moderate rain" { return "💧 moderate rain" }
"Moderate rain at times" { return "💧 moderate rain at times" }
"Moderate snow" { return "❄️ moderate snow" }
"Mist" { return "🌫 misty" }
"Overcast" { return "☁️ overcast" }
"Partly cloudy" { return "⛅️partly cloudy" }
"Patchy heavy snow" { return "❄️ patchy heavy snow ⚠️" }
"Patchy light drizzle" { return "💧 patchy light drizzle" }
"Patchy light rain" { return "💧 patchy light rain" }
"Patchy light rain in area with thunder" { return "💧 patchy light rain in area with thunder" }
"Patchy light rain with thunder" { return "💧 patchy light rain with thunder" }
"Patchy light snow" { return "❄️ patchy light snow" }
"Patchy moderate snow" { return "❄️ patchy moderate snow" }
"Patchy rain possible" { return "💧 patchy rain possible" }
"Patchy rain nearby" { return "💧 patchy rain nearby" }
"Patchy sleet nearby" { return "❄️ patchy sleet nearby" }
"Patchy snow possible" { return "❄️ patchy snow possible" }
"Sunny" { return "☀️ sunny" }
"Thundery outbreaks possible" { return "⚡️thundery outbreaks possible" }
"Thundery outbreaks in nearby" { return "⚡️thundery outbreaks in nearby" }
default { return $text }
}
}

function GetWindDir([string]$text) {
switch($text) {
"NW" { return "" }
"NNW" { return "" }
"N" { return "" }
"NNE" { return "" }
"NE" { return "" }
"ENE" { return "" }
"E" { return "" }
"ESE" { return "" }
"SE" { return "" }
"SSE" { return "" }
"S" { return "" }
"SSW" { return "" }
"SW" { return "" }
"WSW" { return "" }
"W" { return "" }
"WNW" { return "" }
default { return "$text" }
switch ($text) {
"NW" { return "" }
"NNW" { return "" }
"N" { return "" }
"NNE" { return "" }
"NE" { return "" }
"ENE" { return "" }
"E" { return "" }
"ESE" { return "" }
"SE" { return "" }
"SSE" { return "" }
"S" { return "" }
"SSW" { return "" }
"SW" { return "" }
"WSW" { return "" }
"W" { return "" }
"WNW" { return "" }
default { return "$text" }
}
}

Expand All @@ -79,7 +79,7 @@ try {
Write-Progress -completed "Done."

[int]$day = 0
foreach($hourly in $weather.weather.hourly) {
foreach ($hourly in $weather.weather.hourly) {
$hour = $hourly.time / 100
$tempC = $(($hourly.tempC.toString()).PadLeft(3))
$precip = $($($hourly.precipMM).PadLeft(4))
Expand All @@ -94,11 +94,13 @@ try {
if ($hour -eq 0) {
if ($day -eq 0) {
Write-Host "TODAY 🌡°C ☂️mm 💧 💨km/h ☀️UV ☁️ 👁km at $area ($region, $country)" -foregroundColor green
} elseif ($day -eq 1) {
}
elseif ($day -eq 1) {
$date = (Get-Date).AddDays(1)
[string]$dayOfWeek = $date.DayOfWeek
Write-Host "$($dayOfWeek.toUpper())" -foregroundColor green
} else {
}
else {
$date = (Get-Date).AddDays(2)
[string]$dayOfWeek = $date.DayOfWeek
Write-Host "$($dayOfWeek.toUpper())" -foregroundColor green
Expand All @@ -107,6 +109,7 @@ try {
}
"$(($hour.toString()).PadLeft(2))h $tempC° $precip $humidity% $($windDir)$windSpeed $UV $clouds% $visib $desc"
}
} catch {
}
catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
}
5 changes: 3 additions & 2 deletions Scripts/locate-ipaddress.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
param([string]$IPaddress= "")
param([string]$IPaddress = "")

try {
if ($IPaddress -eq "" ) { $IPaddress = read-host "Enter IP address to locate" }

$result = Invoke-RestMethod -Method Get -Uri "http://ip-api.com/json/$IPaddress"
write-output $result
exit 0 # success
} catch {
}
catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
12 changes: 6 additions & 6 deletions Scripts/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ clean_empty_files() {
touch ~/.hushlogin

# Update package list silently
sudo apt update > /dev/null 2>&1
sudo apt update >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\033[1;31mFailed to update package list. Please check your internet connection or package manager configuration.\033[0m"
exit 1
fi

# Clean up storage by removing unused packages
sudo apt autoremove -y > /dev/null 2>&1
sudo apt autoremove -y >/dev/null 2>&1

# Clean up cache memory
sudo apt clean > /dev/null 2>&1
sudo apt clean >/dev/null 2>&1

# Remove unwanted files (e.g., temporary files)
sudo rm -rf /tmp/* /var/tmp/* > /dev/null 2>&1
sudo rm -rf /tmp/* /var/tmp/* >/dev/null 2>&1

# Remove empty files and directories from the entire WSL folder, excluding problematic areas
clean_empty_files "/home"
Expand All @@ -36,7 +36,7 @@ UPDATES=$(apt list --upgradable 2>/dev/null | grep -v "Listing..." | wc -l)

if [ "$UPDATES" -gt 0 ]; then
# Upgrade installed packages silently
sudo apt upgrade -y > /dev/null 2>&1
sudo apt upgrade -y >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\033[1;31mFailed to upgrade packages. Please check for errors.\033[0m"
exit 1
Expand All @@ -48,7 +48,7 @@ else
fi

# Install desired packages silently
sudo apt install -y nala btop screen > /dev/null 2>&1
sudo apt install -y nala btop screen >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e "\033[1;31mFailed to install packages. Please check for errors.\033[0m"
exit 1
Expand Down
Loading

0 comments on commit 9965864

Please sign in to comment.