Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@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 / 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 / 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-cmdletstats.ps1
Last active January 17, 2017 11:34
Powershell command to see the max min and average length of cmdlets. Decided to do this after seeing how crazy long Get-WinAcceptLanguageFromLanguageListOptOut is as a cmdlet and wondering if it was the longest. Also used this to experiment with calculated properties and discovered you can use them in sort and select as well as where.
#Get stats on length of the cmdlet name such as max, min, average, sum
Get-Command | where CommandType -eq cmdlet | select -ExpandProperty name | measure length -Average -Sum -Max -Min
# See the list, sorted by length
Get-Command | where CommandType -eq cmdlet | select -ExpandProperty name | sort length
#Another way to get this statistic using calculated properties
Get-Command | where CommandType -eq cmdlet | select name,@{N='length';E={$_.name.length}} | measure length -Average -Sum -Max -Min
# See the list this way, sorted by length (we can skip the select and use the expression in sort)
@markwragg
markwragg / Build.ps1
Created January 19, 2017 15:44
The start of a build script which publishes the module to PSGallery having incremented the version number
# Required module - BuildHelpers: https://github.com/RamblingCookieMonster/BuildHelpers
[cmdletbinding()]
Param(
[string]$PSGalleryKey = (Import-Clixml PSGalleryKey.xml), #So I don't put it on the internet.
[string]$Module,
[string]$Version,
[switch]$Publish
)
$ModuleData = (Get-Module $Module)
@markwragg
markwragg / The Operations Report Card.md
Last active November 25, 2018 23:30 — forked from sjourdan/The Operations Report Card.md
The Operations Report Card

The Operations Report Card

Source: http://www.opsreportcard.com/.

Public Facing Practices

  1. Are user requests tracked via a ticket system?
  2. Are "the 3 empowering policies" defined and published?
  3. How do users get help?
  4. What is an emergency?
@markwragg
markwragg / PartOfDay.ps1
Created March 15, 2017 16:13
A PowerShell function to return the salutation for the part of day it is currently, either Morning, Afternoon or Evening.
Function Get-PartOfDay {
Param (
[int]$Hour = (Get-Date).Hour
)
Switch ($Hour) {
{$_ -in 0..11} {'Morning'}
{$_ -in 12..17} {'Afternoon'}
{$_ -in 18..23} {'Evening'}
}
}
@markwragg
markwragg / ModuleBuild.ps1
Last active March 26, 2023 03:13 — forked from RamblingCookieMonster/zModuleBuild.ps1
A PowerShell script for creating the default scaffolding and files for a new PowerShell module.
[cmdletbinding()]
Param(
[Parameter(Mandatory=$True)]
[string]$ModuleName,
[string]$Path = "C:\Users\$env:UserName\Documents\Code\$ModuleName",
[string]$Author = 'Mark Wragg',
[string]$Description = '',
[version]$PSVersion = '3.0'
)
@markwragg
markwragg / install-module
Created March 27, 2017 13:14
Using package management to install a PowerShell module to just your own user scope when you have restrictive (non-admin) rights,
Install-Module <modulename> -scope CurrentUser
@markwragg
markwragg / Get-OrdinalNumber.ps1
Created April 3, 2017 09:44
PowerShell function to add the appropriate ordinal number suffix to an integer for any number and return the number and suffix. Based on: http://stackoverflow.com/questions/19501722/how-to-convert-a-date-string-with-ordinal-suffix-to-another-format
Function Get-OrdinalNumber {
Param(
[Parameter(Mandatory=$true)]
[int64]$num
)
$Suffix = Switch -regex ($Num) {
'1(1|2|3)$' { 'th'; break }
'.?1$' { 'st'; break }
'.?2$' { 'nd'; break }