Skip to content

Instantly share code, notes, and snippets.

View daxian-dbw's full-sized avatar
🏠
Working from home

Dongbo Wang daxian-dbw

🏠
Working from home
  • Microsoft
  • Redmond, WA
View GitHub Profile
@daxian-dbw
daxian-dbw / Add-LinkElement.ps1
Created October 22, 2016 05:08
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.
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 October 30, 2016 10:30
A powershell function to consume as much memory as possible
## 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 / outter.cs
Last active October 31, 2016 21:59
Redirect process output/error using event handler. This project experiments with BlockingCollection<T> and the encapsulation of the output/error data handler.
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",
function FindProxyForURL(url, host) {
if ( host == "www.msn.com" ) {
return "PROXY 10.10.10.10";
}
else {
return "DIRECT";
}
}
@daxian-dbw
daxian-dbw / Check-StringReplacementChange.ps1
Last active January 7, 2019 18:04
Validate string replacement changes that affect large amount of files
##
## 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,
@daxian-dbw
daxian-dbw / RunspaceSynchronizer.cs
Created April 13, 2019 22:22
Synchronize the loaded modules and global scope variables between two Runspaces
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.
@daxian-dbw
daxian-dbw / ColorTest.ps1
Created February 20, 2020 19:14
Test script to verify the ConsoleColor to VT sequences map is correct.
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 / AnalyzeSelectObject.ps1
Last active November 11, 2023 19:14
AnalyzeSelectObject.ps1: analyze usage of 'Select-Object' to find cases like 'Select-Object -ExcludeProperty value1 -ExpandProperty value2'. SearchGitHub.psm1: search powershell code in GitHub
using namespace System.Management.Automation.Language
param([string]$Path)
$Path = Resolve-Path $Path | % Path
function AnalyzeSelectObjectUsage
{
param(
[Parameter(ValueFromPipelineByPropertyName)]