Call powershell function in file without dot sourcing

后端 未结 2 1375
无人及你
无人及你 2021-01-18 22:06

Is this possible if I only have one function in the file named the same as the file? I seem to remember reading about it before. Something like this:

hello.ps1

2条回答
  •  盖世英雄少女心
    2021-01-18 22:44

    I would get rid of the function call altogether. You don't lose named parameters and cmdlet wrapping at all. So this:

     function Hello
     {
        [CmdletBinding()]
        param(
           [Parameter(Mandatory=$true)]
           $Message
        )
        Write-Host "Hello, $Message!"
     }
    

    becomes:

     [CmdletBinding()]
     param(
        [Parameter(Mandatory=$true)]
        $Message
     )
     Write-Host "Hello, $Message!"
    

    And you can all it like this:

    > .hello.ps1 "World"
    

提交回复
热议问题