Skip to content

Instantly share code, notes, and snippets.

Joel Bennett Jaykul

Block or report user

Report or block Jaykul

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Jaykul
Jaykul / ToDictionary.ps1
Created Oct 21, 2019
More LINQ Helpers for PowerShell
View ToDictionary.ps1
# Add a .ToDictionary(KeyType,ValueType) for all hashtables
Update-TypeData -TypeName Hashtable -MemberType ScriptMethod -MemberName ToDictionary -Value {
param([Type]$KeyType,[Type]$ValueType)
[Scriptblock]::Create(
"[Enumerable]::ToDictionary(
[DictionaryEntry[]]@(`$this.GetEnumerator()),
[Func[DictionaryEntry,$($KeyType.FullName)]]{ `$args.Key },
[Func[DictionaryEntry,$($ValueType.FullName)]]{ `$args.Value })"
).Invoke()
}
@Jaykul
Jaykul / SubscriptionNameCompleterAttribute.ps1
Last active Nov 8, 2019
Bonus Argument Completers For Azure
View SubscriptionNameCompleterAttribute.ps1
using namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
using namespace System.Management.Automation.Language
using namespace System.Management.Automation
using namespace System.Collections.Generic
using namespace System.Collections
[AttributeUsage("Property,Field")]
class SubscriptionNameCompleter : ArgumentCompleterAttribute {
# PowerShell expects you to write IArgumentCompleter and register them with this syntax:
# [ArgumentCompleter([MyIArgumentCompleter])]
@Jaykul
Jaykul / Measure-SubstringCount.ps1
Last active Sep 26, 2019
Using that RegularExpression MatchEvaluator in PowerShell
View Measure-SubstringCount.ps1
function Measure-SubStringCount {
<#
.SYNOPSIS
Counts occurrences of a regex pattern in the input, shows them with -Verbose
.DESCRIPTION
Written as an example of how the Regex MatchEvalutor can be (ab)used for good.
#>
[CmdletBinding()]
param(
[string]$Pattern,
@Jaykul
Jaykul / About Split-WtTab.md
Last active Sep 23, 2019
I wrote all of this just to get Split-WTTab
View About Split-WtTab.md

I've been using Windows Terminal since the source code was released, and now that we have support for split panes, I wanted a way to easily create a tab with three panes (one for each version of PowerShell I want to test against).

Unfortunately, there's no support for opening tabs from the command-line yet, nor is there any support yet for specifying which profile to use in a new split terminal. There are open issues for those features, but although I've done a PR or two for it, I didn't think it would be an easy thing to pull together, but I knew that new tabs use the default profile.

Switching the default profile and then creating a new split with the hotkeys works great,

@Jaykul
Jaykul / InformationView.ps1xml
Created Sep 17, 2019
Formatting InformationRecord does not work
View InformationView.ps1xml
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>InformationRecord</Name>
<OutOfBand />
<ViewSelectedBy>
<TypeName>System.Management.Automation.InformationRecord</TypeName>
</ViewSelectedBy>
<CustomControl>
@Jaykul
Jaykul / About My PSModulePath.md
Last active Sep 4, 2019
The crazy things we do with PSModulePath ...
View About My PSModulePath.md

As a general rule, most people can just edit the PSModulePath variable in their Environment Variables ...

However, I am not most people.

I want to test all the things, and I want to use PowerShell 7, 6, and 5 interchangeably:

  • I have to use PowerShell 5 for most of our work code (we're provisioning Windows 10, and 5 is what's on the box)
  • I want to test PowerShell 7 (or whatever the current pre-release is)
  • I want to use the current "release" version (PowerShell 6.2, right now) as my daily driver

I don't want to install three or more versions of the Azure modules and keep them all up to date. One copy works interchangeably in all versions of PowerShell. It would be crazy to install that same version three times (once for each version of PowerShell), but that's what Microsoft seems to want me to do.

@Jaykul
Jaykul / ShouldHaveAParameter.ps1
Last active Aug 30, 2019
A better Parameter assertion for Pester Should
View ShouldHaveAParameter.ps1
function Assert-HasAParameter {
<#
.SYNOPSIS
Asserts that a command has the expected parameter (and supports aliases)
.EXAMPLE
Get-Command "Invoke-WebRequest" | Assert-HasAParameter Uri -Mandatory
.EXAMPLE
Get-Command "Invoke-WebRequest" | Should -HaveAMandatoryParameter Uri
@Jaykul
Jaykul / Test-OutboundIP.ps1
Created Aug 9, 2019
A test for our environments, to make sure NAT worked
View Test-OutboundIP.ps1
Describe "Outbound traffic from App Servers should be load balanced" {
$Servers = Invoke-Command -Cn ($Env:COMPUTERNAME -replace "MGT", "DC") {
Get-ADComputer -Filter * | Where-Object Name -notmatch "SQL|DB|ETL|Cluster|DC" | Select-Object -ExpandProperty Name
}
It "Appears all traffic comes from a single IP Address" {
$IPs = Invoke-Command -EnableNetworkAccess -Cn $Servers { Invoke-RestMethod http://ipinfo.io/json }
$Servers.Count | Should -BeGreaterThan 4
$Ips.Count | Should -Be $Servers.Count
@Jaykul
Jaykul / FileEditor.Gui.ps1
Created Aug 9, 2019
The problem with gui.cs is once you create something, you can't create another. I.e. you can't run this script twice.
View FileEditor.Gui.ps1
using namespace Terminal.Gui # maybe it isn't installed yet, but who cares?
if (!(Test-Path Libraries)) {
mkdir Libraries -Confirm
}
if (!(Test-Path .\Libraries\Terminal.Gui.0.24.0\lib\netcoreapp2.0\Terminal.Gui.dll)) {
Install-Package Terminal.Gui -Destination .\Libraries\
}
add-type -path .\Libraries\NStack.Core.0.11.0\lib\netstandard1.5\NStack.dll
add-type -path .\Libraries\Terminal.Gui.0.24.0\lib\netcoreapp2.0\Terminal.Gui.dll
@Jaykul
Jaykul / Connect-RemoteDesktop.ps1
Created Aug 6, 2019
Using WASP to remote desktop, for those ocassions when policy blocks you ...
View Connect-RemoteDesktop.ps1
#requires -Modules WASP
[CmdletBinding()]
param(
[Parameter(Mandatory)]
$ComputerName,
[Parameter(Mandatory)]
[PSCredential]$Credential
)
You can’t perform that action at this time.