Skip to content

Instantly share code, notes, and snippets.

View steviecoaster's full-sized avatar
🤠
It's a sweet day to write some Powershell

Stephen Valdinger steviecoaster

🤠
It's a sweet day to write some Powershell
View GitHub Profile
@steviecoaster
steviecoaster / switch_scriptblock.ps1
Created January 14, 2025 13:27
Use scriptblocks as evaluation in a PowerShell switch statement
$var = @{A = 10; B = 'abc'}
foreach ($key in $var.Keys) {
switch ($var[$key].GetType()) {
{ $_ -eq [int32] } { "$key + 10 = $($var[$key] + 10)" }
{ $_ -eq [string] } { "$key = $($var[$key])" }
}
}
# Output
@steviecoaster
steviecoaster / sample.ps1
Last active January 10, 2025 21:14
Understanding using Scriptblocks in PowerShell
#Download the sample_script.ps1 contents included in this gist
$url = 'https://gist.githubusercontent.com/steviecoaster/027d2e53b79e200ffb2455f74e2f1308/raw//sample_script.ps1'
$scriptData = [System.Net.WebClient]::new().DownloadString($url)
<#
The above uses the [System.Net.WebClient] .Net class directly, however you can use New-Object if you are more comfortable:
(New-Object System.Net.WebClient).DownloadString('https://gist.githubusercontent.com/steviecoaster/027d2e53b79e200ffb2455f74e2f1308/raw/3bc513de7c664915691b6bbd78c49649069f52c5/sample_script.ps1')
#>
# Next, we create our script block, and fill it with our $scriptData
@steviecoaster
steviecoaster / Bootstrap_IIS.ps1
Created January 9, 2025 02:44
Boot strap IIS on windows to host PowerShell Scripts
<#
.SYNOPSIS
Creates the `ChocolateyInstall` IIS fileshare site.
.DESCRIPTION
Creates a new IIS website named `RtpsugDemo` which hosts the
a collection of scripts for onboarding clients to retrieve and run during their
setup.
If you have a need to re-create this for any reason, ensure the existing
@steviecoaster
steviecoaster / Write-ReverseOutput.ps1
Created January 8, 2025 20:40
Fooling around with Write-Output in PowerShell
function Write-ReverseOuput {
[Alias('Write-Output')]
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[String]
$InputObject
)
end {
@steviecoaster
steviecoaster / Get-AllBoundParameters.ps1
Created January 6, 2025 20:31
Get all parameters to function, including default values
Function Get-AllBoundParameters {
[CmdletBinding()]
Param(
# A simple string parameter that is mandatory
[Parameter(Mandatory)]
[String]
$Name,
# A simple switch
[Parameter()]
@steviecoaster
steviecoaster / machineSetup.ps1
Last active December 10, 2024 20:18
My workstation pkgs
#Requires -RunAsAdministrator
#Set our execution policy for the process
Set-ExecutionPolicy Bypass -Scope Process -Force
#Store our packages.config from the gist here temporarily
$tempFile = Join-Path $env:TEMP -ChildPath 'packages.config'
#Write our config file contents
$configFile = 'https://gist.githubusercontent.com/steviecoaster/798bb1046b8fc3275b5a4801cb6f46e4/raw/6e68dbca121276af44ff540c4b6e26df42ee6832/packages.config'
@steviecoaster
steviecoaster / New-RandomPass.ps1
Last active December 10, 2024 23:31
Cross platform password generator
function New-RandomPass {
<#
.Synopsis
Generates and returns a suitably secure password
.EXAMPLE
New-RandomPass
Returns a random password as a SecureString object
.EXAMPLE
@steviecoaster
steviecoaster / Copy-CertToStore.ps1
Last active December 6, 2024 17:12
Copy a x509 certificate from one windows store to another
function Copy-CertToStore {
<#
.SYNOPSIS
Copy a certificate from one store location to another
.DESCRIPTION
Long description
.PARAMETER Certificate
The certificate to copy
@steviecoaster
steviecoaster / dogs.ps1
Created December 2, 2024 18:08
Evaluate breed in a collection
$breeds = @('Labrador','Poodle','St. Bernard','Husky')
'mut' -in $breeds # false
'Poodle' -in $breeds #true
@steviecoaster
steviecoaster / Explaination.md
Last active October 9, 2025 18:40
Use a module from another machine with Implicit remoting

Implicit Remoting explained

Implicit remoting allows you to load a module from a remote session and execute it "locally". What you are actually doing is running a proxy command. The actual function executes on the remote session, and the data is returned to you locally.

This approach does have the same caveats as running a command directly via Invoke-Command in that it is deserialized and rehydrated, losing any available methods on the object.

This is still a hugely useful trick, and I highly recommend using a prefix so you know in your code when something is running on the remote system. For example Get-ADComputer may become Get-RemoteADComputer. You can specify whatever prefix you want, just make it