Skip to content

Instantly share code, notes, and snippets.

View nanoDBA's full-sized avatar

nanoDBA nanoDBA

View GitHub Profile
@nanoDBA
nanoDBA / Lock_and_power_off_display.ps1
Created June 11, 2024 14:07
Turn off the display and lock the workstation using the Windows API via PowerShell
# filename: Lock_and_power_off_display.ps1
#
# This script turns off the display and locks the workstation using the Windows API.
#
# Warning: This script will turn off the display and lock the workstation indefinitely until stopped manually.
#
# Import the necessary libraries for interacting with the Windows API
Add-Type @"
using System;
using System.Runtime.InteropServices;
@nanoDBA
nanoDBA / Get-EC2InstanceVolumes.ps1
Last active August 7, 2025 17:29
This function retrieves the volumes attached to the specified EC2 instances in the specified region. It returns a list of volume details including the instance ID, instance name, instance type, volume ID, device name, state, availability zone, volume type, IOPS, throughput, and size.
<#
.SYNOPSIS
Get the volumes attached to the EC2 instances.
.DESCRIPTION
This function retrieves the volumes attached to the specified EC2 instances in the
specified region. It returns a list of volume details including the instance ID,
instance name, instance type, volume ID, device name, state, availability zone,
volume type, IOPS, throughput, and size.
@nanoDBA
nanoDBA / RemoveCommentsFromFiles.ps1
Created August 13, 2024 03:28
This script accesses files stored in a directory, reading the first 1024 bytes of each file. It includes a function that carefully removes comments marked by /* and */ from the content. After cleaning up the content, it converts it back into bytes and saves the changes to the original files.
$files = Get-ChildItem -Path "A:\Phoenix"
# Define the comment start and end markers
$commentStart = '/*'
$commentEnd = '*/'
foreach ($file in $files) {
# Open the file and read the first 1024 bytes
$maxBytes = 1024
$buffer = New-Object byte[] $maxBytes
@nanoDBA
nanoDBA / Export-SQLTablesToFiles.ps1
Created August 13, 2024 03:29
Loops through an array of tables, and exports each one to its own neatly named SQL file
<# Table Export #>
$someTables = @"
TBL_A_FOO
TBL_B_BAR
TBL_C_BAZ
TBL_D_QUX
TBL_E_QUUX
TBL_F_CORGE
"@.split("`n").TrimEnd("`r")
@nanoDBA
nanoDBA / database_role_securables_query.sql
Created August 22, 2024 10:37
Retrieve securables and permissions for any specified database role. Uses a variable for the role name and prompts the user if the role name is not provided. Modeled after SSMS Securables page of Database Roles properties
-- database_role_securables_query.sql
-- modeled after SSMS Securables page of Database Roles properties
DECLARE @RoleName NVARCHAR(128);
SET @RoleName = ''; -- Replace with role name or leave empty for testing
IF @RoleName = ''
BEGIN
SELECT 'Please provide a valid role name to query securables' AS Message;
@nanoDBA
nanoDBA / Start-Sleep Magenta.ps1
Created September 6, 2024 20:16
Sleep 5 hours in console/script
# Store TimeSpan object in variable $timespan
$timespan = New-TimeSpan -Start (Get-Date) -End (Get-Date).AddHours(5) #(Get-Date).AddDays(1).Date.AddHours(5) #careful with this - syntax is weird
# Print a message to the console in magenta color indicating the time the script will resume execution
Write-Host -ForegroundColor magenta "Sleeping until $((Get-date).AddSeconds($timespan.TotalSeconds)) ..."
# Pause the execution of the script for a number of seconds equal to the total seconds of the $timespan
Start-Sleep -Seconds $timespan.TotalSeconds
@nanoDBA
nanoDBA / Reboot.ps1
Created October 4, 2024 19:24
Reboots machine with a warning message to logged on users after specified number of minutes - defaults to 15 minute delay Reboot.ps1 -DelayMinutes 20
#requires -version 5.0
#Requires -RunAsAdministrator
<# DANGER REBOOTING!
This is meant for a local machine in a scheduled task
and is not handling remoting or remote credentials
#>
[CmdletBinding()]
param (
[Parameter()][int]$DelayMinutes = 15 #default to 15 minute delay if this parameter is not supplied
)
@nanoDBA
nanoDBA / Get-ExplicitLogonEvents.ps1
Created October 4, 2024 20:03
Gets 4648 Explicit Logon Events from Windows Event Log Author: Lee Christensen (@tifkin_)
function Get-ExplicitLogonEvents {
<#
.SYNOPSIS
Gets 4648 Explicit Logon Events from Windows Event Log
Author: Lee Christensen (@tifkin_)
# https://github.com/threatexpress/red-team-scripts/blob/3121db5d53a25d66afa01afb3bf0487d919d1846/HostEnum.ps1#L1552
#>
@nanoDBA
nanoDBA / ServiceBrokerLogNoiseReductionEndpoint.sql
Last active May 22, 2025 18:22
Creates SQL Server Service Broker endpoint on port 4022 to reduce noise in the SQL Server error log (ERRORLOG). Includes port availability checks, conditional creation, logging, and optional code to stop and drop endpoint if needed.
/*
| File: ServiceBrokerLogNoiseReductionEndpoint.sql
| Description: 🚦 Creates a Service Broker endpoint to reduce SQL Server log noise.
| Purpose: Sets up a dedicated endpoint for Service Broker on a user-chosen
| port (default 4022), with checks for port conflicts and robust
| logging. Useful for DBAs who want to keep error logs clean and
| avoid noisy Service Broker errors. Includes optional code to drop
| the endpoint. For test/dev use or advanced troubleshooting. 😎
| Created: 2024-05-21
| Modified: 2025-05-22
@nanoDBA
nanoDBA / Get-RecommendedSqlCU.ps1
Last active November 21, 2024 13:57
Finds the recommended N-1 cumulative update for SQL Server based on patching strategy that avoids intermediate releases
<#
.SYNOPSIS
Finds the recommended N-1 cumulative update for SQL Server based on patching strategy that avoids intermediate releases
.DESCRIPTION
This script queries a Google Sheets document containing SQL Server cumulative update information
and determines the recommended N-1 cumulative update based on a patching strategy that avoids
intermediate releases (e.g., hotfixes, security updates) between cumulative updates.
The script outputs the recommended cumulative update, its release date, and a link to more details.
The patching strategy is defined as follows: