[英]PowerShell - Processing Command - Line Parameters In Correct Order
我有一個關於在 PowerShell 中處理命令行參數的問題:
我開發了一個結構來接收命令行arguments,實際上允許3個參數:“-Help”,“-Step”和“-Config”。
問題與處理參數的方式有關。 在我的概念中,腳本總是首先檢查是否存在“-Config”參數。 之后它應該加載基本設置並最后處理“-Step”參數以確保加載配置。
我也期待,如果用戶輸入“-Help”參數,無論指定哪個參數,腳本只會 output 幫助對話框。
我真的試圖削減大部分代碼,但我想,所有仍然存在的部分都是必要的。
如果您在命令行上執行腳本,如下所示:
.\AI_Installer.ps1 -Config C:\Scripts\DCT01.cfg -Step Configure_Server -Help
output 將是:
[ 5 ] Configuring Server Settings ...
[ 1 ] User - Defined Config Found ! [ C:\Scripts\DCT01.cfg ] 0
[ 2 ] The Directory For The User - Defined Config Is Valid ! [ C:\Scripts\DCT01.cfg ]
[ 3 ] User - Defined Config Is Vaild ! [ DCT01.cfg ]
This Is The Help Sector !
[ 4 ] Loading Basic Settings ...
我已經對這些步驟進行了編號,以便更容易理解,我的意思是;)
如果我可以在“Initialize_Parameters”中手動定義,那將是最好的,哪個參數將首先處理,第二個,最后一個等。
我希望,你明白,我想要做什么,你能夠為我提供一個解決方案。
非常感謝。
最好的問候 NumeroUnoDE
可以定義一個參數集,以便允許一組參數。 這在 Powershell cmdlet 中非常典型。 例如, Get-Process允許進程名稱或 ID,但不能同時使用兩者。
一個簡單的例子需要幫助或兩個非幫助參數,就像這樣,
function Test-ParameterSets {
[CmdletBinding()]
param(
[Parameter(Mandatory,ParameterSetName = 'Config')]
[string]$config,
[Parameter(Mandatory,ParameterSetName = 'Config')]
[string]$step,
[Parameter(Mandatory, ParameterSetName = 'Help')]
[switch]$help
)
if($config) { "Config section $config $step" }
if($help) { "Help section $help" }
}
# Call with two valid parameters
Test-ParameterSets -config "config.cfg" -step "step 001"
Config section config.cfg step 001
# Call with one valid parameters
Test-ParameterSets -help
Help section True
# Call with invalid parameters (no help and others)
Test-ParameterSets -config "config.cfg" -step "step 001" -help Test-ParameterSets : Parameter set cannot be resolved using the specified named parameters.
# Call with missing parameter
Test-ParameterSets -config "config.cfg"
cmdlet Test-ParameterSets at command pipeline position 1
Supply values for the following parameters:
step:
$Global_Switches = $args $Global_Switches_Store = @{ } $Global_Switches_Values_Store = @( ) $AI_Identifier = $myInvocation.MyCommand.Definition $AI_Server_Config = @{ } $AI_Debug_Mode = "Yes"
function Initialize { if( !( $Global_Switches ) ) { $Global_Switches_Store[ '-Step' ] = @( "Configure_Server" ) Load_Basic_Settings Selector } if( $Global_Switches ) { Initialize_Parameters Selector Load_Basic_Settings } }
function Initialize_Parameters { $Global_Switches_Entries = $Global_Switches.Count $Global_Switches_Parameter for( $Global_Switches_Counter = 0; $Global_Switches_Counter -lt $Global_Switches_Entries; $Global_Switches_Counter++ ) { if( $Global_Switches[ $Global_Switches_Counter ].Contains( "-" ) ) { if( !( $Global_Switches_Store.Contains( $Global_Switches[ $Global_Switches_Counter ] ) ) ) { $Global_Switches_Values = @( ) $Global_Switches_Parameter = $Global_Switches[ $Global_Switches_Counter ] } else { Write-Host "The Parameter '$( $Global_Switches[ $Global_Switches_Counter ] )' Already Exists !" } } else { $Global_Switches_Values += $Global_Switches[ $Global_Switches_Counter ] } if( $Global_Switches_Parameter ) { $Global_Switches_Store[ $Global_Switches_Parameter ] = $Global_Switches_Values } } }
function Selector { $Available_Switches = @( "-Config" , "-Help" , "-Step" ) foreach( $Switches_Parameter in $Global_Switches_Store.Keys ) { if( $Available_Switches.Contains( $Switches_Parameter ) ) { if( $Switches_Parameter -eq "-Config" ) { $Switches_Values_Entries = $Global_Switches_Store[ $Switches_Parameter ].Count if( $Switches_Values_Entries -eq 0 ) { Write-Host "Need Values For Parameter '$Switches_Parameter' !" ; exit } if( $Switches_Values_Entries -gt 1 ) { Write-Host "Only One Value Allowed For Parameter '$Switches_Parameter' !" ; exit } $IMWS_Config_User_Defined = $Global_Switches_Store[ $Switches_Parameter ] Write-Host "[ 1 ] User - Defined Config Found ! [ $IMWS_Config_User_Defined ]" 0 if( Test-Path $IMWS_Config_User_Defined ) { Write-Host "[ 2 ] The Directory For The User - Defined Config Is Valid ! [ $IMWS_Config_User_Defined ]" $Config_File_Handler = Split-Path $IMWS_Config_User_Defined -leaf $Config_File_EXTN = [IO.Path]::GetExtension( $Config_File_Handler ) if( $Config_File_EXTN -ne ".cfg" ) { Write-Host "[ 3 ] User - Defined Config Is Not Vaild ! [ $Config_File_Handler ]" exit } else { Write-Host "[ 3 ] User - Defined Config Is Vaild ! [ $Config_File_Handler ]" } } else { Write-Host "[ 2 ] User - Defined Config Not Found ! [ $IMWS_Config_User_Defined ]" exit } } if( $Switches_Parameter -eq "-Help" ) { Write-Host "This Is The Help Sector !" } if( $Switches_Parameter -eq "-Step" ) { $Switches_Values_Entries = $Global_Switches_Store[ $Switches_Parameter ].Count if( $Switches_Values_Entries -eq 0 ) { Write-Host "Need Values For Parameter '$Switches_Parameter' !" # exit } if( $Switches_Values_Entries -gt 1 ) { Write-Host "Only One Value Allowed For Parameter '$Switches_Parameter' !" # exit } $Switches_Parameters = $Global_Switches_Store[ $Switches_Parameter ] for( $Switches_Values_Counter = 0; $Switches_Values_Counter -lt $Switches_Values_Entries; $Switches_Values_Counter++ ) { $Switches_Values = $Switches_Parameters[ $Switches_Values_Counter ] if( $Switches_Values -eq "Configure_Server" ) { Configure_Server } } } } else { Write-Host "Wrong Parameter '$Switches_Parameter' !" } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.