在没有点源的情况下在文件中调用 powershell 函数 [英] Call powershell function in file without dot sourcing

查看:55
本文介绍了在没有点源的情况下在文件中调用 powershell 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的文件中只有一个与文件同名的函数,这可能吗?我似乎记得以前读过它.像这样:

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

function hello {
    Write-Host 'Hello, world'
}

推荐答案

我会完全摆脱函数调用.您根本不会丢失命名参数和 cmdlet 包装.所以这个:

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!"
 }

变成:

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

你可以这样:

> .hello.ps1 "World"

这篇关于在没有点源的情况下在文件中调用 powershell 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆