Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / RDPUser.psm1
Last active September 13, 2016 15:26
A slightly dirty Powershell module which includes a function for converting the results of the command-line Query User in to a Powershell object and a function for logging off disconnected users (with -whatif and -confirm).
# Example usage:
# Get-RDPUser | Disconnect-RDPUser -WhatIf
Function Get-RDPUser {
[CmdletBinding()]
Param()
query user | Select -Skip 1 | ForEach-Object {
$_ = $_ -split "\s\s+"
@markwragg
markwragg / PRTGPausedDevices.psm1
Last active September 12, 2016 10:25
Powershell module for managing retrieving and removing paused devices from PRTG : http://wragg.io/clean-up-paused-devices-from-prtg-with-powershell/
Function Get-PRTGPausedDevice{
[CmdletBinding()]
Param(
$PRTGURL = "prtg.yourcompany.com",
$Username = "youruser",
$Passhash = "yourpasshash",
$Name,
$Message,
[int]$DaysPaused #Use -1 to filter for where date is unknown and 0 to return all
)
@markwragg
markwragg / Test-TestNetConnection.ps1
Last active April 12, 2019 12:06
Powershell snippets to test functionality of the test-NetConnection cmdlet.
#Performs a simple Ping test to the default destination of internetbeacon.msedge.net
Test-NetConnection
#Performs a Ping test to google.com
$Ping = Test-NetConnection -ComputerName google.com | Select *
#Performs a Traceroute test to google.com
$TraceRt = Test-NetConnection -ComputerName google.com -TraceRoute
$TraceRt | Select *
@markwragg
markwragg / Remove-PRTGPausedDevices.ps1
Last active September 12, 2016 10:25
Powershell script to delete multiple devices from PRTG where a name and/or message text is partially matched and where the device is paused and has been paused for longer than x days. The script uses -whatif and -confirm for use with the deletion step.
[CmdletBinding(SupportsShouldProcess = $true,ConfirmImpact='High')]
Param(
$PRTGURL = "https://{your PRTG URL}/api",
$Auth = "username={your PRTG user}&passhash={your PRTG password hash}",
$Name = "",
$Message = "",
[int]$DaysPaused = 7 #Use -1 to filter for where date is unknown and 0 to return all
)
$Devices = $null
@markwragg
markwragg / Test-SelectStringExamples.ps1
Last active March 30, 2023 20:17
Powershell uses of Select-String to filter a log file and example Regular Expressions for identifying IP addresses and IP spaces
#This regex matches anything that's like an IP address (even invalid ones)
$regexIPAddress = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
#This regex matches anything like an IP address that starts 10, 172 or 192
$regexIPSpace = '(10|172|192).\d{1,3}\.\d{1,3}\.\d{1,3}\b'
#Returns the first IP address in each line of the log file/s then sorts and removes duplicates.
Select-String -Path *.log -Pattern $regexIPAddress | ForEach-Object { $_.Matches } | % { $_.Value } | Sort-Object -Unique | Out-File 'UniqueIPs.txt'
#Returns from a selection of Log files any lines which match a certain string pattern
@markwragg
markwragg / Get-InstalledHotfix.ps1
Last active October 21, 2023 08:24
Powershell script to check whether a hotfix is installed on multiple servers.
[CmdletBinding()]
Param(
$Computers = (Import-csv ".\servers.csv"), #Must include "adaccountname" column
$Patch = "KB2468871"
)
$i = 1
ForEach ($Server in $Computers) {
Write-Progress -Activity "Checking $Server for hotfix $Patch" -Status "$i of $($Computers.Count)" -PercentComplete (($i / $Computers.Count)*100)
@markwragg
markwragg / Sort-ArrayCSV.ps1
Last active September 12, 2016 10:27
Powershell command to sort an array of values and output as a comma separated list.
( @(53,88,135,389,636,3268) | Sort-Object | Out-String | ForEach-Object{$_ -replace '\n', ','} ).TrimEnd(',')
@markwragg
markwragg / Get-PowerShellVersion.ps1
Last active September 12, 2016 10:28
Powershell script to get the installed version of Powershell from multiple servers in an Active Directory domain, optionally filtered by part of an OU name (e.g a location).
#Requires -version 2.0
[CmdletBinding()]
param (
[ValidateSet("Dublin","London”,"Tokyo","Sydney","")][string]$location
)
Import-Module ActiveDirectory -Cmdlet get-adcomputer
$servers = get-adcomputer -filter {Enabled -eq $true -and OperatingSystem -Like "Windows*"} -property Enabled,OperatingSystem | ?{$_.DistinguishedName.contains($location)}
@markwragg
markwragg / Compare-DFStoFolders.ps1
Last active July 3, 2023 14:57
Powershell script to get a list of DFS folder targets for all DFS folders under a defined path and test if those paths are valid from the location running the script.
$Servers = @("SERVER01","SERVER02","SERVER03")
$FolderPaths = $Servers | foreach {
Get-ChildItem "\\$_\DFSShare$"
} | Sort Path
$FolderPaths | Export-Csv "FolderPaths-$(Get-Date -format yyyy-MM-dd).csv" -NoTypeInformation
$TestPaths = (($FolderPaths).FullName | Sort-Object).Trimend('\')
$DFSPaths = ((Import-CSV "DFS-$(Get-Date -format yyyy-MM-dd).csv").TargetPath | Where-Object {($_ -ilike "*SERVER*") | Sort-Object).Trimend('\')
@markwragg
markwragg / Watch-TimePassing.ps1
Last active January 26, 2020 22:12
Powershell script to demonstrate the use of nested progress bars using the write-progress cmdlet. This script uses write-progress to display a running clock of the current time, split in to hours, minutes, seconds and milliseconds. The script stops at midnight.
Do{
Write-Progress -Activity "$((get-date).hour) hours" -PercentComplete (((get-date).hour /23) * 100) -Status "$(24 - (get-date).hour) hours remaining"
Do{
Write-Progress -Id 1 -Activity "$((get-date).minute) minutes" -PercentComplete (((get-date).minute / 59) * 100) -Status "$(60 - (get-date).minute) minutes remaining"
Do{
Write-Progress -Id 2 -Activity "$((get-date).second) seconds" -PercentComplete (((get-date).second / 59) * 100) -Status "$(60 - (get-date).second) seconds remaining"
Do{
$Second = (Get-Date).second
Write-Progress -Id 3 -Activity "$((get-date).millisecond) milliseconds" -Status "The time is $(get-date -f "HH:mm:ss")" -PercentComplete (((get-date).millisecond / 1000) * 100) -SecondsRemaining (86400 - (((get-date).hour * 60 * 60) + ((get-date).Minute * 60) + ((get-date).Second)))
# start-sleep -Milliseconds 100