Skip to content

Instantly share code, notes, and snippets.

Avatar

Justin Grote JustinGrote

View GitHub Profile
@JustinGrote
JustinGrote / StartVSCodeServer.ps1
Last active Aug 9, 2022
Powershell Bootstrap script for VSCode Server
View StartVSCodeServer.ps1
#require -version 5.1
#Usage: iwr https://tinyurl.com/VSCodeServer | iex
#Parameterized usage: & ([ScriptBlock]::Create((iwr https://tinyurl.com/VSCodeServer))) -Your -Options
param(
#Path to install the vscode binary to. This defaults to a folder in your Local AppData path. Must be writable by your current user without sudo
[string]$InstallPath,
#Installation architecture. This is normally autodetected.
$Arch,
#Installation platform. Normally autodetected.
$Target = 'unknown-linux-gnu',
@JustinGrote
JustinGrote / FilterExample.psm1
Last active Jul 22, 2022
Example of Filter to convert a ForEach
View FilterExample.psm1
#Filter is just a function with the process{}
#block as the default rather than end{}. That's it, it's not scary.
# $y = 1..3
# foreach ($x in $y) {
# "This is item $x"
# }
# #Result:
# #This is item 1
@JustinGrote
JustinGrote / IncorrectUseOfFormatCmdletsInMiddleOfPipeline.psm1
Last active Jul 18, 2022
Warn if Format command (Format-Table, etc.) are used in the middle of a pipeline.
View IncorrectUseOfFormatCmdletsInMiddleOfPipeline.psm1
using namespace System.Management.Automation.Language
using namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
function IncorrectUseOfFormatCmdletsInMiddleOfPipeline {
<#
.SYNOPSIS
Do not place format-table in the middle of a pipeline
.DESCRIPTION
Detects when Format commands are commonly misused in the middle of a pipeline, causing unexpected results with display objects that dont have the same properties as the original piped object.
@JustinGrote
JustinGrote / install-powershell.ps1
Last active Jul 9, 2022
PowerShell Bootstrap Script
View install-powershell.ps1
<#
.SYNOPSIS
Install PowerShell on Windows, Linux or macOS.
#>
[CmdletBinding(SupportsShouldProcess)]
param(
#Where PowerShell should be installed.
[string]$Destination,
#The version to install. Default is the latest stable version. Additional Options are a LTS, Preview, Daily, or a specific version number tag.
[ValidateNotNullOrEmpty][string]$Version = 'Stable'
@JustinGrote
JustinGrote / Add-JAzADServicePrincipalRole.ps1
Created Jul 8, 2022
Add a Role to a SAML-only Enterprise Application that you can then provide as a claim for user assignment (there is no GUI for this)
View Add-JAzADServicePrincipalRole.ps1
using namespace Microsoft.Azure.PowerShell.Cmdlets.Resources.MSGraph.Models.ApiV10
using namespace System.Management.Automation
using namespace System.Collections.Generic
function Add-JAzADServicePrincipalRole {
[CmdletBinding(DefaultParameterSetName='ByObject', SupportsShouldProcess)]
param(
#The display name of the role. This is the name that will be displayed in the app portal when assigning the role to a user or group.
[Parameter(Mandatory)]$Name,
#The value that will be added to the SAML roles claim for the user. Make this something that is appropriate to signal to your application what role the user should have, e.g. admin, owner, etc., your App may have specific documentation for this effect, for example Palo Alto defines specific values like superuser and superreader
@JustinGrote
JustinGrote / PowerShellModuleTemplateProvider.class.ps1
Created Jul 8, 2022
Implementing a Microsoft .NET Template Engine Package Provider as a Powershell Class - Just because you can doesn't mean you should
View PowerShellModuleTemplateProvider.class.ps1
using namespace System
using namespace System.Collections.Generic
using namespace System.Threading
using namespace System.Threading.Tasks
using namespace Microsoft.TemplateEngine.Abstractions
using namespace Microsoft.TemplateEngine.Abstractions.TemplatePackage
class PowerShellModuleTemplatePackageProvider : ITemplatePackageProvider {
[ITemplatePackageProviderFactory]$Factory
@JustinGrote
JustinGrote / Get-ParamBlock.ps1
Created Jul 7, 2022
Get a hashtable of the parameters in your param block, useful for splatting.
View Get-ParamBlock.ps1
using namespace system.collections.generic
function Get-ParamBlock ([String[]]$Name) {
[hashset[string]]$params = $PSCmdlet.MyInvocation.MyCommand.Parameters.Keys
$params.ExceptWith([string[]]([PSCmdlet]::CommonParameters + [PSCmdlet]::OptionalCommonParameters))
$result = @{}
if ($Name) {$params = $params -in $Name}
foreach ($name in $params) {
$result.$name = $PSCmdlet.GetVariableValue($name)
}
return $result
@JustinGrote
JustinGrote / Write-FunctionError.ps1
Last active Jul 6, 2022
Write an Error within a function in a nice way that displays the context of the function rather than the "Write-Error" context
View Write-FunctionError.ps1
using namespace System.Management.Automation
using namespace Microsoft.PowerShell.Commands
function Write-FunctionError {
<#
.SYNOPSIS
Writes an error within the context of the containing CmdletBinding() function. Makes errr displays prettier
#>
param(
[Parameter(Mandatory)][String]$Message,
[ValidateNotNullOrEmpty()][ErrorCategory]$Category = 'WriteError',
@JustinGrote
JustinGrote / Receive-Task.ps1
Last active Jul 5, 2022
"Await" one or more tasks in PowerShell in a cancellable manner (e.g. ctrl-c still works)
View Receive-Task.ps1
using namespace System.Threading.Tasks
using namespace System.Collections.Generic
filter Receive-Task {
#Wait on one or more tasks in a cancellable manner
[CmdletBinding()]
param(
[parameter(Mandatory, ValueFromPipeline)][Task]$Task,
#How long to wait before checking for a cancellation in milliseconds
[int]$WaitInterval = 500
)
View oh-my-posh.yaml
# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json
blocks:
- alignment: left
newline: true
segments:
- type: path
background: '#333333'
foreground: '#CCCCCC'
properties: