I am creating a powershell module and following the Verb-Noun naming conventions for the functions that I exported.
e.g.
function Do-Something() {
sleep 10
}
Importing the modules works as expected… BUT if I import that module as a custom object.
$myObject = Import-Module MyModule -AsCustomObject
Then try to access the function, I get a parse error:
PS > $myObject.Do-Something()
You must provide a value expression on the right-hand side of the '-' operator.
At line:1 char:22
+ $myObject.Do- <<<< Something()
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression
I don’t understand. Does this mean that I can’t use the ‘-‘ in methods that belong to objects?
I have noticed that .NET objects and other PSObjects do not have ‘-‘ in their method names.
I haven’t figured out a way to escape it yet either… but even so, that would be messy.
Try this:
$myObject.”Do-Something”()