【问题标题】:Not able to call a PowerShell script from another script无法从另一个脚本调用 PowerShell 脚本
【发布时间】:2017-07-12 12:56:50
【问题描述】:

我正在尝试从另一个文件 begin.ps1 调用文件 enable.ps1。这两个文件都在同一个文件夹中。所以,我想可能我可以使用下面的代码来达到这个目的。

这是我在 begin.ps1 中编写的用于调用的代码。

#
# begin.ps1
#



function MapDrives ($Message)
    {
        Write-Host Network drives does not exist till now. Trying again to connect
        Write-Host ...............................................................
        WriteInLogFile "Network drives does not exist till now. Trying again to connect"

        $ScriptPath = Split-Path $MyInvocation.InvocationName
& "$ScriptPath\enable.ps1"


        cmd /c pause | out-null
        Start-Sleep -s 20
    }

我正在尝试调用 PowerShell 文件:enable.ps1

  • 我正在使用 Visual Studio 2015
  • Windows 7
  • PowerShell 5
  • begin.ps1 和 enable.ps1 都在同一个文件夹位置下:

    C:\Users\srijani.ghosh\Documents\visual studio 2015\Projects\test\test

您对我应该如何进行此操作有任何想法吗?

P.S :按照 Martin 的建议进行了一些更改。现在代码如下所示:

function MapDrives ($Message)
{
    Write-Host Network drives does not exist till now. Trying again to connect
    Write-Host ...............................................................
    WriteInLogFile "Network drives does not exist till now. Trying again to connect"

    $ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
    & "$ScriptPath\enable.ps1"


    cmd /c pause | out-null
    Start-Sleep -s 20
}

而且,我正在尝试在 PowerShell ISE 中运行它。它给出了这个错误。

Network drives does not exist till now. Trying again to connect
...............................................................
& : The module 'param($Message)
    Write-Host Network drives does not exist till now. Trying again to connect
    Write-Host ...............................................................
    WriteInLogFile "Network drives does not exist till now. Trying again to connect"
    $ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
    & "$ScriptPath' could not be loaded. For more information, run 'Import-Module param($Message)
    Write-Host Network drives does not exist till now. Trying again to connect
    Write-Host ...............................................................
    WriteInLogFile "Network drives does not exist till now. Trying again to connect"
    $ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
    & "$ScriptPath'.
At C:\Users\srijani.ghosh\Documents\visual studio 2015\Projects\test\test\begin.ps1:45 char:7
+     & "$ScriptPath\enable.ps1"
+       ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (param($Message)...cmd \enable.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoLoadModule

【问题讨论】:

    标签: windows powershell windows-7 powershell-5.0


    【解决方案1】:

    你做得对。但是看起来 $ScriptPath 不包含任何值(正如您在错误消息中看到的那样)。

    您可能必须替换

    $ScriptPath = Split-Path $MyInvocation.InvocationName
    

    $ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition
    

    【讨论】:

    • 感谢您的回复,但这也不起作用:(
    • 如何执行 begin.ps1 脚本?
    • 我为此使用了 Visual Studio 2015。
    • 嗯,很有趣。你可以尝试使用powershell执行脚本吗?
    • 他为什么不能直接使用$PSScriptRoot
    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 2011-10-12
    • 2016-07-11
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2011-06-01
    相关资源
    最近更新 更多