<style>
.reveal .slides {
text-align: left;
}
.miku_center {
text-align: center;
}
</style>
# <div class=miku_center>初見PowerShell</div>
<div style="text-align: right">
##### Mikucat

</div>
[TOC]
---
## Why PowerShell
1. 使用 Cmdlet 而非可執行檔
1. 命令結合物件導向
2. 跨平台開源
4. 取代命令提示字元的自動化腳本
---
## 1. Cmdlet it go
----
#### Cmdlet
念作 command-let
PowerShell 裡的指令就是它
----
#### Cmdlet 特性
- Cmdlet 是 .NET 類別的實例,不是可執行檔
- 參數解析、錯誤處理、輸出格式等等<br>PowerShell 會解析好
- 使用 "動詞+名詞" 命名 Cmdlet
---
## 2. 披著命令的皮
Cmdlet 本身 -> Object
變數、參數 -> Object
Cmdlet 回傳值 -> Object
管線傳遞 -> Object
----
### 哈哈 是我啦: 物件導向
C#:
```csharp
int[] array = {1,8,999,1000};
array.Where( x => x > 100).Sum();
```
PowerShell:
```powershell
$array = @(1,8,999,1000)
($array | where {$_ -gt 100} | measure -Sum).Sum
```
----
### 調用 C# 方法
```powershell
$client = New-Object System.Net.WebClient
$client.DownloadString('https://chocolatey.org/install.ps1')
```
---
## 3. 我微軟的夢想<br>就是==征服全世界==
----
PowerShell Core 7.0
- 基於 .Net Core
- 相容 Unix 格式路徑與命名
- 支援 macOS Debian RHEL etc.
- Null運算
---
## 4. 只是多了ls的命令提示字元?

----
### PS 2.0 預設啟用的 Cmdlet 數量

----
### 範例: 全自動安裝MinGW + CLion
```powershell
$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
```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](https://docs.microsoft.com/zh-tw/powershell/)
---
## <div class=miku_center>END</div>
{"metaMigratedAt":"2023-06-15T06:33:54.279Z","metaMigratedFrom":"YAML","title":"初見PowerShell","breaks":true,"robots":"noindex, nofollow","slideOptions":"{\"theme\":\"black\",\"progress\":true}","contributors":"[{\"id\":\"d5ff3cff-6013-437e-ac74-58dddd547e5a\",\"add\":6201,\"del\":2303}]"}