使用“使用PowerShell运行"执行时,在另一个脚本中调用函数 [英] Call a function in another script when executing using 'Run With PowerShell'

查看:602
本文介绍了使用“使用PowerShell运行"执行时,在另一个脚本中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在库"文件中具有要从工作人员"脚本中调用的功能.

I have functions in a 'library' file to be called from my 'worker' script.

库文件

function ShowMessage($AValue)
{
  $a = new-object -comobject wscript.shell
  $b = $a.popup( $AValue )
}

工作文件

. {c:\scratch\b.ps1}

ShowMessage "Hello"

在PowerShell IDE中运行'worker'脚本可以正常工作,但是当我右键单击worker文件并选择'Run with PowerShell'时,找不到函数'ShowMessage'.这两个文件都在同一个文件夹中.可能会发生什么?

Running the 'worker' script works fine when in the PowerShell IDE but when I right-click the worker file and choose 'Run with PowerShell' it cannot find the function 'ShowMessage'. Both files are in the same folder. What might be happening?

推荐答案

在工作文件中更改为:

. "c:\scratch\b.ps1"

ShowMessage "Hello"

如下面的@RoiDanton所述:

As @RoiDanton mentioned below:

使用相对路径时的注意事项:不要忘了在圆点前加点 在小路之前. .\ b.ps1".

Attention when using relative pathing: Don't forget to prepend a dot before the path . ".\b.ps1".

第一个点是用于修改范围的运算符,在这种情况下,它与路径无关.请参见点源符号.

The first dot is an operator used to modify the scope and in that context it has nothing to do with paths. See Dot Source Notation.

这篇关于使用“使用PowerShell运行"执行时,在另一个脚本中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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