Windows 无法从其他脚本调用PowerShell脚本

Windows 无法从其他脚本调用PowerShell脚本,windows,powershell,windows-7,powershell-5.0,Windows,Powershell,Windows 7,Powershell 5.0,我正在尝试从另一个文件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

我正在尝试从另一个文件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
  • 视窗7
  • PowerShell 5
  • begin.ps1和enable.ps1都位于相同的文件夹位置,即:

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

你知道我该怎么做吗

附言:按照马丁的建议做了一些改变。现在代码如下所示:

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

你做得对。但是,它看起来似乎不包含任何值(如错误消息中所示)

您可能必须更换

$ScriptPath = Split-Path $MyInvocation.InvocationName


感谢您的回复,但这也不起作用:(您如何执行begin.ps1脚本?我正在使用visual studio 2015。嗯,很有趣。您可以尝试使用powershell执行脚本吗?为什么他不能直接使用
$PSScriptRoot
$ScriptPath = split-path -parent $MyInvocation.MyCommand.Definition