Cmdlet ๊ธฐ๋ณธ๊ธฐ, ๊ฐ์ฒด ํ์ดํ๋ผ์ธ(Where/Select/ForEach), Get-Process, Test-NetConnection ํฌํธ ์ง๋จ, REST API(Invoke-RestMethod), JSON/CSV ๊ฐ๊ณต ๋ฐ ์คํ ์ ์ฑ
## 1. PowerShell ๊ธฐ์ด & Cmdlet ๋ฌธ๋ฒ ๊ตฌ์กฐ
PowerShell์ ํ
์คํธ๊ฐ ์๋ **.NET ๊ฐ์ฒด(Object)**๋ฅผ ํ์ดํ๋ผ์ธ(`|`)์ผ๋ก ์ ๋ฌํ๋ ๋ชจ๋ ์์
๋๋ค.
```powershell
# โก [1] ๋ฒ์ ๋ฐ ์คํ ํ๊ฒฝ ํ์ธ
$PSVersionTable.PSVersion # PowerShell ์์ง ๋ฒ์ ํ์ธ (5.1 ๋๋ 7.x)
# ๐ [2] ๋์ ๋ช
๋ น์ด ๊ฒ์ ๋ฐ ๋์๋ง (Cmdlet: Verb-Noun ๋์ฌ-๋ช
์ฌ ๊ตฌ์กฐ)
Get-Command *service* # 'service'๊ฐ ํฌํจ๋ ๋ชจ๋ ๋ช
๋ น์ด ๊ฒ์
Get-Help Get-Process -Online # ์น ๋ธ๋ผ์ฐ์ ์์ ๊ณต์ ์์ธ ๋งค๋ด์ผ ์ด๊ธฐ
Get-Help Get-ChildItem -Examples # ์ค๋ฌด ์ฌ์ฉ ์์ ํ์ธ
# ๐ [3] ๊ฐ์ฒด ์์ฑ ๋ฐ ๋ฉ์๋ ํ์ (๊ฐ์ฅ ์ค์ํ ํ์ต ๋๊ตฌ)
Get-Process | Get-Member # ๋ฐํ๋๋ ๊ฐ์ฒด์ ํ๋กํผํฐ์ ๋ฉ์๋ ๋ชฉ๋ก ํ์ธ
# ๐ท๏ธ [4] ๋ณ์นญ(Alias) ํ์ธ (Linux/CMD ์ฌ์ฉ์ ์นํ)
Get-Alias ls # ls -> Get-ChildItem ํ์ธ
Get-Alias cat # cat -> Get-Content ํ์ธ
```
## 2. ํ์ผ ๋ฐ ๋๋ ํ ๋ฆฌ ์กฐ์
```powershell
# ๐ [1] ํ์ผ/ํด๋ ๋ชฉ๋ก ์กฐํ (Get-ChildItem, ๋ณ์นญ: dir, ls, gci)
Get-ChildItem # ํ์ฌ ๊ฒฝ๋ก ๋ชฉ๋ก
Get-ChildItem -Hidden # ์จ๊น ํ์ผ ํฌํจ
Get-ChildItem -Recurse -Filter *.log # ํ์ ํด๋์ ๋ชจ๋ .log ์ฌ๊ท ๊ฒ์
Get-ChildItem | Sort-Object Length -Descending | Select-Object -First 10 # ์ฉ๋ ์์ 10๊ฐ ํ์ผ
# ๐ [2] ์์ฑ, ๋ณต์ฌ, ์ด๋, ์ญ์
New-Item -ItemType Directory -Path "./logs" -Force # ๋๋ ํ ๋ฆฌ ์์ฑ (mkdir ๊ฐ๋ฅ)
New-Item -ItemType File -Path "./app.env" -Value "PORT=8080" # ํ์ผ ์์ฑ ๋ฐ ๋ด์ฉ ์์ฑ
Copy-Item -Path "./src" -Destination "./dist" -Recurse -Force # ํด๋ ์ฌ๊ท ๋ณต์ฌ
Move-Item -Path "old.txt" -Destination "new.txt" # ์ด๋ ๋ฐ ์ด๋ฆ ๋ณ๊ฒฝ
Remove-Item -Path "./temp" -Recurse -Force # ๊ฐ์ ์ฌ๊ท ์ญ์ (rm -rf ๋์ฒด)
# ๐ [3] ํ์ผ/๊ฒฝ๋ก ์กด์ฌ ์ฌ๋ถ ํ์ธ (True / False ๋ฐํ)
Test-Path "./package.json"
```
## 3. ๊ฐ์ฒด ํ์ดํ๋ผ์ธ (Where / Select / ForEach)
```powershell
# ๐ฏ [1] ์กฐ๊ฑด ํํฐ๋ง (Where-Object, ๋ณ์นญ: ?)
# ๋ฉ๋ชจ๋ฆฌ(WorkingSet)๊ฐ 200MB(200MB ๋จ์ ์ง์) ์ด๊ณผ์ธ ํ๋ก์ธ์ค๋ง ํํฐ
Get-Process | Where-Object { $_.WorkingSet -gt 200MB }
Get-Service | Where-Object Status -eq 'Running'
# ๐ [2] ํ์ํ ์์ฑ๋ง ์ ํ (Select-Object)
Get-Process | Select-Object -Property Id, ProcessName, CPU -First 5
Get-ChildItem | Select-Object -ExpandProperty FullName # ๋ฌธ์์ด ๋ฐฐ์ด๋ก ์ถ์ถ
# ๐ [3] ์ปฌ๋ ์
๋ฐ๋ณต ์ฒ๋ฆฌ (ForEach-Object, ๋ณ์นญ: %)
Get-Process -Name "node" | ForEach-Object { $_.Kill() }
1..5 | ForEach-Object { "Worker thread #$_" }
# ๐ [4] ์ ๋ ฌ, ์ง๊ณ, ๊ทธ๋ฃนํ
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 # CPU ์ ์ ์์ 5๊ฐ
Get-ChildItem | Measure-Object -Property Length -Sum # ํ์ฌ ํด๋ ์ด ํ์ผ ์ฉ๋ ํฉ์ฐ
Get-Service | Group-Object Status # Running / Stopped ์ํ๋ณ ๊ทธ๋ฃน ํต๊ณ
```
## 4. ํ๋ก์ธ์ค & ์๋์ฐ ์๋น์ค ๊ด๋ฆฌ
```powershell
# โ๏ธ [1] ํ๋ก์ธ์ค(Process) ๊ด๋ฆฌ
Get-Process # ์ ์ฒด ํ๋ก์ธ์ค ๋ชฉ๋ก (ps)
Get-Process -Name chrome # ํน์ ์ด๋ฆ์ ํ๋ก์ธ์ค ๊ฒ์
Stop-Process -Name "notepad" -Force # ๊ฐ์ ์ข
๋ฃ (kill)
Stop-Process -Id 1234 -Force # ํน์ PID ๊ฐ์ ์ข
๋ฃ
Start-Process "calc.exe" # ์ ํ๋ก์ธ์ค ์คํ
# ๐ ๏ธ [2] ์๋์ฐ ์๋น์ค(Service) ์ ์ด (๊ด๋ฆฌ์ ๊ถํ ํ์)
Get-Service -Name "wuauserv" # Windows Update ์๋น์ค ์ํ
Start-Service -Name "wuauserv" # ์๋น์ค ์์
Stop-Service -Name "wuauserv" # ์๋น์ค ์ค์ง
Restart-Service -Name "wuauserv" # ์๋น์ค ์ฌ์์
Set-Service -Name "wuauserv" -StartupType Automatic # ์์ ์ ํ ์๋ ์ค์
```
## 5. ๋คํธ์ํฌ ์ง๋จ & REST API ์น ์์ฒญ
```powershell
# ๐ [1] ํฌํธ ์ฐ๊ฒฐ ํ
์คํธ (Telnet / nc ๋์ฒด, ๋๋จํ ์ ์ฉ!)
Test-NetConnection -ComputerName 127.0.0.1 -Port 8080
Test-NetConnection -ComputerName google.com -InformationLevel Detailed
# ๐ [2] ์ด๋ ค์๋ ํฌํธ(Listening) ํ์ธ (netstat ๋์ฒด)
Get-NetTCPConnection -State Listen | Select-Object LocalAddress, LocalPort, OwningProcess
# ๐ [3] DNS ์ด๋ฆ ํ์ธ
Resolve-DnsName api.github.com
# ๐ [4] REST API ํธ์ถ (Invoke-RestMethod - JSON ํ์ฑ ์๋ํ!)
$response = Invoke-RestMethod -Uri "https://api.github.com/users/octocat" -Method Get
$response.name
$response.public_repos
# ๐ก [5] HTTP ํ์ผ ๋ค์ด๋ก๋ (Invoke-WebRequest)
Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "./download.zip"
```
## 6. JSON / CSV ๋ฐ์ดํฐ ๋ณํ & ํ์ผ ์
์ถ๋ ฅ
```powershell
# ๐ [1] JSON ์ง๋ ฌํ & ์ญ์ง๋ ฌํ
# ๊ฐ์ฒด/๋ฐฐ์ด์ JSON ๋ฌธ์์ด๋ก ๋ณํ (์ค์ฒฉ ๊น์ด ๋ณด์กด ์ -Depth ์ง์ )
Get-Process | Select-Object -First 3 | ConvertTo-Json -Depth 3 | Out-File "processes.json"
# JSON ํ์ผ ์ฝ๊ณ PowerShell ๊ฐ์ฒด๋ก ์ญ์ง๋ ฌํ
$config = Get-Content "./config.json" -Raw | ConvertFrom-Json
$config.database.host
# ๐ [2] CSV ํ์ผ ๋ด๋ณด๋ด๊ธฐ & ์ฝ๊ธฐ
Get-Process | Select-Object Id, ProcessName, CPU | Export-Csv -Path "./procs.csv" -NoTypeInformation -Encoding utf8
$csvData = Import-Csv -Path "./procs.csv"
# ๐ [3] ๋ก๊ทธ ํ์ผ ์ค์๊ฐ ๋ชจ๋ํฐ๋ง (Linux tail -f)
Get-Content -Path "./server.log" -Tail 50 -Wait
```
## 7. ๋ณ์, ํ๊ฒฝ๋ณ์ & ๋ฌธ์์ด ์ฒ๋ฆฌ
```powershell
# ๐ฆ [1] ๋ณ์ ์ ์ธ ๋ฐ ์ฐ์ฐ
$name = "Antigravity"
$numbers = 1, 2, 3, 4, 5
$user = @{ Name = "Lucky"; Role = "Developer" } # ํด์ํ
์ด๋ธ (Dictionary)
$user.Role
# ๐ [2] ํ๊ฒฝ๋ณ์ ($env:)
$env:PATH # ์์คํ
PATH ์ถ๋ ฅ
$env:PORT = "3000" # ํ์ฌ ์ธ์
ํ๊ฒฝ๋ณ์ ์์ ์ค์
[System.Environment]::SetEnvironmentVariable("MY_KEY", "value", "User") # ์๊ตฌ ๋ฑ๋ก
# ๐ค [3] ์ ๊ท์ ๋งค์นญ & ์นํ
"Server IP: 192.168.1.100" -match '\d+\.\d+\.\d+\.\d+'
$matches[0] # ์ ๊ท์ ์บก์ฒ ๊ฒฐ๊ณผ (192.168.1.100)
"hello world" -replace "world", "PowerShell"
```
## 8. ์คํ ์ ์ฑ
(ExecutionPolicy), ํ๋กํ & ์์ธ ์ฒ๋ฆฌ
```powershell
# ๐ก๏ธ [1] ์คํฌ๋ฆฝํธ(.ps1) ์คํ ๊ถํ ํด์ (ps1 ํ์ผ ์คํ ์ฐจ๋จ ์ ํ์)
# ๊ด๋ฆฌ์ ๊ถํ ์ฝ์์์ ์คํ
Get-ExecutionPolicy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# ๐ค [2] PowerShell ์์ ํ๋กํ (๋ณ์นญ, ํจ์ ์๋ ๋ก๋)
notepad $PROFILE # ์ฌ์ฉ์ ํ๋กํ ์คํฌ๋ฆฝํธ ํธ์ง
# โ ๏ธ [3] Try / Catch / Finally ์์ธ ์ฒ๋ฆฌ
try {
$result = 10 / 0
} catch [System.DivideByZeroException] {
Write-Warning "0์ผ๋ก ๋๋ ์ ์์ต๋๋ค: $_"
} catch {
Write-Error "์์์น ๋ชปํ ์๋ฌ: $_"
} finally {
Write-Host "์ ๋ฆฌ ์์
์๋ฃ." -ForegroundColor Cyan
}
# ๐ [4] ํด๋ฆฝ๋ณด๋ ์
์ถ๋ ฅ
Get-Process | Set-Clipboard # ํด๋ฆฝ๋ณด๋๋ก ์ ์ก
Get-Clipboard # ํด๋ฆฝ๋ณด๋ ๋ด์ฉ ์ฝ๊ธฐ
```
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์