Is production: true

PowerShell

PowerShell, developed by Microsoft, is a task automation and configuration management framework, consisting of a command-line shell and an associated scripting language. Initially a Windows component only, it is now open-source and cross-platform.

The language draws developer’s eye sight due to

  • Object-Oriented Pipeline: Unlike most shells that pipe text, PowerShell pipes .NET objects between cmdlets, allowing for more robust and powerful data manipulation without text parsing.
  • Cmdlets: Administrative tasks are performed by cmdlets, which are specialized .NET classes. Their Verb-Noun naming convention (e.g., Get-Process) makes them easy to understand and use.
  • Cross-Platform: PowerShell runs on Windows, macOS, and Linux, enabling administrators to use a single toolset across different environments.
  • .NET Integration: Built on the .NET Framework (and now .NET Core), PowerShell provides full access to the extensive capabilities of the .NET ecosystem.
  • Remoting: PowerShell has powerful remoting capabilities, allowing scripts and cmdlets to be invoked on remote machines.
  • Desired State Configuration (DSC): DSC is a management platform in PowerShell that enables you to manage your IT and development infrastructure with configuration as code.
  • Providers: PowerShell uses providers to access data stores, such as the registry and certificate store, as if they were file systems.
  • Scripting Language: PowerShell includes a rich scripting language that supports functions, classes, error handling, and other features found in modern programming languages.

PowerShell is a powerful tool for administrators and developers to automate tasks and manage systems at scale.

Web Site

Tools

  • Visual Studio Code with PowerShell Extension
  • Windows Terminal
  • PowerShell ISE (Windows-only)
  • Azure PowerShell

Basic

Open TCP port for debug

On server side, open a Tcp Socket on 8080 port (change the target port you are testing)

1
2
3
4
5
6
7
8
$Listener = [System.Net.Sockets.TcpListener]{target port};
# $Listener = [System.Net.Sockets.TcpListener]8080;
$Listener.Start()
#.
#.
#.
# when stop
$Listener.Stop

On the client side, use tnc command for connection

1
tnc 127.0.0.1 8080

Script basic initializer

$PSScriptRoot refers to the directory containing the script. try/finally restores the original working directory and prints the end time even if the script body fails.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$scriptName = Split-Path -Leaf $PSCommandPath
$timeStart = Get-Date -Format 'yyyy_MM_dd_HH_mm'
Write-Host "Script started at: $timeStart"

Push-Location -LiteralPath $PSScriptRoot
try {
    # Your script below
}
finally {
    try {
        Pop-Location
    }
    finally {
        $timeEnd = Get-Date -Format 'yyyy_MM_dd_HH_mm'
        [Console]::Error.WriteLine("Script ended at: $timeEnd")
    }
}

Preventing sensitive command being logged by history

To work with shell, we normally have some secrete data to store in the environment, add a backtick(`) as prefix could help prevent the message being logged.

1
` set $env:SomeSecret = "secret"