[英]How to alias a parameterized function as a flag in powershell script?
我有一個PS腳本,其中包含一些功能。 我需要的是我應該能夠將特定功能作為別名或變量標志傳遞給PS腳本。
例如:假設我有一個具有2個函數的TestFunc.ps1文件,如下所示
$XmlFilePath = "C:\XmlPath\file1.xml
Function ParseXml($XmlFilePath)
{
#Do Something
}
Function CreateReport($XmlFilePath)
{
#Do Something
}
現在,如何為兩個函數(ParseXml和CreateReport)創建一個別名,以便可以將它們中的每個作為標志(使用它們的別名)傳遞給腳本? 對於例如:我應該能夠執行以下操作:
. .\TestFunc.ps1 -Xml #This must be able to execute the ParseXml($XmlFilePath) function for me
. .\TestFunc.ps1 -Report #This must execute CreateReport($XmlFilePath) function
任何幫助將不勝感激。 我已經為此努力了一段時間。
謝謝! 阿樹
您可以在腳本中添加一些switch參數並測試它們是否已設置:
param( [switch]$xml, [switch]$Report)
Function ParseXml($XmlPath)
{
"Parse"
}
Function CreateReport($XmlPath)
{
"Report"
}
$XmlPath = "C:\XmlPath\file1.xml"
if ($xml)
{
ParseXml $XmlPath
}
if ($Report)
{
CreateReport $xmlPath
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.