You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为PowerShell参数化函数创建带预设参数的别名?

解决预设语音ID的朗读命令别名问题

方法1:核心函数+轻量包装(最简洁易维护)

不用写大量重复函数,先定义一个通用核心函数,再通过极简的包装生成带预设参数的命令:

  1. 定义核心参数化函数
function sayWithVoice {
    param(
        [Parameter(Mandatory=$true)]
        [int]$VoiceId,
        [Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
        [string[]]$Text
    )
    $fullText = $Text -join ' '
    & c:\say.exe --voice $VoiceId $fullText
}

ValueFromRemainingArguments会自动把所有输入的文本片段拼接成完整字符串,不用手动加引号。

  1. 快速生成预设语音ID的命令
function say_en { sayWithVoice -VoiceId 11 @args }
function say_it { sayWithVoice -VoiceId 7 @args }

@args会把用户输入的所有内容直接传递给核心函数,完全匹配你想要的使用方式。

方法2:脚本块式别名(进阶技巧)

如果不想创建多个小函数,也可以直接给脚本块创建"别名"(本质是匿名函数):

# 先定义核心函数(同方法1)
function sayWithVoice {
    param(
        [Parameter(Mandatory=$true)]
        [int]$VoiceId,
        [Parameter(Mandatory=$true, ValueFromRemainingArguments=$true)]
        [string[]]$Text
    )
    $fullText = $Text -join ' '
    & c:\say.exe --voice $VoiceId $fullText
}

# 创建预设参数的命令
New-Item -Path Function:\say_en -Value { sayWithVoice -VoiceId 11 @args } | Out-Null
New-Item -Path Function:\say_it -Value { sayWithVoice -VoiceId 7 @args } | Out-Null

使用效果

直接输入预设命令即可,无需引号和语音参数:

say_en that's wicked, innit.
say_it Mamma, voglio la pasta.

补充说明

  • 把代码写入PowerShell配置文件(执行notepad $PROFILE打开),重启PowerShell后即可永久生效。
  • 新增其他语言时,只需复制一行包装代码修改语音ID即可,完全避免冗余。

内容的提问来源于stack exchange,提问作者parml

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 13:12:17