Skip to content

Instantly share code, notes, and snippets.

@robderickson
robderickson / Get-RTADObject.ps1
Last active June 20, 2023 16:16
Use LDAP filter syntax to get AD objects with the ActiveDirectory module installed.
function Get-RTADObject {
[CmdletBinding()]
param(
[string]$LdapFilter,
[string]$ComputerName,
[PSCredential]$Credential
)
process {
if ($ComputerName) {
@robderickson
robderickson / Add-RetentionTagToFolder.ps1
Last active September 1, 2023 21:44
Apply an Exchange retention policy tag to a mailbox folder.
# During a migration from Exchange 2010 to Exchange 2016, I needed to transition from Managed Folder Mailbox Policies
# to Retention Policies. In an effort to avoid disrupting existing user work flows, I needed to apply a tag to the
# Managed Folders folder separate from the DPT. I adapted this script from a blog post by Akash Bhargava (thanks!):
# https://docs.microsoft.com/en-us/archive/blogs/akashb/stamping-retention-policy-tag-using-ews-managed-api-1-1-from-powershellexchange-2010
# I'm not sure how well this will work for nested folders.
function Add-RetentionTagToFolder {
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='High')]
param(
[Parameter(
Mandatory=$true,
@robderickson
robderickson / Get-IisLogFieldConfig.ps1
Created January 25, 2021 22:00
For when you get tired of "send me a screenshot of the selected logging fields in IIS."
function Get-IisLogFieldConfig {
[CmdletBinding()]
param(
[string[]$ComputerName,
)
PROCESS {
foreach ($computer in $ComputerName) {
Invoke-Command -ComputerName $computer -ScriptBlock {
[System.Reflection.Assembly]::LoadFrom( "C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll" ) | Out-Null
@robderickson
robderickson / GetMoveParameters.ps1
Created July 11, 2020 04:39
TL;DR: Over-complicate Exchange database selection during mailbox provisioning. For each mailbox in a collection of Exchanges mailboxes, return PrimarySmtpAddress and the smallest database (factoring in AvailableNewMailboxSpace). As databases are selected, add the mailbox's size to the database size so the next database selected factors in the s…
function GetMoveParameters {
[CmdletBinding()]
param(
[string[]]$Mailbox,
[string[]]$Databases
)
PROCESS {
if ($Databases) {
$dbs = $Databases | Where-Object {$_.Mounted -eq $true}
# Convert UserAccountControl attribute values to $true (enabled) or $false (disable). See https://support.microsoft.com/en-us/help/305144/how-to-use-useraccountcontrol-to-manipulate-user-account-properties
function ConvertTo-ADUserEnabled {
[CmdletBinding()]
param(
[int]$UserAccountControl
)
PROCESS {
switch ($UserAccountControl) {
512 {$true}
@robderickson
robderickson / Get-MailboxItemTotal.ps1
Last active May 7, 2020 12:11
Get sum of items, deleted items, total item sizes, and deleted item sizes for all specified mailboxes
function Get-MailboxItemTotal {
[CmdletBinding()]
param(
$Identity
)
PROCESS {
if ($Identity) {
Write-Verbose 'Getting specified mailboxes.'
$Identities = $Identity | Foreach-Object {Get-Mailbox -Identity $_} | Select-Object -ExpandProperty Identity
@robderickson
robderickson / move-videos.ps1
Last active March 22, 2020 23:10
Example of how to sort files into folders based on file name.
# Assume file names are like ThatShow - S01E03
# Assume they will go in their respective subfoders in a directory called Media (M:\Media)
$ParentPath = 'M:\Media'
# Get collection of .mp4 files from working directory
$files = Get-ChildItem *.mp4
foreach ($file in $files) {
# Parse the title from the file name
$ShowTitle = $file.Name.Split('-')[0].TrimEnd()
# Parse the season from the file name
$Season = "Season " + [decimal]$file.Name.Split('-')[1].TrimStart().Substring(1,2)
@robderickson
robderickson / Search-ExchangeLogAndMailbox.ps1
Created January 14, 2020 21:11
Combine Get-MessageTrackingLog with Search-Mailbox for finding and removing malicious mail items.
function Search-ExchangeLogAndMailbox {
# TODO: Write help
[CmdletBinding(DefaultParameterSetName='Trace')]
param(
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]$Sender,
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]$MessageSubject,
@robderickson
robderickson / inventory.yml
Created December 15, 2019 02:04
Ansible inventory with vars
all:
children:
web:
hosts:
192.168.56.3:
vars:
# These credentials are being "rejected by the server"; Can do interactive login with same credentials
ansible_user: vagrant
ansible_password: vagrant
ansible_port: 5985
@robderickson
robderickson / RunAsExplorer.md
Created October 8, 2019 19:20
How to enable 'Run as a different user' for Explorer.exe
  1. Run regedit.exe elevated.
  2. Take ownership of HKEY_CLASSES_ROOT\AppID{CDCBCFCA-3CDC-436f-A4E2-0E02075250C2}.
  3. Rename the RunAs value to _RunAs.
  4. Create a new shortcut on your Desktop for C:\Windows\System32\runas.exe.
  5. Name it something like Admin-Explorer.
  6. Right-click the new shortcut and click Properties.
  7. Change the target to: C:\Windows\System32\runas.exe /noprofile /user:<domain>\<username> "c:\windows\explorer.exe /separate"
  8. (Optional) Change the icon.
  9. (Optional) Pin to taskbar.