Skip to content

Instantly share code, notes, and snippets.

View JustinGrote's full-sized avatar

Justin Grote JustinGrote

View GitHub Profile
@JustinGrote
JustinGrote / TestICMP.csproj
Last active February 14, 2023 18:10
Async Pinger Powershell Cmdlet in C#
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Management.Automation" Version="7.3.2" PrivateAssets="all" />
@JustinGrote
JustinGrote / Get-MgServicePrincipalPermission.ps1
Last active April 3, 2024 21:56
Get a list of application and delegated permissions for a service principal, similar to what the Azure Portal shows
#requires -version 7 -module Microsoft.Graph.Applications
using namespace Microsoft.Graph.PowerShell.Models
using namespace System.Collections.Generic
enum MicrosoftGraphServicePrincipalType {
Application
Delegated
}
class MgServicePrincipalPermission {
@JustinGrote
JustinGrote / Sync-PrivateDnsZone.ps1
Created November 10, 2022 18:26
Copy all A records from one Private DNS zone to another (or multiple)
using namespace Microsoft.Azure.Commands.PrivateDns.Models
filter Sync-AzPrivateDnsZone {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
#The zone to copy A records from
[Parameter(Mandatory)][PSPrivateDNSZone]$SyncFrom,
#The zone to copy A records into
[Parameter(Mandatory, ValueFromPipeline)][PSPrivateDNSZone]$SyncTo,
#How fast to create records
[int]$ThrottleLimit = 10
@JustinGrote
JustinGrote / ThrowStdOutErrors.ps1
Created November 4, 2022 17:40
Catch only specific errors coming from native commands
filter ThrowStdOutErrors($messageFilter,[Parameter(ValueFromPipeline)]$obj) {
if ($obj -is [Management.Automation.ErrorRecord]) {
if ($obj -match $messageFilter) {
throw $obj
} else {
Write-Error $obj
return
}
}
$obj
@JustinGrote
JustinGrote / Find-LatestPackagesWithDependencies.ps1
Last active November 2, 2022 20:54
Get the latest version of all packages from a nuget feed that have dependencies
$items = 0..26 | ForEach-Object -ThrottleLimit 30 -Parallel {
$return = Invoke-RestMethod -Verbose "https://www.powershellgallery.com/api/v2/Packages()?`$filter=IsLatestVersion eq true and Dependencies%20gt%20%27a%27&`$select=Dependencies,Version,NormalizedVersion&`$orderby=Created%20desc&`$top=50000&`$skip=$($PSItem*100)"
$return | ForEach-Object {
[PSCustomObject]@{
Name = $_.title.'#text'
Dependency = $_.Properties.Dependencies
Version = $_.Properties.Version
NormalizedVersion = $_.Properties.NormalizedVersion
}
}
@JustinGrote
JustinGrote / Get-JAzRouteTableSummary.ps1
Created September 29, 2022 14:15
Fetch all routes for all Azure subnets that have a route table attached
using namespace Microsoft.Azure.Commands.Profile.Models.PSAzureSubscription
using namespace Microsoft.Azure.Management.Internal.Resources.Utilities.Models
param(
$Subscriptions = $(Get-AzSubscription)
)
$Subscriptions
| Where-Object State -ne 'Disabled'
| ForEach-Object {
$context = Select-AzSubscription -SubscriptionID $PSItem.Id
@JustinGrote
JustinGrote / StartVSCodeServer.ps1
Last active February 29, 2024 02:37
Powershell Bootstrap script for VSCode Server
#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
[ValidateNotNullOrEmpty()]
[string]$InstallPath = $(Join-Path -Path ([System.Environment]::GetFolderPath('LocalApplicationData')) -ChildPath 'vscode-cli'),
#Installation architecture. This is normally autodetected.
$Arch,
$OS,
@JustinGrote
JustinGrote / FilterExample.psm1
Last active July 22, 2022 01:30
Example of Filter to convert a ForEach
#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 September 6, 2022 11:33
Warn if Format command (Format-Table, etc.) are used in the middle of a pipeline.
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 July 9, 2022 06:07
PowerShell Bootstrap Script
<#
.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'