Skip to content

Instantly share code, notes, and snippets.

View dstreefkerk's full-sized avatar

Daniel dstreefkerk

View GitHub Profile
@dstreefkerk
dstreefkerk / Copy-Shrug.ps1
Created February 14, 2020 00:49
Put this into your PowerShell profile file for an offline version of www.copyshrug.com.
function Copy-Shrug {
"¯\_(ツ)_/¯" | Set-Clipboard
Write-Output "Shrug copied to clipboard"
}
New-Alias -name 'cps' -Value Copy-Shrug
@dstreefkerk
dstreefkerk / Get-MachineAccountQuotaUsers.ps1
Created January 29, 2020 04:38
Gets a list of AD computers that were created by regular users exercising their default right to create up to 10 computer accounts in an AD domain
$machineAccountQuotaComputers = Get-ADComputer -filter {ms-DS-CreatorSID -ne "$null"} -Properties ms-DS-CreatorSID,Created
foreach ($machine in $machineAccountQuotaComputers) {
$creator = $null
try {
$creator = [System.Security.Principal.SecurityIdentifier]::new($machine.'ms-DS-CreatorSID').Translate([System.Security.Principal.NTAccount]).Value
}
catch {
$creator = $machine.'ms-DS-CreatorSID'
}
@dstreefkerk
dstreefkerk / CSVGridView.bat
Created November 8, 2019 06:19
Batch file that enables a CSV to be dragged/dropped and then opened in a PowerShell GridView. Requires the PowerShell ISE to be instaled.
@echo off
IF "%~1"=="" GOTO NOFILE
set CSVPATH=%~1
ECHO Loading CSV %CSVPATH%
powershell.exe -NoProfile -NoExit -NoLogo -Command "if ((Test-Path $env:CSVPATH -PathType Leaf) -and ($env:CSVPATH -like '*.csv')) {Import-Csv -Path $env:CSVPATH | Out-GridView -Wait -Title $env:CSVPATH};exit"
GOTO END
:NOFILE
@dstreefkerk
dstreefkerk / dfstargets.ps1
Last active March 19, 2021 01:55
Get a list of active DFS folder targets under a specific DFS root
Get-DfsnFolder -Path \\internal.contoso.com\dfsroot\* | Get-DfsnFolderTarget | ? {$_.State -eq "Online"} | Group-Object -Property Path | ForEach-Object {$_.group[0]}
@dstreefkerk
dstreefkerk / Get-AussieGovDomains.ps1
Created July 9, 2019 23:11
Retrieve a list of Australian government (.gov.au) domains from the CKAN Data API at https://data.gov.au/
<#
.DESCRIPTION
Retrieve a list of Australian government (.gov.au) domains from the CKAN Data API at https://data.gov.au/
#>
# https://data.gov.au/dataset/ds-dga-4d5301b2-bc64-4774-b437-56a408836e57/details
$dataUri = 'https://data.gov.au/data/api/3/action/datastore_search?resource_id=507f8129-b84c-4215-ae7d-5aca364e4a0e&limit=2000'
# Basic function to strip the URL down to the bare FQDN
# Retrieve SPF records for a domain via Cloudflare DoH
$domain = 'example.com'
$result = Invoke-RestMethod -Uri "https://cloudflare-dns.com/dns-query?name=$domain&type=TXT" -Headers @{'accept'='application/dns-json'}
if ($result -ne $null) {
if ($result.answer -ne $null) {
$result.answer | Select-Object -ExpandProperty data | Where-Object {$_ -like '*v=spf1*'}
}
}
@dstreefkerk
dstreefkerk / Invoke-SpeechPrank.ps1
Last active March 19, 2021 01:56
Some PowerShell pranking fun. Combine with PSRemoting to confuse your co-workers. I've not used this since 2014, so I don't know if it still works.
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
// f(), g(), ... are unused COM method slots. Define these if you care
int f(); int g(); int h(); int i();
int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
int j();
int GetMasterVolumeLevelScalar(out float pfLevel);
@dstreefkerk
dstreefkerk / Invoke-RegexReplaceTest.ps1
Created April 27, 2019 02:45
Some simple character replacement via Regex in PowerShell
# Regex Examples with -Replace
$testString = "ABCabc 123456_!#$%"
Write-Host "Remove all numbers in a string" -ForegroundColor Yellow
"Before: $testString"
"After: $($testString -replace '\d')"
""
Write-Host "Remove everything but numbers from a string" -ForegroundColor Yellow
"Before: $testString"
@dstreefkerk
dstreefkerk / New-ContosoUser.ps1
Created April 27, 2019 02:37
Generate a new AD User based upon a few specific requirements
# Requirements
#
# 1. Inputs - First Name, Last Name
#
# 2. SamAccountName and CN must be in firstname.lastname format
# 3. UPN must be in firstname.lastname@contoso.com format
# 4. If a user already exists with the same UPN or SamAccountName, add a number to the end or increment the existing number
function New-ContosoUser ([string]$FirstName,[string]$LastName) {
$maxUsersPerName = 100
@dstreefkerk
dstreefkerk / Get-GpoPerYear.ps1
Created April 1, 2019 00:57
Get a count of GPOs created per year
Import-Module GroupPolicy
$gpos = Get-GPO -All | Select-Object DisplayName,Description,CreationTime,DomainName
$gpos | Select-Object *,@{n='Year';e={$_.CreationTime | Get-Date -Format 'yyyy'}} | Group-Object -Property Year | Select-Object -Property @{n='Year';e={$_.Name}},@{n='Quantity';e={$_.Count}} | Format-Table -AutoSize