changed 5 years ago
Linked with GitHub

初見PowerShell

Mikucat


Why PowerShell

  1. 使用 Cmdlet 而非可執行檔
  2. 命令結合物件導向
  3. 跨平台開源
  4. 取代命令提示字元的自動化腳本

1. Cmdlet it go


Cmdlet

念作 command-let
PowerShell 裡的指令就是它


Cmdlet 特性

  • Cmdlet 是 .NET 類別的實例,不是可執行檔
  • 參數解析、錯誤處理、輸出格式等等
    PowerShell 會解析好
  • 使用 "動詞+名詞" 命名 Cmdlet

2. 披著命令的皮

Cmdlet 本身 -> Object
變數、參數 -> Object
Cmdlet 回傳值 -> Object
管線傳遞 -> Object


哈哈 是我啦: 物件導向

C#:

int[] array = {1,8,999,1000};
array.Where( x => x > 100).Sum();

PowerShell:

$array = @(1,8,999,1000)
($array | where {$_ -gt 100} | measure -Sum).Sum

調用 C# 方法

$client = New-Object System.Net.WebClient
$client.DownloadString('https://chocolatey.org/install.ps1')

3. 我微軟的夢想
就是征服全世界


PowerShell Core 7.0

  • 基於 .Net Core
  • 相容 Unix 格式路徑與命名
  • 支援 macOS Debian RHEL etc.
  • Null運算

4. 只是多了ls的命令提示字元?


PS 2.0 預設啟用的 Cmdlet 數量


範例: 全自動安裝MinGW + CLion

$MINGW_URL = 'https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z'
$MINGW_ZIP = 'mingw64.7z'

$CLION_URL = 'https://download.jetbrains.com/cpp/CLion-2019.3.5.exe'
$CLION_EXE = 'clion.exe'
$CLION_INSTALL_DIR = 'C:\CLion'
$CLION_CONFIG_URL = 'https://download.jetbrains.com/cpp/silent.config'
$CLION_CONFIG = 'silent.config'


# Change Work Dir
Set-Location $PSScriptRoot

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-Not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    $PSCommandPath = $MyInvocation.PSCommandPath
    Start-Process powershell.exe @("-File $PSCommandPath") -Verb runAs
    exit
}

# Kill TrAsH cHyIoU
Rename-Item "C:\Program Files (x86)\CHIYOU\REDAgent.exe" "BLUEAgent.exe" -Force
taskkill.exe /F /T /IM "REDAgent.exe"
taskkill.exe /F /T /IM "BLUEAgent.exe"

function dl ($url, $file) {
    .\aria2c.exe --dir=$PSScriptRoot --out=$file $url --file-allocation=none
}

# MinGW-w64
dl -url $MINGW_URL -file $MINGW_ZIP
.\7z.exe x -oC:\ $MINGW_ZIP

# Edit System Path
$path = [System.Environment]::GetEnvironmentVariable("Path", "Machine").Split(";") | Where-Object { $_ -notlike "*msys*"}
[System.Environment]::SetEnvironmentVariable("Path", 'C:\mingw64\bin;' + ($path -join ';'), "Machine")

# CLion
dl -url $CLION_URL -file $CLION_EXE
dl -url $CLION_CONFIG_URL -file $CLION_CONFIG
Start-Process $CLION_EXE @("/S", "/CONFIG=$CLION_CONFIG", "/D=$CLION_INSTALL_DIR") -Wait

# Logout
shutdown.exe /l

C# Invoke DLL over Powershell

add-type @"
using System;
using System.Runtime.InteropServices;
 
public static partial class User32 {
   [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern Int32 MessageBox(IntPtr hWnd,String text,String caption,int options);
}
"@
$chosen = [User32]::MessageBox(0, 'Miku is my waifu!' , 'This is a truth', 4)
Write-Output $(if ($chosen -eq 6) {"Yeah!"} else {"You are a heretic! (angry)"})

Reference

PowerShell 文件 - PowerShell | Microsoft Docs


END

Select a repo