Constants.ps1
function Set-Constant {
param (
[string]$Name,
[Object]$Value
)
if (-not (Get-Variable -Name $Name -Scope Global -ErrorAction SilentlyContinue)) {
Set-Variable -Name $Name -Value $Value -Option Constant -Scope Global
}
}
# 定数を定義
Set-Constant -Name "PI" -Value 3.14159
Set-Constant -Name "EULER" -Value 2.71828
Set-Constant -Name "GRAVITY" -Value 9.80665
# 配列を定義
Set-Constant -Name "PRIMES" -Value @(2, 3, 5, 7, 11, 13)
# 列挙型を定義
Add-Type -TypeDefinition @"
public enum Colors {
Red,
Green,
Blue,
Yellow
}
"@
Set-Constant -Name "Colors" -Value [Colors]
MainScript.ps1
# Constants.ps1をインポート
. .\Constants.ps1
# 定数を使用
Write-Host "PIの値は: $PI"
Write-Host "オイラー数の値は: $EULER"
Write-Host "重力加速度の値は: $GRAVITY"
# 配列を使用
Write-Host "素数の配列: $PRIMES"
# 列挙型を使用
Write-Host "色の列挙型: $([Colors]::Red), $([Colors]::Green), $([Colors]::Blue), $([Colors]::Yellow)"