念作 command-let
PowerShell 裡的指令就是它
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
$client = New-Object System.Net.WebClient
$client.DownloadString('https://chocolatey.org/install.ps1')
PowerShell Core 7.0
$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
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)"})