Skip to content

Instantly share code, notes, and snippets.

Avatar

Steve Lee SteveL-MSFT

View GitHub Profile
@SteveL-MSFT
SteveL-MSFT / get-winrtversion.ps1
Created Aug 27, 2021
Example calling WnRT API from PS7 to get Windows version information
View get-winrtversion.ps1
# adapted from https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/identifying-windows-version-part-2
# download and unzip latest https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref and get these dlls from the 'lib' folder
Add-Type -AssemblyName ./winrt.runtime.dll
Add-Type -AssemblyName .\Microsoft.Windows.SDK.NET.dll
# define call and information to query
[Collections.Generic.List[System.String]]$names = 'DeviceFamily',
'OSVersionFull',
'FlightRing',
View Get-KingCountyParcel.ps1
param($address)
$parcelUrl = 'https://gismaps.kingcounty.gov/parcelviewer2/addSearchHandler.ashx?add='
$parcel = Invoke-RestMethod ($parcelUrl + [uri]::EscapeUriString($address))
$id = $parcel.items.pin
$propertyUrl = 'https://gismaps.kingcounty.gov/parcelviewer2/pvinfoquery.ashx?pin='
$property = Invoke-RestMethod ($propertyUrl + $id)
$property.items
View FileContentResource.psm1
[DscResource()]
class FileContentResource {
[DscProperty(Key)]
[string] $filePath
[DscProperty(Mandatory)]
[string] $content
[FileContentResource] Get()
@SteveL-MSFT
SteveL-MSFT / AutoCorrect.ps1
Last active Mar 27, 2021
Example script showing auto-correction with DSC and PS7
View AutoCorrect.ps1
Start-Job {
# path would need to be updated to where you have the FileContentResource.psm1 example saved
if ($env:PSModulePath -notlike "*demo*") {
$env:PSModulePath += [System.IO.Path]::PathSeparator + '/Users/steve/test/demo'
}
# temporarily ensure latest version required for this demo is loaded
Import-Module /Users/steve/test/demo/PSDesiredStateConfiguration -Force
@SteveL-MSFT
SteveL-MSFT / FileContentResource.psm1
Last active Sep 2, 2021
Example DSC class-based resource for file content
View FileContentResource.psm1
# this is an example of what a converted script resource would look like as a class keeping
# the private functions
[DscResource()]
class FileContentResource {
[DscProperty(Key)]
[string] $filePath
[DscProperty(Mandatory)]
@SteveL-MSFT
SteveL-MSFT / Get-Commands.ps1
Created Mar 16, 2021
Find modules/commands used by script
View Get-Commands.ps1
param($file)
$tokens = $null
$err = $null
$file = Resolve-Path $file
$ast = [System.Management.Automation.Language.Parser]::ParseFile($file, [ref]$tokens, [ref]$err)
$commands = $ast.FindAll({$true},$true) | ? { $_ -is [System.Management.Automation.Language.CommandAst] } | % { $_.CommandElements[0].Value } | Sort-Object -Unique
$sources = [System.Collections.Generic.List[string]]::new()
$commands | % {
View F7_PSHistory.ps1
$parameters = @{
Key = 'F7'
BriefDescription = 'ShowMatchingHistoryOcgv'
LongDescription = 'Show Matching History using Out-ConsoleGridView'
ScriptBlock = {
param($key, $arg) # The arguments are ignored in this example
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
$history = [Microsoft.PowerShell.PSConsoleReadLine]::GetHistoryItems().CommandLine | Select-Object -Unique
View FizzBuzz.ps1
1..100|%{$_%15?$_%3?$_%5?$_ :'Buzz':'Fizz':'FizzBuzz'|Write-Host}
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]}}
@SteveL-MSFT
SteveL-MSFT / render.ps1
Last active Sep 2, 2021
Example using System.Commandline.Rendering.dll
View render.ps1
using namespace System.CommandLine.Rendering
add-type -AssemblyName ./System.CommandLine.Rendering.dll
$cs = [ContainerSpan]::new(
[StyleSpan]::UnderlinedOn(),
[ForegroundColorSpan]::Green(),
[BackgroundColorSpan]::Yellow(),
[ContentSpan]::new("hello"),
[StyleSpan]::UnderlinedOff()