簡體   English   中英

無法從其他腳本調用PowerShell腳本

[英]Not able to call a PowerShell script from another script

我試圖從另一個調用文件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

你對我該如何處理有任何想法嗎?

PS:按照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

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

你可能需要更換

$ScriptPath = Split-Path $MyInvocation.InvocationName

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM