Skip to content

Instantly share code, notes, and snippets.

View aldrichtr's full-sized avatar

Tim Aldrich aldrichtr

View GitHub Profile
# Test Report: Pester test report for stitch
* Date: 2023-03-03
* Time: 23:09:44
Expand the following summaries for more details:
<details>
<summary> Environment:
@aldrichtr
aldrichtr / user-profile.psm1
Created June 30, 2023 20:15
PowerShell functions to create a new user profile
<#
.Synopsis
Rough PS functions to create new user profiles
.DESCRIPTION
Call the Create-NewProfile function directly to create a new profile
.EXAMPLE
Create-NewProfile -Username 'testUser1' -Password 'testUser1'
.NOTES
Created by: Josh Rickard (@MS_dministrator) and Thom Schumacher (@driberif)
# Set your PowerShell execution policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
function Invoke-ComputerSetup {
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
SupportsShouldProcess=$true,
PositionalBinding=$false,
<#
If you create enums and/or custom attributes and want to reuse them, you would (or at least, I did) think that they should go into psm1 module files and be included in other modules via the NestedModules line of the module manifest.
BUT YOU WOULD BE (or at least, I was) WRONG!
Instead, create them in .ps1 script files and add them to the ScriptsToProcess line of the module manifest.
And if you use the enum directly in Pester tests, you will need to run the enum script file there, too, as they do not persist between scripts (as noted here: https://tfl09.blogspot.com/2014/11/enums-in-powershell-v5.html).
That said, if you follow this advice for enums (is it that the enums are in ScriptsToProcess, or anything? I haven't tested), you may need to refactor classes into THEIR own psm1 files. But in so doing, you may break Get-Help and intellisense for functions that take objects of those classes as parameters. At which point you may wind up with all the Enums and Classes back in the main psm1 file(s).
By way of
#Classes do not always play well in the nested modules section of a manifest. I've had trouble with importing classes in multiple files.
#My current approach is: Create a .ps1 file WITHOUT the standard clauses of BEGIN{} PROCESS{} END{} that uses invoke-expressions:
$invocables = @()
$invocables += $( get-content "$PSScriptRoot\MyFolder\MyClass.psm1" ) -join "`r`n"
$invocables += $( get-content "$PSScriptRoot\MyOtherClassThatDepenedsOnTheFirstOne.psm1" ) -join "`r`n"
foreach ($i in $invocables) { Invoke-Expression -Command $i }
#Classes do not always play well in the nested modules section of a manifest. I've had trouble with importing classes in multiple files.
#My current approach is: Create a .ps1 file WITHOUT the standard clauses of BEGIN{} PROCESS{} END{} that uses invoke-expressions:
$invocables = @()
$invocables += $( get-content "$PSScriptRoot\MyFolder\MyClass.psm1" ) -join "`r`n"
$invocables += $( get-content "$PSScriptRoot\MyOtherClassThatDepenedsOnTheFirstOne.psm1" ) -join "`r`n"
foreach ($i in $invocables) { Invoke-Expression -Command $i }
#Simple functions and aliases that don't rise to the level of a module that I like having in my PowerShell Profile
function sudo {[CmdletBinding(<#PositionalBinding=$True#>)]PARAM([String]$CommandLine,[Switch]$RunPrior) IF($RunPrior.IsPresent) {$CommandLine = (Get-History | Select-Object -Last 1).CommandLine}; Write-Verbose "Running: $CommandLine"; start-process powershell -verb:Runas "$CommandLine"}
function outlook {. 'C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE' /restore}
function newdir {PARAM($Path) new-item -ItemType Directory -Path $Path }
function ssms2008 {ls 'C:\Program Files\', 'C:\Program Files (x86)\' -Filter micro*Sql*server | ls -Filter '100' | ls -Recurse -Filter *ssms*exe | ii}
@aldrichtr
aldrichtr / PracticalSubmodulesWithGitAndPester.txt
Created July 7, 2023 14:45 — forked from sdoubleday/PracticalSubmodulesWithGitAndPester.txt
Notes on the act of developing with external repositories (i.e. submodules) in git and pester
Notes on the act of developing with external repositories (i.e. submodules) in git and pester
*To the reader:* If you are here looking for expert advice, please leave as swiftly as possible. No such advice is present here. This is purely a set of reminders for the physical(?) act of how I handle working with git submodules and pester tests.
This assumes you are working in PowerShell. Version 5, if it matters, with git 2.10 (or later? It was what I had when I wrote this) and pester.
*Downloading and using a repo with submodules* - When I was getting started with this, I NEVER developed submodules from the main module. I schlepped over to the submodule, branch, develop, merge back to master, and then come back here and update the submodule, and I still think that's a good policy for anything other than mass hotfixes.
Clone the repo as normal. Here, the folder PesterNewFixtureModule will be created in the current folder:
git clone https://github.com/sdoubleday/PesterNewFixtureModule.git PesterNewFixtureModu

Credit: Mark Kraus
Website: https://get-powershellblog.blogspot.com

Collection Type Guidence

When to use what

  • Use Arrays if you know the element types and have a fixed length and/or known-up-front collection size that will not change.
  • Use ArrayList if you have an unkown collection size with either unknown or mixed type elements.
  • Use a Generic List when know the type of the elements but not the size of the collection.
  • Use a HashTable if you are going to do key based lookups on a collection and don't know the object type of the elements.
  • Use a Dictionary<TKey, TValue> you are going to do key based lookups on a collection and you know the type of the elements.
  • Use a HashSet when you know the type of elements and just want unique values and quick lookups and assignmnets.
@aldrichtr
aldrichtr / Microsoft.PowerShell_profile.ps1
Created August 15, 2023 20:58 — forked from dprice/Microsoft.PowerShell_profile.ps1
PowerShell Profile #tag PowerShell
$sw = [Diagnostics.Stopwatch]::StartNew()
Write-Host "Updating path..." -foregroundColor DarkGreen
if(!($env:path).contains((Split-Path $PROFILE))) {
$env:path += ";" + (Split-Path $PROFILE)
}
if(!($env:path).contains('D:\dNx\dnx-util;')) {
$env:path += ";" + 'D:\dNx\dnx-util;'
}
if(!($env:path).contains('D:\dNx\dnx-util\test;')) {
$env:path += ";" + 'D:\dNx\dnx-util\test;'