powershell - 在没有点源的情况下在文件中调用 powershell 函数

标签 powershell

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

你好.ps1

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

最佳答案

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

 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 - 在没有点源的情况下在文件中调用 powershell 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420554/

相关文章:

.net - 在 PowerShell 中使用 System.Net.WebClient 通过 SAN 证书访问 HTTPS URL

php - 连接到Office365时访问被拒绝错误

powershell 上的 Azure CLI 命令给出 '"c :\\\"Program' is not recognized as an internal or external command,

azure - 是否有一些工具/命令可以从 powershell 运行 azcli 文件?

powershell - 如何在 Powershell 中设置 TLS 1.1?

regex - 合并不连续区域的文字

powershell - 如何在 powershell 中设置默认参数值?

powershell - 格式化包含哈希表值的Powershell字符串

powershell - 使用脚本搜索谷歌

powershell - 使用所需状态配置与 Azure 规模集进行驱动器映射