Skip to content

Instantly share code, notes, and snippets.

Steve Lee SteveL-MSFT

View GitHub Profile
@SteveL-MSFT
SteveL-MSFT / profile.ps1
Last active Apr 23, 2019
PowerShell Prompt
View profile.ps1
#Requires -Version 6
# Version 1.0.7
# check if newer version
try {
$gist = Invoke-RestMethod https://api.github.com/gists/a208d2bd924691bae7ec7904cab0bd8e -ErrorAction Stop
$gistProfile = $gist.Files."profile.ps1".Content
$currentProfile = Get-Content $profile -Raw
@SteveL-MSFT
SteveL-MSFT / ModuleClass.ps1
Created Jan 26, 2019
Access class inside script module
View ModuleClass.ps1
@"
class foo
{
[string] static bar([string] $text)
{
return $text
}
}
"@ > class.psm1
@SteveL-MSFT
SteveL-MSFT / Update-Code.ps1
Last active Dec 23, 2018
Insert space after closing space in C# code
View Update-Code.ps1
# 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 / install-pscore-preview.ps1
Created Dec 12, 2018
Install preview builds of PSCore6
View install-pscore-preview.ps1
iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -Preview"
@SteveL-MSFT
SteveL-MSFT / start-netprocess.ps1
Created Dec 10, 2018
Create Process with NetCredentials only
View start-netprocess.ps1
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 / demo_windows_compat.ps1
Last active Oct 17, 2018
Demos for Windows PowerShell compatibility with PowerShell Core 6
View demo_windows_compat.ps1
# 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 / demo_winps_comparison.ps1
Last active Oct 17, 2018
Demos comparing Windows PowerShell and PowerShell Core 6
View demo_winps_comparison.ps1
# 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_os_agnostic.ps1
Last active Feb 23, 2019
Demos of writing OS agnostic PowerShell Core scripts
View demo_os_agnostic.ps1
# 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
View Update-ErrorFormat.ps1
# 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 / base64.ps1
Last active Oct 11, 2018
PowerShell Base64 Encode/Decode test
View base64.ps1
# 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
You can’t perform that action at this time.