Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / Glossary.psm1
Last active November 11, 2016 20:11
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 / 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 / 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 / xkcd.psm1
Last active January 17, 2017 16:59
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 / 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 / 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 / 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 / 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 / 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 / ConvertDateProperties.ps1
Last active May 19, 2017 11:43
PowerShell Function to recurse all levels of a nested object and convert valid date strings to [datetime] properties
Function Convert-DateProperties {
<#
.SYNOPSIS
Converts string properties that have 'date' in their name to datetime properties, recursively in all levels of an object.
.PARAMETER object
The object to convert.
.PARAMETER name
The string that should be matched in the name property of the object. Default = 'Date'.
.EXAMPLE
$MyObj | Convert-DateProperties