ã¯ããã«ïŒpwshã£ãŠäœïŒ ð€
pwshã¯ãPowerShellã®å®è¡å¯èœãã¡ã€ã«åã§ããPowerShellã¯ãMicrosoftã«ãã£ãŠéçºãããã匷åãªã³ãã³ãã©ã€ã³ã·ã§ã«ã§ãããã¹ã¯ãªããèšèªã§ããããŸããããšããšã¯Windowsåãã«éçºãããŸããããPowerShell 6.0以é (æ§ç§°: PowerShell Core) ã¯ãªãŒãã³ãœãŒã¹åãããWindowsã ãã§ãªããmacOSãLinuxã§ãåäœããã¯ãã¹ãã©ãããã©ãŒã ãªããŒã«ãžãšé²åããŸããã
åŸæ¥ã®Windowsã«æšæºæèŒãããŠãããWindows PowerShellã(å®è¡ãã¡ã€ã«å: `powershell.exe`) ãšåºå¥ãããããã¯ãã¹ãã©ãããã©ãŒã çã®å®è¡ãã¡ã€ã«å㯠`pwsh.exe` (Windows) ãŸã㯠`pwsh` (macOS/Linux) ãšãªã£ãŠããŸãã
pwsh (PowerShell) ã®äž»ãªç¹åŸŽã¯ä»¥äžã®éãã§ãã
- ã¯ãã¹ãã©ãããã©ãŒã : Windows, macOS, Linux ã§åäœããŸãã
- ãªããžã§ã¯ãæå: ã³ãã³ãã®åºåãããã¹ãã§ã¯ãªããæ§é åããããªããžã§ã¯ãã§ããããããã€ãã©ã€ã³ (`|`) ã§ããŒã¿ãæž¡ãããããæè»ãªåŠçãå¯èœã§ãã
- .NETããŒã¹: ææ°çã¯.NETãã©ãããã©ãŒã äžã§åäœããè±å¯ãªæ©èœãå©çšã§ããŸãã
- ã³ãã³ãã¬ãã (Cmdlet): ãåè©-åè©ãåœ¢åŒ (äŸ: `Get-Process`, `Set-Location`) ã®åãããããã³ãã³ãäœç³»ãæã£ãŠããŸãã
- ã¹ã¯ãªããã£ã³ã°: é«åºŠãªã¹ã¯ãªãããäœæããã·ã¹ãã 管çãå®åäœæ¥ã®èªååãå®çŸã§ããŸãã
- ãªãŒãã³ãœãŒã¹: GitHubã§éçºãé²ããããŠãããã³ãã¥ããã£ã«ããè²¢ç®ã掻çºã§ãã
pwshã®ã€ã³ã¹ããŒã« ð»
pwsh (PowerShell 7以é) ã¯ãWindowsã«æšæºã§ã¯æèŒãããŠããªããããå¥éã€ã³ã¹ããŒã«ãå¿ èŠã§ãïŒWindows PowerShell 5.1ã¯æšæºæèŒïŒãmacOSãLinuxã§ãå©çšããã«ã¯ã€ã³ã¹ããŒã«ãå¿ èŠã§ãã
Windowsãžã®ã€ã³ã¹ããŒã«
ããã€ãã®æ¹æ³ããããŸãã
-
Winget (æšå¥š): Windows 10/11 ã®ããã±ãŒãžãããŒãžã£ãŒ Winget ã䜿ãã®ãç°¡åã§ãã管çè
æš©éã§ã¿ãŒããã«ãéãã以äžã®ã³ãã³ããå®è¡ããŸãã
winget search Microsoft.PowerShell winget install --id Microsoft.Powershell --source winget
- MSIã€ã³ã¹ããŒã©ãŒ: Microsoftã®å ¬åŒããã¥ã¡ã³ããGitHubãªãªãŒã¹ããŒãžããMSIãã¡ã€ã«ãããŠã³ããŒãããŠã€ã³ã¹ããŒã«ããŸããGUIã§ãªãã·ã§ã³ãéžæããªããã€ã³ã¹ããŒã«ã§ããŸãã
- Microsoft Store: Microsoft Storeãããã€ã³ã¹ããŒã«å¯èœã§ãããäžéšå¶éãããå ŽåããããŸããæ軜ã«è©Šãããå Žåã«äŸ¿å©ã§ãã
- ZIPããã±ãŒãž: ç¹å®ã®ãã©ã«ãã«å±éããã ãã§å©çšã§ããŸããããã¹èšå®ãªã©ãæåã§è¡ãå¿ èŠããããŸãã
-
.NET ã°ããŒãã«ããŒã«: .NET SDKãã€ã³ã¹ããŒã«ãããŠããã°ã以äžã®ã³ãã³ãã§ã€ã³ã¹ããŒã«ã§ããŸãã
dotnet tool install --global PowerShell
macOSãžã®ã€ã³ã¹ããŒã« ð
Homebrewã䜿ãã®ãäžè¬çã§ãã
# Homebrew ãæªã€ã³ã¹ããŒã«ã®å Žå
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# PowerShell ã®ã€ã³ã¹ããŒã«
brew install --cask powershell
ã€ã³ã¹ããŒã«åŸãã¿ãŒããã«ã§ `pwsh` ãšå ¥åããŠèµ·åã§ããŸãã
Linuxãžã®ã€ã³ã¹ããŒã« ð§
ãã£ã¹ããªãã¥ãŒã·ã§ã³ã«ãã£ãŠæ¹æ³ãç°ãªããŸããå€ãã®å Žåãããã±ãŒãžãããŒãžã£ãŒ (apt, yumãªã©) ãå©çšã§ããŸãã
Ubuntu/Debianç³»ã®äŸ (apt):
# Microsoftãªããžããªã®ããŒãšãœãŒã¹ãè¿œå (ååã®ã¿)
sudo apt-get update
sudo apt-get install -y wget apt-transport-https software-properties-common
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update
# PowerShell ã®ã€ã³ã¹ããŒã«
sudo apt-get install -y powershell
CentOS/RHELç³»ã®äŸ (yum/dnf):
# Microsoftãªããžããªãè¿œå (ååã®ã¿)
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl=https://packages.microsoft.com/rhel/7/prod/\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/microsoft.repo' # RHEL 7 ã®äŸãããŒãžã§ã³ã«åãããŠå€æŽ
sudo yum update # ãŸã㯠dnf update
# PowerShell ã®ã€ã³ã¹ããŒã«
sudo yum install -y powershell # ãŸã㯠dnf install -y powershell
åãã£ã¹ããªãã¥ãŒã·ã§ã³ã®ææ°ã®ã€ã³ã¹ããŒã«æ¹æ³ã¯å ¬åŒããã¥ã¡ã³ãã確èªããŠãã ããã
åºæ¬çãªäœ¿ãæ¹ âš
pwshãèµ·åããã«ã¯ãã¿ãŒããã« (Windows Terminal, ã³ãã³ãããã³ãã, Terminal.app, etc.) ãéãã`pwsh` ãšå ¥åããŠEnterããŒãæŒããŸãã
ã³ãã³ãã¬ãã (Cmdlet)
PowerShellã®åºæ¬çãªã³ãã³ãã¯ãã³ãã³ãã¬ããããšåŒã°ãããåè©-åè©ãã®åœ¢åŒã«ãªã£ãŠããŸãã
# ãã«ãã衚瀺 (manã³ãã³ãã®ãããªãã®)
Get-Help Get-Process
# å©çšå¯èœãªã³ãã³ããæ€çŽ¢
Get-Command *Process*
# å®è¡äžã®ããã»ã¹äžèŠ§ãååŸ
Get-Process
# çŸåšã®å Žæ (ãã£ã¬ã¯ããª) ãååŸ
Get-Location
# ãã£ã¬ã¯ããªã移å (cd ãšåã)
Set-Location C:\Users
# ãã¡ã€ã«ããã©ã«ãã®äžèŠ§ã衚瀺 (ls ã dir ãšåã)
Get-ChildItem
# ç»é¢ã«æååãåºå
Write-Host "ããã«ã¡ã¯ãPowerShell!" -ForegroundColor Yellow
å€ãã®ã³ãã³ãã¬ããã«ã¯ãšã€ãªã¢ã¹ (ç瞮圢) ãçšæãããŠããŸããäŸãã° `Get-ChildItem` ã®ãšã€ãªã¢ã¹ã¯ `ls`, `dir`, `gci` ãªã©ã§ãããšã€ãªã¢ã¹ã¯ `Get-Alias` ã§ç¢ºèªã§ããŸãããã¹ã¯ãªããå ã§ã¯å¯èªæ§ã®ãããå®å šãªã³ãã³ãã¬ããåã䜿ãããšãæšå¥šãããŸãã
ãã€ãã©ã€ã³ (`|`)
PowerShellã®åŒ·åãªæ©èœã®äžã€ããã€ãã©ã€ã³ã§ããã³ãã³ãã¬ããã®åºåãå¥ã®ã³ãã³ãã¬ããã®å ¥åãšããŠæž¡ãããšãã§ããŸããPowerShellã§ã¯ããã¹ãã§ã¯ãªããªããžã§ã¯ãããã€ãã©ã€ã³ãæµãããããæè»ãªåŠçãå¯èœã§ãã
# ããã»ã¹äžèŠ§ãååŸããCPU䜿çšçãé«ãé ã«ãœãŒãããŠäžäœ5件ã衚瀺
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
# ã«ã¬ã³ããã£ã¬ã¯ããªã®ãã¡ã€ã«äžèŠ§ãããååã ".log" ã§çµãããã®ã ããéžæ
Get-ChildItem | Where-Object {$_.Name -like "*.log"}
# ãµãŒãã¹äžèŠ§ãååŸããå®è¡äžã®ãã®ã ããCSVãã¡ã€ã«ã«åºå
Get-Service | Where-Object {$_.Status -eq "Running"} | Export-Csv -Path ./running_services.csv -NoTypeInformation
å€æ°
å€æ°ã¯ `$` ã§å§ãŸããå€ãæ ŒçŽããããã«äœ¿ããŸãã
$userName = "Taro"
$age = 30
$message = "ç§ã®åå㯠$userName ã§ã幎霢㯠$age æ³ã§ãã"
Write-Host $message
# ã³ãã³ãã®çµæãå€æ°ã«æ ŒçŽ
$processes = Get-Process
Write-Host "çŸåšã$($processes.Count) åã®ããã»ã¹ãå®è¡äžã§ãã"
æŒç®å
æ¯èŒæŒç®åãè«çæŒç®åãªã©ããããŸããå€ã㯠`-` ã§å§ãŸããŸãã
æŒç®å | æå³ | äŸ |
---|---|---|
-eq | çãã (Equal) | `$a -eq 10` |
-ne | çãããªã (Not Equal) | `$name -ne “admin”` |
-gt | ãã倧ãã (Greater Than) | `$count -gt 100` |
-ge | ä»¥äž (Greater than or Equal) | `$score -ge 60` |
-lt | ããå°ãã (Less Than) | `$index -lt 0` |
-le | ä»¥äž (Less than or Equal) | `$value -le 1000` |
-like | ã¯ã€ã«ãã«ãŒãæ¯èŒ | `$filename -like “*.txt”` |
-notlike | ã¯ã€ã«ãã«ãŒãåŠå®æ¯èŒ | `$path -notlike “*System*”` |
-match | æ£èŠè¡šçŸæ¯èŒ | `$email -match “@example\.com$”` |
-notmatch | æ£èŠè¡šçŸåŠå®æ¯èŒ | `$input -notmatch “^\d+$”` |
-contains | ã³ã¬ã¯ã·ã§ã³ã«å«ãŸããã | `$array -contains “apple”` |
-notcontains | ã³ã¬ã¯ã·ã§ã³ã«å«ãŸããªãã | `$list -notcontains “error”` |
-and | è«ç AND | `($age -ge 20) -and ($hasLicense -eq $true)` |
-or | è«ç OR | `($status -eq “Stopped”) -or ($status -eq “Stopping”)` |
-not / ! | è«ç NOT | `-not ($isAdmin)` |
å¶åŸ¡æ§æ
ã¹ã¯ãªããã®æµããå¶åŸ¡ããããã®æ§æã§ãã
# if / elseif / else
$score = 75
if ($score -ge 80) {
Write-Host "åª"
} elseif ($score -ge 60) {
Write-Host "è¯"
} else {
Write-Host "å¯"
}
# foreach (ã³ã¬ã¯ã·ã§ã³ã®ã«ãŒã)
$fruits = "apple", "banana", "cherry"
foreach ($fruit in $fruits) {
Write-Host "I like $fruit"
}
# ForEach-Object (ãã€ãã©ã€ã³ã§ã®ã«ãŒã)
Get-Process | ForEach-Object {
Write-Host "ããã»ã¹å: $($_.Name), ID: $($_.Id)"
}
# for (ã«ãŠã³ã¿å€æ°ã䜿ã£ãã«ãŒã)
for ($i = 0; $i -lt 5; $i++) {
Write-Host "ã«ãŠã³ã: $i"
}
# while (æ¡ä»¶ãçã®éã«ãŒã)
$count = 0
while ($count -lt 3) {
Write-Host "whileã«ãŒã: $count"
$count++
}
# switch (å€ã«ããåå²)
$day = (Get-Date).DayOfWeek
switch ($day) {
"Monday" { Write-Host "æææ¥ã§ã" }
"Tuesday" { Write-Host "ç«ææ¥ã§ã" }
# ... ä»ã®ææ¥ ...
Default { Write-Host "é±æ«ã§ãïŒ" }
}
ã¹ã¯ãªããã£ã³ã° ð
äžé£ã®PowerShellã³ãã³ãããã¡ã€ã«ã«ä¿åããåå©çšå¯èœãªã¹ã¯ãªãããäœæã§ããŸããã¹ã¯ãªãããã¡ã€ã«ã®æ¡åŒµå㯠`.ps1` ã§ãã
ã¹ã¯ãªããã®äœæãšå®è¡
- ããã¹ããšãã£ã¿ (VS Code, ã¡ã¢åž³ãªã©) ãéããPowerShellã³ãã³ããèšè¿°ããŸãã
- ãã¡ã€ã«ã `.ps1` æ¡åŒµåã§ä¿åããŸã (äŸ: `MyScript.ps1`)ãæåã³ãŒãã¯UTF-8 (BOMä»ããŸãã¯BOMãªã) ãæšå¥šãããŸãã
-
PowerShellã³ã³ãœãŒã«ãéããã¹ã¯ãªãããã¡ã€ã«ãå®è¡ããŸããã»ãã¥ãªãã£ã®ãããããã©ã«ãã§ã¯ã¹ã¯ãªããã®å®è¡ãå¶éãããŠããå ŽåããããŸãã
# ã¹ã¯ãªããã®å®è¡ (ã«ã¬ã³ããã£ã¬ã¯ããªã«ããå Žå) .\MyScript.ps1 # ãã«ãã¹ã§æå®ããŠå®è¡ C:\Scripts\MyScript.ps1
å®è¡ããªã·ãŒ (Execution Policy) â ïž
PowerShellã«ã¯ãæªæã®ããã¹ã¯ãªããããã·ã¹ãã ãä¿è·ããããã®å®è¡ããªã·ãŒããããŸããããã©ã«ãã§ã¯ `Restricted` (ã¹ã¯ãªããå®è¡äžå¯) ãŸã㯠`RemoteSigned` (ããŒã«ã«ã®ã¹ã¯ãªããã¯å®è¡å¯ãããŠã³ããŒãããã¹ã¯ãªããã¯çœ²åãå¿ èŠ) ã«ãªã£ãŠããããšãå€ãã§ãã
# çŸåšã®å®è¡ããªã·ãŒã確èª
Get-ExecutionPolicy
# å®è¡ããªã·ãŒãå€æŽ (管çè
æš©éãå¿
èŠ)
# RemoteSigned: ããŒã«ã«ã¹ã¯ãªããã¯èš±å¯ããããããã®ã¯çœ²åå¿
èŠ
Set-ExecutionPolicy RemoteSigned
# AllSigned: 眲åãããã¹ã¯ãªããã®ã¿èš±å¯
# Set-ExecutionPolicy AllSigned
# Unrestricted: å
šãŠã®ã¹ã¯ãªãããèš±å¯ (éæšå¥š)
# Set-ExecutionPolicy Unrestricted
# Bypass: äœããããã¯ããèŠåã衚瀺ããªã (äžæçãªå®è¡ã«)
# Set-ExecutionPolicy Bypass -Scope Process # çŸåšã®ããã»ã¹ã®ã¿äžæçã«å€æŽ
# ããã©ã«ãã«æ»ã
# Set-ExecutionPolicy Default
å®è¡ããªã·ãŒã®å€æŽã¯ã·ã¹ãã ã®ã»ãã¥ãªãã£ã«åœ±é¿ããŸããå®æã« `Unrestricted` ã«èšå®ããããªã¹ã¯ãç解ããäžã§é©åãªããªã·ãŒãéžæããŠãã ããã`RemoteSigned` ãäžè¬çã«æšå¥šãããŸãã
é¢æ° (Function)
åŠçããŸãšããŠåå©çšå¯èœãªé¢æ°ãäœæã§ããŸãã
function Show-Greeting {
param(
[Parameter(Mandatory=$true)]
[string]$Name,
[string]$Greeting = "ããã«ã¡ã¯"
)
Write-Host "$Greeting, $Name ãã!"
}
# é¢æ°ã®åŒã³åºã
Show-Greeting -Name "äœè€"
Show-Greeting -Name "Suzuki" -Greeting "Hello"
ãšã©ãŒãã³ããªã³ã° (Try/Catch/Finally)
ãšã©ãŒãçºçããå¯èœæ§ã®ããåŠçãå²ã¿ãé©åã«åŠçããŸãã
try {
# ãšã©ãŒãçºçããå¯èœæ§ã®ããåŠç
$content = Get-Content -Path C:\non_existent_file.txt -ErrorAction Stop
Write-Host "ãã¡ã€ã«ã®å
容: $content"
} catch [System.Management.Automation.ItemNotFoundException] {
Write-Warning "æå®ããããã¡ã€ã«ãèŠã€ãããŸããã§ãã: $($_.Exception.Message)"
} catch {
# ãã®ä»ã®ãšã©ãŒ
Write-Error "äºæãã¬ãšã©ãŒãçºçããŸãã: $($_.Exception.Message)"
} finally {
# ãšã©ãŒã®æç¡ã«é¢ãããåžžã«å®è¡ãããåŠç
Write-Host "åŠçãçµäºããŸãã"
}
`-ErrorAction Stop` ãã€ãããšãéåžžã¯åŠçãç¶è¡ãããšã©ãŒ (Non-Terminating Error) ã§ãã¹ã¯ãªãããåæ¢ããã`catch` ãããã¯ã§ææã§ããããã«ãªããŸãã
ã¢ãžã¥ãŒã« ð§©
ã¢ãžã¥ãŒã«ã¯ãé¢é£ããã³ãã³ãã¬ãããé¢æ°ãå€æ°ãªã©ããŸãšããããã±ãŒãžã§ããã¢ãžã¥ãŒã«ãå©çšããããšã§ãPowerShellã®æ©èœãæ¡åŒµã§ããŸããå€ãã®è£œåããµãŒãã¹ (Azure, Microsoft 365, Active Directoryãªã©) ãã管ççšã®PowerShellã¢ãžã¥ãŒã«ãæäŸããŠããŸãã
ã¢ãžã¥ãŒã«ã®æ€çŽ¢ãšã€ã³ã¹ããŒã«
PowerShell Gallery ãšããå ¬åŒãªããžããªããã¢ãžã¥ãŒã«ãæ€çŽ¢ããã€ã³ã¹ããŒã«ã§ããŸãã
# ã¢ãžã¥ãŒã«ãæ€çŽ¢ (äŸ: Azureé¢é£)
Find-Module -Name Az.*
# ç¹å®ã®ã¢ãžã¥ãŒã«ãã€ã³ã¹ããŒã« (管çè
æš©éãå¿
èŠãªå Žåãã)
# ååå®è¡æã«ã¯ NuGet ãããã€ããŒã®ã€ã³ã¹ããŒã«ããªããžããªã®ä¿¡é Œãæ±ããããããšããããŸã
Install-Module -Name Az -Scope CurrentUser # çŸåšã®ãŠãŒã¶ãŒã®ã¿ã«ã€ã³ã¹ããŒã« (æšå¥š)
# Install-Module -Name PSWindowsUpdate # å
šãŠãŒã¶ãŒåãã«ã€ã³ã¹ããŒã« (管çè
æš©éãå¿
èŠ)
# ã€ã³ã¹ããŒã«æžã¿ã®ã¢ãžã¥ãŒã«äžèŠ§ã衚瀺
Get-InstalledModule
ã¢ãžã¥ãŒã«ã®ã€ã³ããŒããšäœ¿çš
PowerShell 3.0以éã§ã¯ãã¢ãžã¥ãŒã«å ã®ã³ãã³ããåããŠäœ¿çšããéã«èªåçã«ã€ã³ããŒããããæ©èœ (Module Auto-Loading) ããããŸãããæ瀺çã«ã€ã³ããŒãããããšãå¯èœã§ãã
# ã¢ãžã¥ãŒã«ãæ瀺çã«ã€ã³ããŒã
Import-Module -Name Az
# ã¢ãžã¥ãŒã«å
ã®ã³ãã³ãã䜿çš
Get-AzVM # (Azã¢ãžã¥ãŒã«ãã€ã³ããŒããããŠããå¿
èŠããã)
# ç¹å®ã®ã¢ãžã¥ãŒã«ã«å«ãŸããã³ãã³ãã確èª
Get-Command -Module Az
# ã»ãã·ã§ã³ããã¢ãžã¥ãŒã«ãåé€
Remove-Module -Name Az
ã¢ãžã¥ãŒã«ã®æŽæ°ãšã¢ã³ã€ã³ã¹ããŒã«
# ã€ã³ã¹ããŒã«æžã¿ã¢ãžã¥ãŒã«ã®æŽæ°ã確èª
Get-InstalledModule | Update-Module -WhatIf
# ã¢ãžã¥ãŒã«ãæŽæ° (管çè
æš©éãå¿
èŠãªå Žåãã)
Update-Module -Name Az
# ã¢ãžã¥ãŒã«ãã¢ã³ã€ã³ã¹ããŒã« (管çè
æš©éãå¿
èŠãªå Žåãã)
Uninstall-Module -Name Az
èªäœã¢ãžã¥ãŒã«
èªåã§äœæããé¢æ°ãªã©ã `.psm1` ãã¡ã€ã«ã«ãŸãšããç¹å®ã®ãã©ã«ãæ§é ã«é 眮ããããšã§ãèªäœã¢ãžã¥ãŒã«ãšããŠå©çšããããšãã§ããŸãã詳现ã¯å ¬åŒããã¥ã¡ã³ããåç §ããŠãã ããã
pwsh vs Windows PowerShell ð¥
`pwsh` (PowerShell 7+) ãš `powershell.exe` (Windows PowerShell 5.1) ã®äž»ãªéãããŸãšããŸãã
é ç® | pwsh (PowerShell 7+) | Windows PowerShell 5.1 |
---|---|---|
å®è¡ãã¡ã€ã«å | `pwsh.exe` / `pwsh` | `powershell.exe` |
ãã©ãããã©ãŒã | Windows, macOS, Linux | Windowsã®ã¿ |
ããŒã¹.NET | .NET (Core) 6, 7, 8… | .NET Framework 4.x |
éçºç¶æ³ | 掻çºã«éçºäž (ãªãŒãã³ãœãŒã¹) | éçºçµäº (ã»ãã¥ãªãã£ä¿®æ£ã®ã¿) |
ããã©ã«ãæèŒ | ãªã (å¥éã€ã³ã¹ããŒã«) | Windows 7/Server 2008 R2 以éã«æšæºæèŒ |
äºææ§ | é«ãäºææ§ãæã€ããäžéšéäºæããã.NET FrameworkäŸåã®å€ãã¢ãžã¥ãŒã«ã¯åããªãå Žåããã (`WindowsCompatibility` ã¢ãžã¥ãŒã«ã§äžéšå¯Ÿå¿å¯)ã | åŸæ¥ã®Windowsåãæ©èœãšã®äºææ§ãé«ãã |
äž»ãªæ°æ©èœ | ããã©ãŒãã³ã¹åäžãæ°ããæŒç®åãã³ãã³ãã¬ãããã¯ãã¹ãã©ãããã©ãŒã 察å¿ãSSHçµç±ã®ãªã¢ãŒãåŠçæ¹åãªã© | å®å®ããŠãããæ°æ©èœè¿œå ã¯ãªã |
PowerShell ISE | ãµããŒãå¯Ÿè±¡å€ (VS Code + PowerShellæ¡åŒµæ©èœæšå¥š) | æšæºæèŒ (éçºçµäº) |
æ°ããæ©èœãããã©ãŒãã³ã¹ãã¯ãã¹ãã©ãããã©ãŒã 察å¿ãå¿ èŠãªå Žå㯠`pwsh` (PowerShell 7+) ãç©æ¥µçã«å©çšããŸããããç¹ã«çç±ããªãéããææ°ã®LTS (é·æãµããŒã) ããŒãžã§ã³ã䜿ãã®ãããããã§ãã
ãã ããå€ãWindowsç°å¢ãã.NET Frameworkã«åŒ·ãäŸåããæ¢åã®ã¹ã¯ãªããã»ããŒã«ããã®ãŸãŸåããå¿ èŠãããå Žåã¯ãWindows PowerShell 5.1 ãå¿ èŠã«ãªãå Žé¢ããããŸããäž¡è ã¯å ±åã§ãããããå¿ èŠã«å¿ããŠäœ¿ãåããããšãå¯èœã§ãã
ãã¹ããã©ã¯ãã£ã¹ â
ããè¯ãPowerShellã¹ã¯ãªãããæžãããã®ãã³ãã§ãã
- ã³ã¡ã³ãã掻çšãã: `#` ã§å§ãŸãã³ã¡ã³ããé©åã«è¿œå ããã³ãŒãã®æå³ãæ確ã«ããŸããè€éãªããžãã¯ãåææ¡ä»¶ã説æããŸãããã
- åœåèŠåãå®ã: å€æ°åãé¢æ°åã¯ãåŠçå 容ããããå ·äœçã§äžè²«æ§ã®ããååãã€ããŸã (äŸ: `$targetComputer`, `Get-SystemInfo`)ãPascalCaseãcamelCaseãäžè¬çã§ãã
- ãšã€ãªã¢ã¹ãé¿ãã: ã¹ã¯ãªããå ã§ã¯ã`ls` ã `cd` ã®ãããªãšã€ãªã¢ã¹ã§ã¯ãªãã`Get-ChildItem` ã `Set-Location` ã®ãããªå®å šãªã³ãã³ãã¬ããåã䜿çšããå¯èªæ§ãé«ããŸãã
- ãšã©ãŒãã³ããªã³ã°ãå®è£ ãã: `try/catch/finally` ã䜿çšããŠãäºæãã¬ãšã©ãŒã§ã¹ã¯ãªãããåæ¢ããªãããã«ããåé¡çºçæã®åå 究æã容æã«ããŸãã
- ã¢ãžã¥ãŒã«åãæèãã: ç¹°ãè¿ã䜿ãåŠçã¯é¢æ°åããé¢é£ããé¢æ°ã¯ã¢ãžã¥ãŒã« (.psm1) ã«ãŸãšãããšãåå©çšæ§ãä¿å®æ§ãåäžããŸãã
- ããŒãã³ãŒãã£ã³ã°ãé¿ãã: ãã¡ã€ã«ãã¹ããµãŒããŒåãèªèšŒæ å ±ãªã©ãã¹ã¯ãªããå ã«çŽæ¥æžã蟌ãŸãããã©ã¡ãŒã¿ãèšå®ãã¡ã€ã«ããèªã¿èŸŒãããã«ããŸãã
- åªçæ§ (Idempotency) ãèæ ®ãã: ã¹ã¯ãªãããäœåºŠå®è¡ããŠãåãçµæã«ãªãããã«èšèšããŸããäŸãã°ããã¡ã€ã«ãäœæããåã«ååšç¢ºèªãè¡ããªã©ã§ãã
- 詳现åºåãšãããã°åºåã掻çšãã: `Write-Verbose` ã `Write-Debug` ãã¹ã¯ãªããå ã«åã蟌ã¿ã`-Verbose` ã `-Debug` ã¹ã€ãããä»ããŠå®è¡ããããšã§ãåäœç¶æ³ã確èªããããããŸãã`Write-Host` ã¯å¯Ÿè©±çãªè¡šç€ºã«ã¯äŸ¿å©ã§ãããæ¬çªã¹ã¯ãªããã®ãã°åºåã«ã¯åããŸããã
- ãã©ã¡ãŒã¿æ€èšŒãè¡ã: é¢æ°ã®ãã©ã¡ãŒã¿ã«ã¯ `[Parameter(Mandatory=$true)]` ã `[ValidateSet(“Option1”, “Option2”)]`, `[ValidateRange(0, 100)]` ãªã©ã®å±æ§ãè¿œå ããäžæ£ãªå ¥åå€ãæ©æã«æ€åºããŸãã
- #Requires ã¹ããŒãã¡ã³ãã䜿çšãã: ã¹ã¯ãªããã®å é 㧠`#Requires -Version 7.2` ã `#Requires -Modules Az` ã®ããã«ãå¿ èŠãªPowerShellã®ããŒãžã§ã³ãã¢ãžã¥ãŒã«ãæèšããŸãã
ã³ãŒããã©ãŒãããã®ãã³ã
- ã€ã³ãã³ãã«ã¯ã¹ããŒã¹4ã€ã䜿ãã®ãäžè¬çã§ãã
- 1è¡ã®é·ããé©åã«ä¿ã¡ãèªã¿ããããç¶æããŸã (äŸãã°80ïœ120æåçšåºŠ)ã
- æŒç®åã®ååŸãã«ã³ãã®åŸã«ã¯ã¹ããŒã¹ãå ¥ããŸãã
- æ¬åŒ§ `{}` ã®ã¹ã¿ã€ã«ãäžè²«ãããŸã (äŸ: One True Brace Style)ã
- Visual Studio Code ãªã©ã®ãšãã£ã¿ãš PowerShellæ¡åŒµæ©èœ ã䜿ããšããã©ãŒããããæ§æãã§ãã¯ãèªåã§è¡ãã䟿å©ã§ãã
ãŸãšã ð
pwsh (PowerShell) ã¯ãWindowsã ãã§ãªãmacOSãLinuxã§ãå©çšã§ãããéåžžã«åŒ·åã§æè»ãªã³ãã³ãã©ã€ã³ã·ã§ã«ããã³ã¹ã¯ãªããèšèªã§ãããªããžã§ã¯ãæåã®ãã€ãã©ã€ã³ãè±å¯ãªã³ãã³ãã¬ããã.NETã®æ©èœãå©çšã§ããç¹ã倧ããªç¹åŸŽã§ãã
åºæ¬çãªã³ãã³ãæäœãããè€éãªã·ã¹ãã 管çã¿ã¹ã¯ã®èªååãŸã§ãå¹ åºãçšéã«æŽ»çšã§ããŸãããã²ç©æ¥µçã« `pwsh` ãã€ã³ã¹ããŒã«ããæ¥ã ã®äœæ¥å¹çåã«åœ¹ç«ãŠãŠã¿ãŠãã ããïŒ
åèæ å ±
- PowerShell ããã¥ã¡ã³ã | Microsoft Learn: å ¬åŒã®å æ¬çãªããã¥ã¡ã³ãã
- PowerShell/PowerShell – GitHub: PowerShellã®ãœãŒã¹ã³ãŒããªããžããªããªãªãŒã¹æ å ±ãIssueããã¡ãã
- Windows PowerShell 5.1 ãš PowerShell 7.x ã®éã | Microsoft Learn: ããŒãžã§ã³éã®å·®ç°ã«é¢ãã詳现ã
ã³ã¡ã³ã