2016-07-22 4 views
0

Powershell의 설정 파일에서 메뉴를 만들려고했습니다. 메뉴의 목적은 다른 스크립트와 실행 파일을 호출하고 유지 관리를 쉽게하기 위해서 메뉴 항목을 추가해야 할 때마다 쉽게 구성을 업데이트 할 수 있도록하는 것입니다.설정 파일에서 Powershell 메뉴 만들기

답변

3

우선 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: 
+0

이 잘 작동 할 것입니다 안녕하세요 보인다 훨씬 깨끗하고 XML로 쉽게. 별도로 업데이트 할 수 있도록 XML 파일을 읽도록 스크립트를 업데이트했습니다. 감사합니다. . – Ralluhb