Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load another powershell file before the CmdletBinding?

I am using powershell 5. I created an enum in different ps1 file and I like to load it in another ps1 that has CmdletBinding.

I tried the code below but didn't work. Is there any way to load the enum ps1 before the CmdletBinding?

. ".\GeneratorType.ps1" 

[CmdletBinding()]
Param
(
        [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName = $True,Mandatory=$False)]       
        [GeneratorType]$type = [GeneratorType]::All
)
like image 806
Michael Sync Avatar asked Oct 26 '25 14:10

Michael Sync


1 Answers

Think of it like this: A PowerShell script is a self-contained ScriptBlock.

The [CmdletBinding()] attribute is part of the param-block. According to the language specification, a param-block must be the first part of the ScriptBlock if present.

So no, you cannot put anything before the CmdletBinding attribute.

What you can do, is to convert your GeneratorTypes.ps1 script to a module and use

#Requires -Modules

to make sure it's loaded when the param block is interpreted.

like image 71
oɔɯǝɹ Avatar answered Oct 29 '25 06:10

oɔɯǝɹ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!