0
Powershellの設定ファイルからメニューをビルドしようとしています。メニューの目的は、他のスクリプトや実行ファイルを呼び出して保守を簡単にすることで、メニュー項目を追加する必要があるときはいつでも設定を簡単に更新できるようにすることです。設定ファイルからPowershellメニューを作成する
Powershellの設定ファイルからメニューをビルドしようとしています。メニューの目的は、他のスクリプトや実行ファイルを呼び出して保守を簡単にすることで、メニュー項目を追加する必要があるときはいつでも設定を簡単に更新できるようにすることです。設定ファイルからPowershellメニューを作成する
まず、XML形式の設定ファイルを使用することを強くお勧めします。その本当に便利です。私はこのような解決策を考え出したています
#Assuming this is content of your XML config file. You could easily add new Ids to insert new actions.
[xml][email protected]"
<?xml version="1.0"?>
<Menu>
<Actions>
<Id>
<Choice>1</Choice>
<Script>C:\DoThis.ps1</Script>
<Description>Will do this</Description>
</Id>
<Id>
<Choice>2</Choice>
<Script>C:\DoThat.ps1</Script>
<Description>Will do that</Description>
</Id>
</Actions>
</Menu>
"@
#Here's the actual menu. You could add extra formating if you like.
$Message = ''
$Continue = $true
DO
{
cls
Write-Host 'Welcome to the menu!'
Write-Host ''
if ($Message -ne '')
{
Write-Host ''
}
foreach ($Item in @($Config.Menu.Actions.Id))
{
Write-Host ("{0}.`t{1}." -f $Item.Choice,$Item.Description)
}
Write-Host ''
Write-Host "Q.`tQuit menu."
Write-Host ''
$Message = ''
$Choice = Read-Host 'Select option'
if ($Choice -eq 'Q'){$Continue = $false} #this will release script from DO/WHILE loop.
$Selection = $Config.Menu.Actions.Id | ? {$_.Choice -eq $Choice}
if ($Selection)
{
cls
Write-Host ("Starting {0}" -f $Selection.Description)
& $Selection.Script
Write-Host ''
}
else
{
$Message = 'Unknown choice, try again'
}
if ($Continue)
{
if ($Message -ne '')
{
Write-Host $Message -BackgroundColor Black -ForegroundColor Red
}
Read-Host 'Hit any key to continue'
}
cls
}
WHILE ($Continue)
Write-Host 'Exited menu. Have a nice day.'
Write-Host ''
出力:
Welcome to the menu!
1. Will do this.
2. Will do that.
Q. Quit menu.
Select option:
こんにちは、これは素晴らしい仕事しようとしているが、XMLとそんなにクリーンかつ容易に思えます。 スクリプトを更新してXMLファイルを読み込んで、別々に更新できるようにしました。おかげさまで – Ralluhb