2017-02-22 30 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: \ Users \ srijani.ghosh \ Documents \ Visual Studio 2015 \ Projects \ Test \ тест

У вас есть какие-либо идеи о том, как я должен действовать на это?

P.S: внес некоторые изменения, предложенные Мартином. Теперь код выглядит так:

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

Я использую для этого визуальную студию 2015. –

 Смежные вопросы

  • Нет связанных вопросов^_^