「Windows/powershell/ini読込」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「<pre> $CONFIG_DIRPATH = @(Split-Path $script:MyInvocation.MyCommand.path -parent).Trim() $CONFIG_FILENAME = "hoge.ini" $CONFIG_PATH = "${CONFIG_DIRPATH}/${CONFIG_FILENAME...」) |
|||
(同じ利用者による、間の1版が非表示) | |||
行1: | 行1: | ||
+ | ==サンプル== | ||
<pre> | <pre> | ||
$CONFIG_DIRPATH = @(Split-Path $script:MyInvocation.MyCommand.path -parent).Trim() | $CONFIG_DIRPATH = @(Split-Path $script:MyInvocation.MyCommand.path -parent).Trim() | ||
行18: | 行19: | ||
参考:https://bags-lab.com/2020/11/01/powershell-environment-ini/ | 参考: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 |
2021年8月31日 (火) 15:44時点における最新版
サンプル
$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