Bri*_*ost 48 powershell function
我开始使用PowerShell并在'库'文件中创建函数以提高可读性,然后我从'worker'脚本调用它.
function ShowMessage($AValue)
{
$a = new-object -comobject wscript.shell
$b = $a.popup( $AValue )
}
Run Code Online (Sandbox Code Playgroud)
在PowerShell IDE中运行'worker'脚本时工作正常,但是当我右键单击worker文件并选择'Run with PowerShell'时,它找不到函数'ShowMessage'.两个文件都在同一个文件夹中.请问这里可能会发生什么?
And*_*huk 77
尝试添加如下脚本:
. "c:\scratch\b.ps1"
ShowMessage "Hello"
Run Code Online (Sandbox Code Playgroud)
正如下面提到的@RoiDanton:使用相对路径时的注意事项:不要忘记在路径前添加一个点."\b.ps1" .作为psh的新手,我不知道第一个点是修改范围的运算符,并且在该上下文中与路径无关.请参阅点源表示法.
Sha*_*evy 15
在您的工作文件中,点源库文件,这将把所有内容(函数,变量等)加载到全局范围,然后您就可以从库文件中调用函数.
=================== Worker file ==========================
# dot-source library script
# notice that you need to have a space
# between the dot and the path of the script
. c:\library.ps1
ShowMessage -AValue Hello
=================== End Worker file ======================
Run Code Online (Sandbox Code Playgroud)