[英]How to alias a parameterized function as a flag in powershell script?
I have a PS script which has a few functions in it. 我有一个PS脚本,其中包含一些功能。 What I need is that I should be able to pass the specific function to the PS script as an alias or a variable flag. 我需要的是我应该能够将特定功能作为别名或变量标志传递给PS脚本。
For Eg: Suppose I have a TestFunc.ps1 file having 2 functions as below 例如:假设我有一个具有2个函数的TestFunc.ps1文件,如下所示
$XmlFilePath = "C:\XmlPath\file1.xml
Function ParseXml($XmlFilePath)
{
#Do Something
}
Function CreateReport($XmlFilePath)
{
#Do Something
}
Now how can I create an alias for both the functions (ParseXml and CreateReport) so that I could pass each of them as a flag (using their alias) to the script? 现在,如何为两个函数(ParseXml和CreateReport)创建一个别名,以便可以将它们中的每个作为标志(使用它们的别名)传递给脚本? For Eg: I should be able to do something like: 对于例如:我应该能够执行以下操作:
. .\TestFunc.ps1 -Xml #This must be able to execute the ParseXml($XmlFilePath) function for me
. .\TestFunc.ps1 -Report #This must execute CreateReport($XmlFilePath) function
Any help would be greatly appreciated. 任何帮助将不胜感激。 I have been struggling in this for a while now. 我已经为此努力了一段时间。
Thanks! 谢谢! Ashu 阿树
You can add some switch parameter to your script and test if they are sets: 您可以在脚本中添加一些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.