Skip to content

Instantly share code, notes, and snippets.

View johlju's full-sized avatar
👋
Reviewing, coding and reviewing again

Johan Ljunggren johlju

👋
Reviewing, coding and reviewing again
View GitHub Profile
@johlju
johlju / debug.ps1
Last active February 2, 2024 06:39
Konstigt proplem med `-replace`
# Innehållet i $b ska ersätta raden "#placeholder parameters" i innehållet av $a.
# Men saker får fnatt som man kan se i result.txt och den verkar "loopa" och göra help knasiga saker.
# Testat i PS7.4.1 enbart.
# Löste det genom att istället använda [RegEx]::Replace(), men vore roligt att veta varför.
$a = @'
# Version v#.#.# (yyyy-MM-dd)
#placeholder parameters
@johlju
johlju / Update_GitHubPAT_on_AzureDevOps.ps1
Last active August 20, 2023 07:22
DSC Community: Update GitHub PAT on Azure Pipelines
# Prerequisites:
#
# Install Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
# Install AzureDevOps extension: az extension add --name azure-devops
# Install 1Password CLI: https://developer.1password.com/docs/cli/get-started
# save the raw script in a temporary location: https://gist.github.com/johlju/c7be5816c52c9aff7b4a00ff01d435a8
Invoke-WebRequest -Uri 'https://gist.githubusercontent.com/johlju/c7be5816c52c9aff7b4a00ff01d435a8/raw/d73ea890b03af12de6b9dd174f5af6e08abe760d/AutomateSettingGitHubPAT.ps1' -OutFile 'AutomateSettingGitHubPAT.ps1'
# Login with the account that access DSC Community pipelines:
@johlju
johlju / PSResourceObject.ps1
Last active January 31, 2024 09:44
PowerShell class example that implements `IComparable` and `System.IEquatable`
class PSResourceObject : IComparable, System.IEquatable[Object]
{
[System.String]
$Name
[System.Version]
$Version
[System.String]
$PreRelease
@johlju
johlju / AutomateSettingGitHubPAT.ps1
Last active August 17, 2023 11:51
Automate setting GitHubToken secret variable
# Install Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
# Install AzureDevOps extension: az extension add --name azure-devops
# Login with the account that access DSC Community pipelines: az login --allow-no-subscriptions
# Run this script and pass the PAT in the parameter.
[CmdletBinding()]
param
(
# TODO: Fix as PSCredential so that PAT is not part of PowerShell command line history
[Parameter(Mandatory = $true)]
@johlju
johlju / DscConfiguration.ps1
Last active July 2, 2022 17:16
Problem returning property that is using class for a class-based DSC resource
# For some attempts
using module SqlServerDsc
$ConfigurationData = @{
AllNodes = @(
@{
NodeName = 'localhost'
PSDscAllowDomainUser = $true
PSDSCAllowPlainTextPassword = $true
}
@johlju
johlju / README.md
Last active June 4, 2022 12:30
Steps to rename a DSC Community module repository

Steps to rename a DSC Community module repository

GitHub repository, Azure Devops and PowerShell Gallery

  1. Send in a PR (and merge it) to update the pipelines files so the repo are using the latest from Sampler.
    ./build.ps1 -ResolveDependency -Tasks build
    # Use the default values and answer Y on all questions 
    New-SampleModule -DestinationPath '..\' -ModuleType 'dsccommunity' -ModuleAuthor 'DSC Community contributors' -ModuleName (Split-Path -Leaf $PWD) -ModuleDescription 'no description' -LicenseType MIT -SourceDirectory 'source' -ModuleVersion '0.0.1' 
    Get-ChildItem -Path *.bak -Recurse -Force | Remove-Item -Force
    
@johlju
johlju / Invoke-PesterWrapper.ps1
Created April 21, 2022 05:20
Invoke-PesterWrapper
# Add this to your PowerShell $PROFILE
function Invoke-PesterWrapper
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0)]
[System.String]
$ResourceName,
@johlju
johlju / Get-CoveredInstruction.ps1
Created April 3, 2021 10:38
Get expected missed & covered instructions from code coverage
<#
Source:
SourceLineNumber HitCount
---------------- --------
20 219
22 219
22 219
22 219
@johlju
johlju / gist:8578bf8cf693bbb2dc8f818820323759
Created May 27, 2020 16:44
Pester 5 MockUp Fails on SwitchParamater
BeforeAll {
Remove-Module -Name 'DscResource.Common' -Force
New-Module -Name 'DscResource.Common' -ScriptBlock {
function Assert-Module
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
@johlju
johlju / Notes.txt
Last active May 16, 2020 09:34
Notes when converting to Pester 5
* Should -Throw has a breaking change that it is no longer using `-contains` but instead `-like`
Expected an exception, with message 'Robocopy reported errors when copying files. Error code: 8.' to be thrown, but the message was 'System.InvalidOperationException: Robocopy reported errors when copying files. Error code: 8. (SQLCOMMON0012)'.
Solution, wasn't using the localized string (used hard-coded string) and then needed to use test helper Get-InvalidOperationRecord to get the correct string that contains System.InvalidOperationException (alternative is to use wildcards *)
Assert-MockCalled -> Should -Invoke (uses the same parameters, so backward compatible)
InModuleScope must be inside Describe-block.
Use it where it is needed, for example using localization variable.