繁体   English   中英

如何在Powershell脚本中将参数化函数作为标志的别名?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM