Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Dongbo Wang daxian-dbw

🏠
Working from home
  • Microsoft
  • Redmond, WA
View GitHub Profile
@daxian-dbw
daxian-dbw / ColorTest.ps1
Created Feb 20, 2020
Test script to verify the ConsoleColor to VT sequences map is correct.
View ColorTest.ps1
Write-Host "ConsoleColor.Black:`t" -NoNewline; Write-Host "TESTING" -ForegroundColor Black
"x1b[30m:`t`t`e[30mTESTING`e[0m"
Write-Host "ConsoleColor.Gray:`t" -NoNewline; Write-Host "TESTING" -ForegroundColor Gray
"x1b[37m:`t`t`e[37mTESTING`e[0m"
Write-Host "ConsoleColor.Red:`t" -NoNewline; Write-Host "TESTING" -ForegroundColor Red
"x1b[91m:`t`t`e[91mTESTING`e[0m"
Write-Host "ConsoleColor.Green:`t" -NoNewline; Write-Host "TESTING" -ForegroundColor Green
@daxian-dbw
daxian-dbw / RunspaceSynchronizer.cs
Created Apr 13, 2019
Synchronize the loaded modules and global scope variables between two Runspaces
View RunspaceSynchronizer.cs
using System;
using System.Collections.Generic;
using System.Management.Automation;
public class RunspaceSynchronizer
{
public static bool SourceActionEnabled = false;
private static bool TargetActionEnabled = false;
// 'moduleCache' keeps track of all modules imported in the source Runspace.
View proxy.pac
function FindProxyForURL(url, host) {
if ( host == "www.msn.com" ) {
return "PROXY 10.10.10.10";
}
else {
return "DIRECT";
}
}
@daxian-dbw
daxian-dbw / outter.cs
Last active Oct 31, 2016
Redirect process output/error using event handler. This project experiments with BlockingCollection<T> and the encapsulation of the output/error data handler.
View outter.cs
using System;
// A test process that write stdout and stderr.
namespace ConsoleApp {
public class Outter {
private static string[] outputs = new string[] { "Hello world",
"This is me",
"I'm here",
"You are awesome",
"Me too",
@daxian-dbw
daxian-dbw / Add-LinkElement.ps1
Created Oct 22, 2016
Add <Link> tags to the '<Compile>', '<None>' and '<EmbeddedResource>' for the *.csproj files in PowerShell/PowerShell. This is to make sure in Visual Studio all file items are organized in the appropriate folders.
View Add-LinkElement.ps1
function Add-LinkElement
{
param(
[Parameter(Mandatory=$true)]
[string]$csprojPath,
[Parameter(Mandatory=$true)]
[string]$projectName,
[Parameter(Mandatory=$true)]
@daxian-dbw
daxian-dbw / Consume-Memory.ps1
Last active Oct 30, 2016
A powershell function to consume as much memory as possible
View Consume-Memory.ps1
## A helper to check the memory limit of a PS remote session
function Consume-Memory
{
## Get the total physical memory size
$totalPhysicalMem = get-ciminstance -class "cim_physicalmemory" | Measure-Object -Sum -Property Capacity | % Sum
Write-Host "Total Free Physical Memory: $($totalPhysicalMem / 1gb) GB" -ForegroundColor Green
## Create a two-dimensional array (20, N)
$array = @(,@(1)) * 20
@daxian-dbw
daxian-dbw / AnalyzeSelectObject.ps1
Last active Jul 24, 2020
AnalyzeSelectObject.ps1: analyze usage of 'Select-Object' to find cases like 'Select-Object -ExcludeProperty value1 -ExpandProperty value2'. SearchGitHub.psm1: search powershell code in GitHub
View AnalyzeSelectObject.ps1
using namespace System.Management.Automation.Language
param([string]$Path)
$Path = Resolve-Path $Path | % Path
function AnalyzeSelectObjectUsage
{
param(
[Parameter(ValueFromPipelineByPropertyName)]
@daxian-dbw
daxian-dbw / Check-StringReplacementChange.ps1
Last active Jan 7, 2019
Validate string replacement changes that affect large amount of files
View Check-StringReplacementChange.ps1
##
## Get the diff content of a Github PR, and analyze if the diffs are only caused by the string replacement.
## [Interesting exercise]
##
param(
[Parameter(Mandatory=$true)]
[string]
$PRUrl,