facebook twitter hatena line email

Windows/powershell/ini読込

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

サンプル

$CONFIG_DIRPATH = @(Split-Path $script:MyInvocation.MyCommand.path -parent).Trim()
$CONFIG_FILENAME = "hoge.ini"
$CONFIG_PATH = "${CONFIG_DIRPATH}/${CONFIG_FILENAME}"
echo $CONFIG_PATH

$CONFIG = @{}
Get-Content $CONFIG_PATH | %{ $CONFIG += ConvertFrom-StringData $_ }
echo $CONFIG.NAME
echo $CONFIG.AGE

hoge.ini

NAME=taro
AGE=16

参考:https://bags-lab.com/2020/11/01/powershell-environment-ini/

function内でini読み込みを、実行して他でもCONFIG値を使いたい場合

Get-Content $CONFIG_PATH | %{ $CONFIG += ConvertFrom-StringData $_ }

Get-Content $CONFIG_PATH | %{ $global:CONFIG += ConvertFrom-StringData $_ }

sectionを使う場合

参考:https://stackoverflow.com/questions/43690336/powershell-to-read-single-value-from-simple-ini-file