在PowerShell中运行一个函数
问题描述:
我是PS新手,已经给脚本运行了。我做的第一件事是输入c:\powershell\ir.ps1
。这似乎工作。然后,在定义我的客户目录后,我应该能够输入诸如ir-n
之类的函数。这个工作在人的办公桌上显示,我该怎么做,但我得到了以下错误:在PowerShell中运行一个函数
The term 'ir-n' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:1 char:5
+ ir-n <<<<
+ CategoryInfo : ObjectNotFound: (ir-n:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
有什么简单的,我可以做的就是它运行?我看到ir.ps1
文件中的功能,所以我知道它在那里。
答
看起来您正在运行ir.ps1
脚本,当您应该采购它时。我猜测ir.ps1
文件正在定义一个名为ir-n
的函数。在这种情况下,运行脚本不会在脚本的上下文中定义函数,而不会在命令窗口中定义函数。您需要获取脚本以使其保留在命令窗口中。
尝试运行以下
PS$> . c:\powershell\ir.ps1
运行之后再尝试ir-n
。
答
你可能需要dot source
该脚本会离开它定义可用的功能在全球范围内例如为:
PS> . c:\powershell\ir.ps1
你们和这个论坛是真棒......它的工作。如果我不能回来,我会看看我现在能否完成。谢谢! – user385028 2010-07-07 01:21:12
如果这个工作,你应该接受这个答案。 – JMD 2015-02-24 17:41:33
我不敢相信你注意到答案很好,还没有接受。我相信这只是一个简单的监督。 :) – 2017-11-06 12:50:56