Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / xkcd.psm1
Last active August 28, 2024 08:01
PowerShell function to get one or more XKCD comics via the API and return it's details as an object. IDs can be passed via the pipeline. Has a -Random switch to select a Random comic with which a -Min and -Max range can optionally be specified. Also has a -Newest switch to return the latest x comics. Mostly an excuse to play with Parameter Sets.
Function Get-XKCD{
[cmdletbinding(DefaultParameterSetName=’Specific’)]
Param (
[Parameter(ParameterSetName=’Specific’,ValueFromPipeline=$True,Position=0)][int[]]$Num,
[Parameter(ParameterSetName=’Random’)][switch]$Random,
[Parameter(ParameterSetName=’Random’)][int]$Min = 1,
[Parameter(ParameterSetName=’Random’)]
[int]$Max = ((Invoke-WebRequest "http://xkcd.com/info.0.json").Content | ConvertFrom-Json).num,
[Parameter(ParameterSetName=’Newest’)][int]$Newest,
[switch]$Download
@markwragg
markwragg / Get-ParameterAliases
Created January 13, 2017 13:58
Was curious about how to find the aliases of parameters in Powershell. Turns out also get-help -full and get-help -parameter shows them from PS3 onwards.
#Adapted from here http://mikefrobbins.com/2012/08/30/finding-aliases-for-powershell-cmdlet-parameters/
(get-command *).parameters.values | select name,aliases | sort aliases -unique | sort name
@markwragg
markwragg / glossary.ps1
Last active November 11, 2016 16:45
Powershell Azure function for sending messages to Slack
$requestBody = Get-Content $req -raw
$requestBody = $requestBody.Replace('&',"`n")
$requestBody = ConvertFrom-StringData -StringData $requestBody
$Response = (Import-CSV "D:\home\site\wwwroot\Glossary\Glossary.csv") | Where-Object {$_.Term -like "$($requestBody.text)*"}
if(!$Response){Out-File -Encoding Ascii -FilePath $res -inputObject "Sorry, there is no match in the Glossary for *$($requestBody.text)*."}
$Response | ForEach-Object{
Out-File -Encoding Ascii -FilePath $res -inputObject "*$($_.Term):* $($_.Description)" -Append
@markwragg
markwragg / Glossary.psm1
Last active August 28, 2024 08:01
A Powershell Module that includes a function for searching a CSV file which is a Glossary of terms and presents the results in a variety of formats.
<#
.Synopsis
Glossary Module
.DESCRIPTION
A Powershell Module that includes a function for searching a CSV file which is a Glossary of terms and presents the results in a variety of formats.
.EXAMPLE
Search-Glossary MyTerm
.EXAMPLE
Search-Glossary MyTerm -PassThru | Export-CSV MyTerms.csv
#>
@markwragg
markwragg / Audit-Computer.ps1
Created November 8, 2016 10:41
Pester tests for validating the configuration of a server matches the configuration captured previously (e.g prior to a change).
Get-WmiObject Win32_logicaldisk | Export-Clixml "$env:computername-LogicalDisks.xml"
(Get-Service | select name,displayname,status) | Export-Clixml "$env:computername-Services.xml"
(Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object {$_.IPEnabled -eq "True"}) | Export-Clixml "$env:computername-Network.xml"
@markwragg
markwragg / SEP.tests.ps1
Last active May 22, 2019 19:33
Pester tests for Symantec Endpoint Protection to perform operational validation of configuration and health.
Param(
$SEPLatestVersion = "12.1.7"
)
Describe 'Symantec Endpoint Protection checks' {
Context 'SEP service checks' {
$SEPServices = @('SepMasterService','SmcService')
@markwragg
markwragg / Send-Credentials.ps1
Created October 12, 2016 07:53
Powershell script for communicating a list of credentials to users by email. Beware these credentials are therefore communicated in plain text.
[CmdletBinding()]
Param(
[CmdletBinding(SupportsShouldProcess = $true)]
$Inputfile
)
Import-CSV Credentials.csv | ForEach-Object {
$Body = "Dear $($_.GivenName),
@markwragg
markwragg / Remove-OldFiles.ps1
Last active November 6, 2022 21:54
Powershell script to remove files older than a certain date and log the files removed to a file.
[CmdletBinding()]
Param(
$limit = (Get-Date).AddDays(-7),
$path = "C:\Example\Path"
)
# Delete files older than the $limit.
$FilesToDelete = (Get-ChildItem -Path $path -Exclude Logs | Where-Object { $_.LastWriteTime -lt $limit }) | Select Path
# If files are deleted create a log detailing this
@markwragg
markwragg / Get-FolderSize.ps1
Last active October 5, 2016 13:02
Powershell script to get the total size of a list of folders and the last modified date of the newest file in each folder. The script takes a CSV input file which needs to be a list of paths with a header of "path".
[CmdletBinding()]
param (
$Paths = (Import-CSV "Paths.csv")
)
$i = 0
$Total = 0
$Paths | ForEach-Object {
@markwragg
markwragg / Test-Pester.tests.ps1
Last active May 1, 2022 20:12
Powershell pester script with various examples of using pester for operational validation.
Describe 'My process checks' {
Context 'Checking essential Windows processes are running' {
It 'winlogon.exe is running' {
get-process -Name 'winlogon' | Should be $true
}
}