Powershell-Skript mit params *und* Funktionen
Ich möchte ein powershell-Skript schreiben, params und Funktionen nutzt.
Habe ich versucht, dieses:
param
(
$arg
)
Func $arg;
function Func($arg)
{
Write-Output $arg;
}
aber ich habe dieses:
The term 'Func' 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 func.ps1:6 char:5
+ Func <<<< $arg;
+ CategoryInfo : ObjectNotFound: (Func:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Fein, dachte ich. Ich werde versuchen, dieses statt:
function Func($arg)
{
Write-Output $arg;
}
param
(
$arg
)
Func $arg;
Aber dann bekam ich diese:
The term 'param' 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 C:\Users\akina\Documents\Work\ADDC\func.ps1:7 char:10
+ param <<<<
+ CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Ist, was ich Fragen, für machbar? Oder bin ich unvernünftig in meiner Anfrage?
Die Bestellung eines powershell-Skripts ist in der Regel, 1) Params, 2) Funktionen 3) - Funktion ruft/bestellt-cmdlets ausführen.
Christopher Ranney, das ist eine hilfreiche Zusammenfassung. Wenn Sie gepostet hatte dies als eine Frage, ich würde gestimmt haben, es bis.
Abstimmen können Sie Kommentare, wenn euer Herz so begehrt. 🙂
Christopher Ranney, das ist eine hilfreiche Zusammenfassung. Wenn Sie gepostet hatte dies als eine Frage, ich würde gestimmt haben, es bis.
Abstimmen können Sie Kommentare, wenn euer Herz so begehrt. 🙂
InformationsquelleAutor Tola Odejayi | 2013-02-15
Du musst angemeldet sein, um einen Kommentar abzugeben.
Der param-block in einem script muss die erste nicht-Kommentar-code. Danach definieren Sie die Funktion, bevor Sie es aufrufen, z.B.:
Den Write-Output ist unnötig in deinem Beispiel, da das Standardverhalten ist, die Ausgabe der Objekte auf den output-stream.
Das gleiche gilt für ein Modul. Obwohl es ist nicht wahrscheinlich der typische Fall, eine PSM1 kann Parameter definieren und ausführen von code (nicht nur definieren fucnctions) wie eine normale Skript-Datei.
InformationsquelleAutor Keith Hill
Alles, was Sie brauchen, ius, um sicherzustellen PARAM ist der erste string des Skripts.
InformationsquelleAutor Anton Chernysh
Setzen Sie den param-tag innerhalb der Funktion..
Etwas wie dieses:
InformationsquelleAutor user3119242