2017-02-22 168 views
1

我正在嘗試調用文件enable.ps1從另一個文件begin.ps1。這兩個文件都在同一個文件夾中。所以,我認爲這可能是我可以使用下面的代碼來達到這個目的。無法從另一個腳本調用PowerShell腳本

這裏是我寫的代碼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:\用戶\ srijani.ghosh \文檔\ Visual Studio的2015年\項目\測試\測試

你對我應該如何着手對此有何想法?

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 

回答

0

你做得對。但它看起來像$ScriptPath不包含任何值(如您在錯誤消息中看到的)。

你可能不得不更換

$ScriptPath = Split-Path $MyInvocation.InvocationName 

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

感謝您的答覆,但不工作要麼:( –

+0

你如何執行begin.ps1腳本? –

+0

我正在使用visual studio 2015。 –

相關問題