Skip to content

Instantly share code, notes, and snippets.

Avatar

Steve Lee SteveL-MSFT

View GitHub Profile
@SteveL-MSFT
SteveL-MSFT / profile.ps1
Last active Dec 2, 2021
PowerShell Prompt
View profile.ps1
#Requires -Version 7
# Version 1.2.10
# 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)) {
@SteveL-MSFT
SteveL-MSFT / graphql.ps1
Last active Nov 2, 2021
GraphQL Query Example
View graphql.ps1
$query = @"
query {
organization(login: "powershell") {
name
url
repository(name: "powershell") {
name
pullRequests(last: 20, states: [OPEN]) {
edges {
node {
@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(
View getargv0.cs
using System;
using System.Runtime.InteropServices;
namespace myps
{
public static class GetProcArgv
{
private const int CTL_KERN = 1;
private const int KERN_ARGMAX = 8;
private const int KERN_PROCARGS2 = 49;
@SteveL-MSFT
SteveL-MSFT / Update-Code.ps1
Last active Sep 2, 2021
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 / 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',
@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 | % {
@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()
@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)]
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