Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / ClassDefinitionFromObject.ps1
Created May 17, 2017 14:08
PowerShell function to create a Class Definition from an existing object. Forked from Brian Scholer (https://github.com/briantist).
function New-ClassDefinitionFromObject {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[object]$InputObject,
[Parameter()]
[String]$Name = 'Root',
[SupportsWildcards()]
@markwragg
markwragg / Sort-Natural.ps1
Last active September 26, 2024 21:54
PowerShell natural sort. A regex way to sort files that have a number in them correctly, e.g rather than img1, img10, img11, img2, -> img1, img2, img10, img11
# http://stackoverflow.com/a/5429048/2796058
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
Get-ChildItem | Sort-Object $ToNatural
@markwragg
markwragg / Get-LocalTime.ps1
Last active May 30, 2017 08:40
PowerShell function that uses Google APIs to get the current local time for any valid address string. Requires a Google API key.
Function Get-LocalTime {
[cmdletbinding()]
Param(
[Parameter(Mandatory=$true)]
$Address,
$Time = (Get-Date),
#Get an API key from here: https://developers.google.com/apis-explorer/
$APIKey = (Import-Clixml "$env:USERPROFILE\$ENV:USERNAME-GoogleApi.xml"),
@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 }
@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 / 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 / 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 / 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 / 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 / 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)