PowerShell 動詞 (PowerShell Approved Verbs) | 繁體中文譯 (Meaning in Traditional Chinese) |
---|---|
Add | 加入 |
Clear | 清除 |
Close | 關閉 |
Copy | 複製 |
Enter | 輸入 |
Exit | 退出 |
Find | 尋找 |
Format | 格式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-BetterWebRequest { | |
[CmdletBinding()] | |
param( | |
# Hashtable for the Invoke-WebRequest | |
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)] | |
[hashtable] | |
$iwrSplat | |
) | |
try { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fibonacci - Recursion | |
Function FibR { | |
[CmdletBinding()] | |
[Parameter(Position = 0)] | |
param ([int]$index) | |
Set-StrictMode -Version Latest | |
if ($index -lt 2) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
- 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.