Skip to content

Instantly share code, notes, and snippets.

View LawrenceHwang's full-sized avatar
☁️

Lawrence Hwang LawrenceHwang

☁️
View GitHub Profile
@LawrenceHwang
LawrenceHwang / GeneratePesterTest.ps1
Last active January 7, 2019 23:17
Generate Pester tests from PSCustomObject's NoteProperty
function GeneratePesterTest
{
# Generate Pester tests from PSCustomObject's NoteProperty
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[PSCustomObject]$InputObject,
#The object name that you are comparing with in your test.
@LawrenceHwang
LawrenceHwang / PowerShell 動詞繁體中文譯.md
Created January 7, 2019 08:21
PowerShell Approved Verbs in Traditional Chinese / PowerShell 動詞, 繁體中文譯
PowerShell 動詞 (PowerShell Approved Verbs) 繁體中文譯 (Meaning in Traditional Chinese)
Add 加入
Clear 清除
Close 關閉
Copy 複製
Enter 輸入
Exit 退出
Find 尋找
Format 格式
@LawrenceHwang
LawrenceHwang / TranslatePowerShellVerb.ps1
Last active January 7, 2019 08:12
Code snippets to get PowerShell verb translations in different language using AWS Translate
# Code snippets to tranlate PowerShell approved verbs into different language using AWS Translate
# This snippet assumes the AWS credential and AWS default regions have been set.
# Also, make sure your console environment supports the target encoding.
$Verb = (Get-Verb).Verb
# Find Language code here:
# https://docs.aws.amazon.com/powershell/latest/reference/items/ConvertTo-TRNTargetLanguage.html
$SourceLanguageCode = 'en'
$TargetLanguageCode = 'zh-TW'
@LawrenceHwang
LawrenceHwang / Get-LifeCalendar.ps1
Created December 18, 2018 18:14
A powershell function to display the life calendar in weeks.
function Get-LifeCalendar {
[CmdletBinding()]
Param(
$StartDate = (Get-Date -Date '1980-01-01'),
$LifeinYear = 75
)
# A powershell function to display the life calendar in weeks.
# Original idea from: https://waitbutwhy.com/2014/05/life-weeks.html
$DaysSinceStartDate = (New-TimeSpan -Start $StartDate -End (Get-Date)).Days
@LawrenceHwang
LawrenceHwang / RenewIP.ps1
Created August 16, 2018 03:58
Renew IP until the node receives a lease with PowerShell
$InterfaceAlias = 'Wi-Fi'
while ((Get-NetIPAddress -InterfaceAlias $InterfaceAlias -AddressFamily IPv4).IPAddress -like '169*')
{
$arg = '/C ipconfig /renew'
Start-Process -FilePath 'cmd.exe' -argumentlist $arg -wait -PassThru
Start-Sleep -Seconds 3
}
@LawrenceHwang
LawrenceHwang / Invoke-BetterWebRequest.ps1
Created July 16, 2018 00:06
Invoke-BetterWebRequest - A fix for TLS 1.2 on Windows PowerShell
function Invoke-BetterWebRequest {
[CmdletBinding()]
param(
# Hashtable for the Invoke-WebRequest
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[hashtable]
$iwrSplat
)
try {
@LawrenceHwang
LawrenceHwang / CompareResult.PS1
Created May 30, 2018 04:47
[PowerShell] Recursion vs Iteration
# This is iteration
9:42 PM> Measure-Command -Expression {FibI 20}
...
TotalSeconds : 0.0093811
TotalMilliseconds : 9.3811
# This is recursion
9:43 PM> Measure-Command -Expression {FibR 20}
...
TotalSeconds : 22.7287975
@LawrenceHwang
LawrenceHwang / FibR.ps1
Last active May 30, 2018 04:56
[PowerShell] Fibonacci number with recursion
# Fibonacci - Recursion
Function FibR {
[CmdletBinding()]
[Parameter(Position = 0)]
param ([int]$index)
Set-StrictMode -Version Latest
if ($index -lt 2) {
@LawrenceHwang
LawrenceHwang / FibI.ps1
Created May 30, 2018 04:15
[PowerShell] Fibonacci number with iteration
# Fibonacci - Interation
Function FibI {
[CmdletBinding()]
[Parameter(Position = 0)]
param ([int]$index)
Set-StrictMode -Version Latest
if ($index -eq 0) {

Credit: Mark Kraus
Website: https://get-powershellblog.blogspot.com

Collection Type Guidence

When to use what

  • Use Arrays if you know the element types and have a fixed length and/or known-up-front collection size that will not change.
  • Use ArrayList if you have an unkown collection size with either unknown or mixed type elements.
  • Use a Generic List when know the type of the elements but not the size of the collection.
  • Use a HashTable if you are going to do key based lookups on a collection and don't know the object type of the elements.
  • Use a Dictionary<TKey, TValue> you are going to do key based lookups on a collection and you know the type of the elements.
  • Use a HashSet when you know the type of elements and just want unique values and quick lookups and assignmnets.