Skip to content

Instantly share code, notes, and snippets.

View deadlydog's full-sized avatar

Daniel Schroeder deadlydog

View GitHub Profile
@deadlydog
deadlydog / PowerShellUtilities.psm1
Last active November 4, 2024 07:43
Powershell Invoke-ScriptBlockWithRetries function to execute arbitrary PowerShell code and automatically retry if an error occurs
function Invoke-ScriptBlockWithRetries {
[CmdletBinding(DefaultParameterSetName = 'RetryNonTerminatingErrors')]
param (
[Parameter(Mandatory = $true, HelpMessage = "The script block to execute.")]
[ValidateNotNull()]
[scriptblock] $ScriptBlock,
[Parameter(Mandatory = $false, HelpMessage = "The maximum number of times to attempt the script block when it returns an error.")]
[ValidateRange(1, [int]::MaxValue)]
[int] $MaxNumberOfAttempts = 5,
@deadlydog
deadlydog / Import-PfxCertificate.ps1
Last active October 26, 2024 05:16
PowerShell script that imports a .pfx certificate file. Useful to do before building the solution on a build server. For more info, see https://blog.danskingdom.com/creating-a-pfx-certificate-and-applying-it-on-the-build-server-at-build-time/
param([string] $PfxFilePath, $Password)
# You may provide a [string] or a [SecureString] for the $Password parameter.
$absolutePfxFilePath = Resolve-Path -Path $PfxFilePath
Write-Output "Importing store certificate '$absolutePfxFilePath'..."
Add-Type -AssemblyName System.Security
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($absolutePfxFilePath, $Password, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"PersistKeySet")
@deadlydog
deadlydog / PowerShellSingleFileScriptTemplate.ps1
Last active August 30, 2024 14:59
PowerShell template for a single-file script
<#
.SYNOPSIS
PUT SHORT SCRIPT DESCRIPTION HERE AND ADD ANY ADDITIONAL KEYWORD SECTIONS AS NEEDED (.PARAMETER, .EXAMPLE, ETC.).
#>
[CmdletBinding()]
param (
# PUT PARAMETER DEFINITIONS HERE AND DELETE THIS COMMENT.
)
process {
@deadlydog
deadlydog / InvokeMethodWithRetries.cs
Created December 15, 2021 04:16
C# generic function to execute any function, retrying if an exception is thrown
public static T InvokeMethodWithRetries<T>(Func<T> method, int maxNumberOfAttempts = 5)
{
int numberOfAttempts = 0;
while (numberOfAttempts < maxNumberOfAttempts)
{
try
{
return method.Invoke();
}
catch (Exception ex)
@deadlydog
deadlydog / MigrateIisAppsAndConfigurationToOtherServer.cmd
Created November 17, 2022 16:24
Migrate IIS Apps And Configuration ToOther Server
:: This script will migrate all of the IIS Applications and Configuration from the local server to the destination server.
:: This script needs to be ran from the source server that has the IIS configuration that you want to migrate.
:: MsDeploy will often generate a lot of output, so it's best to remote desktop onto the server that you want to migrate,
:: rather than running this command via PowerShell Remoting, as it will take a very long time to pipe the output back to
:: your local machine.
:: The server you are syncing to must also have Web Deploy installed on it. You can download it from:
:: http://www.microsoft.com/en-ca/download/details.aspx?id=43717
:: Be sure to do the "Complete" installation, not just the "Typical" so that the Web Deployment Agent Service gets installed.
@deadlydog
deadlydog / Repository's .editorconfig file
Last active March 12, 2024 21:04
This .editorconfig file should live in the repository and be committed to source control. This file should not contain personal preference settings like `indent_size`, as those should be specified in a parent .editorconfig file outside of the repository.
# This file should only include settings that affect the physical contents of the file, not just how it appears in an editor.
# Do not include personal preference presentation settings like a tab's `indent_size` in this file; those should be specified
# in a parent .editorconfig file outside of the repository.
# v1.7 - Source: https://gist.github.com/deadlydog/bd000162e85c155b243a712c16f7411c
# Ensure that personal preference presentation settings can be inherited from parent .editorconfig files.
root = false
#### Core EditorConfig Options ####
@deadlydog
deadlydog / SlackWebhookTest.ps1
Created November 28, 2023 23:12
PowerShell snippet for testing a Slack webhook
# This code shows how to easily test a Slack webhook from PowerShell. The URL here is a dummy URL.
$slackUri = "https://hooks.slack.com/services/T025sdfsdfsdf/B67sdfsdfsdf/khAfsdfsfsfsdfsdfsdf"
$body = ConvertTo-Json @{
pretext = "Testing Slack integration"
text = "This is just a test to ensure the Slack webhook works and sends messages to the correct Slack channel."
color = "#142954"
}
Invoke-RestMethod -Uri $slackUri -Method Post -Body $body -ContentType 'application/json'
@deadlydog
deadlydog / ExampleUsage.cs
Last active June 28, 2023 09:07
Wraps the .NET IMemoryCache in the Stale Cache pattern to provide Best-Before (Stale) date functionality, allowing apps to achieve better fault tolerance
using Caching;
using Microsoft.Extensions.Logging;
namespace App;
public class ExampleUsage
{
private readonly ILogger<ExampleUsage> _logger;
private readonly StaleMemoryCache _staleMemoryCache;
private readonly IAuthService _authService;
@deadlydog
deadlydog / PowerShell bug reproduction code.ps1
Last active March 10, 2022 04:35
Paste this into the PowerShell command line and you'll get an odd `An error occurred while creating the pipeline` error. Remove one "d" from the end and you'll get the expected `Get-Something is not recognized as a name of a cmdlet` error. This info is related to this tweet: https://twitter.com/deadlydog/status/1500295904615702531?s=20&t=7XvHRhT…
Get-Something dslkfjds lkjfdsl jflkdsfljdsalkf dslkf jlkdsjf lkdsj fljds lkf jdsalkf dslkfj dslfj lkdsjf ldskj flkds jflkjds lkf jdslfk jdsalkf jds jflkds jflkdsjflkds flkjdsflk jdslkf lkdsjflkdsj fljds lkf jdslfj sldjf kds flk jdsfl dsf dsljf ds jfhds lf jdsjf dsljflkds jfsjldsj fl jdsflkjdsflkjds dsfadsf dd
@deadlydog
deadlydog / Personal .editorconfig file
Last active January 2, 2022 05:24
This .editorconfig file should live outside of the repository (and thus not be committed to source control) in a parent directory, as it includes personal preference settings like `indent_size`.
# This .editorconfig file should live outside of all repositories (and thus not be committed to source control) in
# a parent directory, as it includes personal preference settings, like a tab's `indent_size`.
# v1.3
root = true
[*]
charset = utf-8
end_of_line = crlf
indent_style = tab