Skip to content

Instantly share code, notes, and snippets.

Avatar

Steve Lee SteveL-MSFT

View GitHub Profile
@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_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
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 / 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 / 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 / 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 perftest.ps1
$num = 50
$ps = @("powershell.exe", "C:\Program Files\PowerShell\6\pwsh.exe", "C:\Program Files\PowerShell\7-preview\pwsh.exe")
$ps | ForEach-Object {
$thisps = $_
$psv = & $thisps -nop -c '$psv = $psversiontable.gitcommitid; if ($psv -eq $null) { $psv = $psversiontable.psversion.tostring() }; $psv'
[int]$i = 0
1..$num | ForEach-Object {
$i += (Measure-Command {
& $thisps -nop -c exit
}).TotalMilliseconds
View wpf.ps1
add-type -AssemblyName presentationframework
$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
Width = "400" Height = "300" ShowInTaskbar = "True">
</Window>
"@
@SteveL-MSFT
SteveL-MSFT / base64.ps1
Last active Apr 30, 2020
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
View 12DaysOfChristmas.ps1
nal w write-host
$d='First','Second','Third','Fourth','Fifth','Sixth','Seventh','Eighth','Ninth','Tenth','Eleventh','Twelfth',"A Partridge in a Pear Tree.`n",'Two Turtle Doves, and','Three French Hens,','Four Calling Birds,','Five Gold Rings,','Six Geese-a-Laying,','Seven Swans-a-Swimming,','Eight Maids-a-Milking,','Nine Ladies Dancing,','Ten Lords-a-Leaping,','Eleven Pipers Piping,','Twelve Drummers Drumming,'
$l="On the % day of Christmas`nMy true love sent to me"
0..11|%{w $l.Replace("%",$d[$_])
$_..0|%{w $d[$_+12]}}