如何在PowerShell脚本中将参数化函数作为标志进行别名? [英] How to alias a parameterized function as a flag in powershell script?

查看:221
本文介绍了如何在PowerShell脚本中将参数化函数作为标志进行别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PS脚本,里面有一些功能。
我需要的是我应该能够将特定函数作为别名或变量标志传递给PS脚本。

I have a PS script which has a few functions in it. 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.

例如:假设我有一个TestFunc.ps1文件,其中包含两个函数,如下所示:

For Eg: Suppose I have a TestFunc.ps1 file having 2 functions as below

$XmlFilePath = "C:\XmlPath\file1.xml
Function ParseXml($XmlFilePath)
{
#Do Something
}

Function CreateReport($XmlFilePath)
{
#Do Something
}

现在我怎样才能为这两个函数创建一个别名(ParseXml和CreateReport),以便我可以将它们中的每一个作为标志(使用它们的别名)传递给脚本?
例如:我应该可以执行类似操作:

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? 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.

谢谢!
Ashu

Thanks! Ashu

推荐答案

您可以将一些开关参数添加到您的脚本并测试它们是否设置:

You can add some switch parameter to your script and test if they are sets:

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     
    }

这篇关于如何在PowerShell脚本中将参数化函数作为标志进行别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆