2016-08-25 3 views
1

Ich fragte bereits eine etwas verwandte Frage here.Wie importiere ich Funktionen in separaten Dateien in eine Hauptdatei und führe sie als Jobs aus?

Ich habe eine Reihe von Funktionen in separaten Dateien, die in einer Hauptdatei gespeichert sind. Wie würde ich diese Funktion in der Hauptdatei als Job bezeichnen?

Hier ist func1.ps1:

function FOO { write-output "HEY" } 

Hier ist func2.ps1:

function FOO2 { write-output "HEY2" } 

Hier ist testjobsMain.ps1

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

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

$var 

Wenn ich laufen testjobsMain.ps1 ich Dieser Fehler:

. : 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

Haben Sie versucht mit vollen (nicht relativen) Wegen? Das ist: '. c: \ func1.ps1' –

+0

Verdammt das war alles was es war. Gibt es eine Möglichkeit, den relativen Pfad der Funktionsdateien zu verwenden? Sie werden alle im selben Ordner sein. – red888

+0

Siehe meine Antwort unten. Ich habe eine Menge Schmerzen mit PS relativen/absoluten Pfaden, glaub mir. –

Antwort

1

Absolute Pfade für mich gearbeitet:

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

Bei Bedarf in testjobsMain.ps1 Sie relative Pfade mit absoluter durch Verwendung $PSScriptRoot automatische Variable ersetzen können. Zum Beispiel:

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