Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / Get-PRTGSensorAutoAckSetting.ps1
Last active February 18, 2022 19:26
Powershell script for returning the Auto Acknowledge setting for all Ping sensors in PRTG using the API.
[CmdletBinding()]
Param(
$PRTGURL = "https://{your PRTG URL}/api",
$Auth = "username={your username}&passhash={your password hash}",
$SensorType = "ping"
)
$Sensors = ""
$Sensors = (invoke-restmethod "$PRTGURL/table.json?content=sensors&output=json&columns=objid,probe,group,device,sensor,status&count=10000&filter_type=$SensorType&$Auth").sensors
@markwragg
markwragg / Get-PRTGTransactionSensorURLSetting.ps1
Last active September 12, 2016 10:29
Powershell script for getting the URL step settings from all HTTP Transaction sensors in PRTG : http://wragg.io/get-transaction-sensor-url-settings-from-prtg-with-powershell/
[CmdletBinding()]
Param(
$PRTGURL = "https://{your prtg URL}/api",
$Auth = "username={your prtg user}&passhash={your prtg password hash}"
)
$Sensors = ""
$Sensors = (invoke-restmethod "$PRTGURL/table.json?content=sensors&output=json&columns=objid,probe,group,device,sensor,status&count=10000&filter_type=httptransaction&$Auth").sensors
$i = 1
@markwragg
markwragg / Get-Uptime.ps1
Last active September 12, 2016 10:26
Powershell script for getting the uptime of multiple Windows servers using WMI : http://wragg.io/get-uptime-from-multiple-servers/
#Requires -version 2.0
[CmdletBinding()]
param (
[ValidateSet("London","Berlin","Tokyo","")][string]$location
)
Write-Host ("`n" * 5)
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 / 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
@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 / 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 / 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-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 / 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 / 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