Skip to content

Instantly share code, notes, and snippets.

View SteveL-MSFT's full-sized avatar

Steve Lee SteveL-MSFT

View GitHub Profile
@SteveL-MSFT
SteveL-MSFT / base64.ps1
Last active October 14, 2022 14:28
PowerShell Base64 Encode/Decode test
# Base64 algorithm borrowed from https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64
function ConvertTo-Base64String ($string)
{
$base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
$result = [System.Text.StringBuilder]::new()
$pad = ""
$count = $string.Length % 3;
# string needs to be multiple of 3 characters
@SteveL-MSFT
SteveL-MSFT / Update-ErrorFormat.ps1
Last active November 30, 2018 13:52
ErrorRecord Format
# Define the PS code to embed in the XML below here.
$sb = {
# Make sure we control the specific strict mode in effect.
# (The strict-mode setting is inherited from the parent scope.)
Set-StrictMode -Version 1
# Save the error record at hand as well as its invocation info.
$err = $_
$myInv = $err.InvocationInfo
@SteveL-MSFT
SteveL-MSFT / demo_os_agnostic.ps1
Last active February 23, 2019 17:59
Demos of writing OS agnostic PowerShell Core scripts
# PowerShell remoting over SSH
## Configuring Windows
### Install SSHD as Feature on Demand
### Modify sshd_config for powershell subsystem
## SSH remoting
ssh –l user@domain computer
@SteveL-MSFT
SteveL-MSFT / demo_winps_comparison.ps1
Last active October 17, 2018 10:32
Demos comparing Windows PowerShell and PowerShell Core 6
# Side-by-side
## Use PSCore6 w/ elevation
Invoke-WebRequest https://github.com/PowerShell/PowerShell/releases/download/v6.1.0/PowerShell-6.1.0-win-x64.zip -OutFile pwsh61.zip
Unblock-File pwsh61.zip
Expand-Archive pwsh61.zip
## PowerShell.exe vs Pwsh.exe
@SteveL-MSFT
SteveL-MSFT / demo_windows_compat.ps1
Last active October 17, 2018 10:48
Demos for Windows PowerShell compatibility with PowerShell Core 6
# Single script for Windows PowerShell and PowerShell Core
## Use PSEdition
@'
if ($PSVersionTable.PSEdition -eq "Core")
{ "PowerShell Core!" }
else
{ "Windows PowerShell!" }
'@ > both.ps1
@SteveL-MSFT
SteveL-MSFT / start-netprocess.ps1
Created December 10, 2018 18:51
Create Process with NetCredentials only
param($commandLine, [pscredential]$credential)
$csharp = @'
using System;
using System.Runtime.InteropServices;
public class Advapi32
{
[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
public static extern bool CreateProcessWithLogonW(
@SteveL-MSFT
SteveL-MSFT / install-pscore-preview.ps1
Created December 12, 2018 20:16
Install preview builds of PSCore6
iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -Preview"
@SteveL-MSFT
SteveL-MSFT / Update-Code.ps1
Last active September 2, 2021 02:41
Insert space after closing space in C# code
# Usage example for one commit per directory:
# dir -Recurse -Path . -Directory | % { $dir = $_; dir -path $dir.fullname *.cs | % { ~/test/update-code.ps1 -path $_.fullname; git add $_.fullname }; git commit -m "Update $($dir.name)" }
param($path)
$src = Get-Content $path -Raw
$skip = " get; set; "
# this matches a closing brace that is not followed by:
# catch, else, finally, another closing brace, newline, space, hash, slash (comment), * (comment)
@SteveL-MSFT
SteveL-MSFT / ModuleClass.ps1
Created January 26, 2019 02:32
Access class inside script module
@"
class foo
{
[string] static bar([string] $text)
{
return $text
}
}
"@ > class.psm1
@SteveL-MSFT
SteveL-MSFT / profile.ps1
Last active March 11, 2024 01:21
PowerShell Prompt
#Requires -Version 7
# Version 1.2.13
# check if newer version
$gistUrl = "https://api.github.com/gists/a208d2bd924691bae7ec7904cab0bd8e"
$latestVersionFile = [System.IO.Path]::Combine("$HOME",'.latest_profile_version')
$versionRegEx = "# Version (?<version>\d+\.\d+\.\d+)"
if ([System.IO.File]::Exists($latestVersionFile)) {