I am trying to build a script, that accepts named arguments - but the script also has functions in it... This gives me a problem, that I cannot see how to fix. The script - c:\\temp\\example.ps1 - looks like:
function test
{
param($p1)
write-host $p1
}
param(
[parameter(mandatory=$false)]
[switch]$EnableOption,
[parameter(mandatory=$false)]
[string]$Hostname
)
test -p1 $Hostname
This gives me:
param : 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:\Temp\example.ps1:7 char:1
+ param(
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
How do I fix this - so that named parameters can be used for functions - but also be used in the "main" script (getting the command line arguments when calling the script) ?
Change the order in your script
param(
[parameter(mandatory=$false)]
[switch]$EnableOption,
[parameter(mandatory=$false)]
[string]$Hostname
)
function test
{
param($p1)
write-host $p1
}
test -p1 $Hostname
Param
section must be at the start of your code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.