首页 文章

由C#应用程序调用的Powershell脚本

提问于
浏览
3

我有这个powershell脚本(test1.ps1)调用另一个PowerShell脚本(test2.ps1)来完成这项工作 .

两个脚本文件都在同一个文件夹中

test1.ps1

echo "from test1.ps1"

.\test2.ps1

test2.ps1

echo "from test2.ps1"

当我通过创建运行空间在C#中调用test1.ps1,向管道添加命令并调用它时,我收到一条错误消息,提示 "The term '.\test2.ps1' 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."

1 回答

  • 4

    这两个脚本可能位于同一个文件夹中,但 .\test2.ps1 将在与调用应用程序相同的文件夹中查找test2.ps1,即C#应用程序 .

    在test.ps1中有这个:

    $scriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
    .$scriptdir\test2.ps1
    

相关问题