2016-08-25 3 views
1

Я уже задал несколько смежный вопрос here.Как импортировать функции в отдельные файлы в основной файл и запускать их в качестве заданий?

У меня есть куча функций в отдельных файлах, найденных в одном основном файле, как бы я назвал эти функции в основном файле заданиями?

Вот func1.ps1:

function FOO { write-output "HEY" } 

Здесь func2.ps1:

function FOO2 { write-output "HEY2" } 

Вот testjobsMain.ps1

$Functions = { 
    . .\func1.ps1 
    . .\func2.ps1 
} 

$var = Start-Job -InitializationScript $Functions -ScriptBlock { FOO } | Wait-Job | Receive-Job 

$var 

Когда я бегу testjobsMain.ps1 я получаю эта ошибка:

. : The term '.\func1.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again. 
At line:2 char:4 
+  . .\func1.ps1 
+  ~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (.\func1.ps1:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

. : The term '.\func2.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 
name, or if a path was included, verify that the path is correct and try again. 
At line:3 char:4 
+  . .\func2.ps1 
+  ~~~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (.\func2.ps1:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

Running startup script threw an error: The term '.\func2.ps1' is not recognized as the name of a cmdlet, function, script file, or operable 
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.. 
    + CategoryInfo   : OpenError: (localhost:String) [], RemoteException 
    + FullyQualifiedErrorId : PSSessionStateBroken 
+1

Вы пробовали с полными (не относительными) дорожками? То есть: '. c: \ func1.ps1' –

+0

Черт, это было все, что было. Есть ли способ, которым я могу использовать относительный путь к файлам функций? Все они будут в одной папке. – red888

+0

См. Мой ответ ниже. Поверьте мне, у меня много боли с относительными/абсолютными путями PS. –

ответ

1

Абсолютные пути работали для меня:

$Functions = { 
    . c:\foo.ps1 
} 
$var = Start-Job -InitializationScript $Functions -ScriptBlock { FOO } | Wait-Job | Receive-Job 
$var 

В случае необходимости, в testjobsMain.ps1 вы можете заменить относительные пути с абсолютным путем использования $PSScriptRoot автоматической переменной. Например:

$Functions = [scriptblock]::Create(" . $PSScriptRoot\foo.ps1 `n . $PSScriptRoot\bar.ps1 `n")