Skip to content

Instantly share code, notes, and snippets.

@rad1ke
Last active September 2, 2023 09:33
Show Gist options
  • Save rad1ke/8951cd741394b18870efb5c93e1e9ebc to your computer and use it in GitHub Desktop.
Save rad1ke/8951cd741394b18870efb5c93e1e9ebc to your computer and use it in GitHub Desktop.
# Various PowerShell Snippets
$ProgressPreference = "SilentlyContinue"
Add-Type "using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy {public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) {return true;}}";[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[Net.ServicePointManager]::SecurityProtocol = "tls13, tls12"
$Bullet = $([char]0x00B7)
$Hostname = [System.Net.Dns]::GetHostEntry([string]$env:computername).HostName
$User = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$OperatingSystem = Get-WmiObject Win32_OperatingSystem
$Processor = Get-WmiObject Win32_Processor
$VideoController = Get-WmiObject Win32_VideoController
$PhysicalMemory = Get-WmiObject Win32_PhysicalMemory
$NetworkAdapterConfiguration = Get-WmiObject Win32_NetworkAdapterConfiguration
$Uptime = (Get-Date) - ($OperatingSystem.ConvertToDateTime($OperatingSystem.LastBootUpTime))
$OS = $OperatingSystem.Caption
try {
$TPM = Get-Tpm -ErrorAction SilentlyContinue
if($TPM.TpmPresent -eq $true) {
if($TPM.TpmReady -eq $true) {
$TPMStatus = "Enabled"
}
if($TPM.TpmReady -eq $false) {
$TPMStatus = "Disabled"
}
$TPMVersion = ", " + (Get-WmiObject -Namespace root\cimv2\security\microsofttpm -Class Win32_TPM).SpecVersion.Split(',')[0]
}
if($TPM.TpmPresent -eq $false) {
$TPMStatus = "Not available"
}
if($TPM -eq "Administrator privilege is required to execute this command.") {
$TPMStatus = "Insufficient permissions"
}
}
catch {
$TPMStatus = "Not available"
}
$SecureBoot = try{
Confirm-SecureBootUEFI -ErrorAction SilentlyContinue
}
catch {
$SecureBootStatus = "Disabled"
}
if($SecureBoot -eq $true) {
$SecureBootStatus = "Enabled"
}
if($env:firmware_type -eq "Legacy") {
$SecureBootStatus = "Not available"
}
$CPU = ($Processor | Select-Object -Property Name -First 1).Name -replace "\(R\)" -replace "\(TM\)" -replace " CPU" -replace "( @).*"
$Cores = ($Processor | Measure-Object -Property NumberOfCores -Sum).Sum
$GPURAM = [math]::Round(($VideoController).AdapterRAM / 1GB)
$GPUHeader = $(($VideoController).VideoProcessor -replace "\(R\)") + $(if($GPURAM){" (" + $GPURAM }) + $(if($GPURAM){" GB)"})
$RAM = " " + ($PhysicalMemory | ForEach-Object { "`n $Bullet " + $_.DeviceLocator + ": " + $_.Capacity / 1GB + " GB" + $(if($_.Speed -ne $null){ " (" + $_.Speed + " MHz)" })})
$RAMTotal = ($PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum
$RAMUsed = $RAMTotal - ((($OperatingSystem | Measure-Object -Property FreePhysicalMemory -Sum).sum * 1KB))
$RAMUsage = [math]::Round($RAMUsed / $RAMTotal * 100)
$StorageVolumes = Get-Volume | Where-Object { $_.DriveType -eq "Fixed" }
$Storage = $StorageVolumes | ForEach-Object { "`n $Bullet " + $(if($_.FileSystemLabel -eq "System Reserved"){"[System Reserved]: "}elseif($_.driveLetter -eq $null){"[Not assigned]: "}else{$_.DriveLetter + ": "}) + [math]::Round(($_.Size - $_.SizeRemaining)/$_.Size * 100) + "% used (" + [math]::Round($_.Size / 1GB - $_.SizeRemaining / 1GB) + "/" + [math]::Round($_.Size / 1GB) + " GB)" }
$StorageTotal = [math]::Round(($StorageVolumes | Measure-Object -Property Size -Sum).Sum / 1GB)
$StorageHeader = "$StorageTotal GB in total"
$IPv4PingTime = [math]::Round((Measure-Command { $IPv4PingStatus = (New-Object System.Net.Sockets.TcpClient).ConnectAsync("1.1.1.1", "80").Wait("1000") }).TotalMilliseconds)
$DNS = Resolve-DnsName -Name cloudflare.com -DnsOnly -QuickTimeout -ErrorAction Ignore
$NetworkAdapters = $NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null }
$NetworkingHeader = $(if($IPv4PingStatus -eq $true){"IPv4: " + $IPv4PingTime + "ms"}else{"IPv4: Unreachable"}) + $(if($DNS){", DNS: Online"}else{", DNS: Unreachable"})
$Networking = $NetworkAdapters | ForEach-Object { " $Bullet " + ($_.Description -replace "\(R\)" -replace " Gigabit Network Connection" -replace " Ethernet Connection" -replace " Ethernet Adapter") + "`n Mode: " + $(if($_.DHCPEnabled -eq $true){"DHCP"}else{"Static"}) + "`n IP: " + (($_.IPAddress | Where-Object {$_ -notlike "fe80*"} ) -join ", ") + "`n Subnet: " + ($_.IPSubnet -join ", ") + "`n Gateway: " + $_.DefaultIPGateway + "`n DNS: " + ($_.DNSServerSearchOrder -join ", ") + "`n MAC: " + $_.MACAddress }
$Message = "# General"
$Message += "`n Hostname: " + $Hostname
$Message += "`n User: " + $User
$Message += "`n Uptime: " + $Uptime.Days + " days, " + $Uptime.ToString("hh\:mm\:ss")
$Message += "`n OS: " + $OS
$Message += "`n`n# Security features"
$Message += "`n Firmware type: " + $env:firmware_type
$Message += "`n Secure Boot: " + $SecureBootStatus
$Message += "`n TPM: " + $TPMStatus + $TPMVersion
$Message += "`n`n# Hardware"
$Message += "`n CPU: " + $CPU + " (" + $Cores + " Cores)"
$Message += "`n GPU: " + $GPUHeader
$Message += "`n`n RAM: " + $RAMUsage + "% used " + "(" + [math]::Round($RAMUsed / 1GB,1) + "/" + [math]::Round($RamTotal / 1GB) + " GB)" + $RAM
$Message += "`n`n Storage: " + $StorageHeader + $Storage
$Message += "`n`n Networking: " + $NetworkingHeader + "`n" + $Networking
$Message
if (!(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process -FilePath 'powershell' -ArgumentList ('-File', $MyInvocation.MyCommand.Source, $args | %{ $_ }) -Verb RunAs; exit
}
# Run dsa.msc (RSAT/Active Directory Users and Computers) as a different username and domain
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -Command `"runas /netonly /noprofile /u:USERNAME@DOMAIN 'mmc.exe dsa.msc /server=DC-HOSTNAME'`"" -Verb RunAs; exit }
# GET-Request
Invoke-RestMethod -Uri "" -Method GET -ContentType "application/json" -Headers @{ 'API-Key' = '' }
# POST-Request
Invoke-RestMethod -Uri "" -Method POST -ContentType "application/json" -Headers @{ 'API-Key' = '' } -Body "{}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment