单行PowerShell语音报时脚本失效,求实现每5分钟整点报时方案
每5分钟整点语音报时解决方案
基础语音播报验证
先确认语音功能正常,执行以下命令测试是否能播报当前时间:
PowerShell -Command "Add-Type -AssemblyName System.Speech; $synth = New-Object System.Speech.Synthesis.SpeechSynthesizer; $time = Get-Date -Format 'hh:mm tt'; $synth.Speak('It is ' + $time)"
若仍失败,检查系统语音服务是否开启,或在「设置→时间和语言→语音」更换默认语音库。
方案一:PowerShell循环脚本实现
创建名为TalkingClock.ps1的脚本,内容如下:
Add-Type -AssemblyName System.Speech $synth = New-Object System.Speech.Synthesis.SpeechSynthesizer while ($true) { $currentTime = Get-Date # 仅在分钟为5的倍数时播报(00、05、10...55) if ($currentTime.Minute % 5 -eq 0) { $timeText = $currentTime.ToString('hh:mm tt') $synth.Speak("It's $timeText") # 避免同一分钟重复播报,延迟61秒 Start-Sleep -Seconds 61 } # 每10秒检查一次时间 Start-Sleep -Seconds 10 }
运行方式:
- 打开PowerShell,定位到脚本所在目录,执行
.\TalkingClock.ps1 - 如需后台运行,执行
powershell -WindowStyle Hidden -File .\TalkingClock.ps1
方案二:Windows任务计划实现(推荐)
无需保持PowerShell窗口打开,支持开机自动启动:
- 打开「任务计划程序」,点击「创建任务」
- 常规标签:填写任务名称,勾选「不管用户是否登录都要运行」,选择「不存储密码」
- 触发器标签:点击「新建」,设置「每天」,开始时间选当前时间稍后;重复任务间隔设为「5分钟」,持续时间选「无限期」,确认
- 操作标签:点击「新建」,操作选「启动程序」,程序或脚本填
powershell.exe,添加参数填:-Command "Add-Type -AssemblyName System.Speech; $synth = New-Object System.Speech.Synthesis.SpeechSynthesizer; $time = Get-Date -Format 'hh:mm tt'; $synth.Speak('It is ' + $time)" - 设置标签:勾选「如果任务运行时间超过1分钟就停止」,确认保存
旧脚本失效原因说明
Out-Speech是第三方PowerShell模块,默认未预装,需手动安装才能使用SAPI.SpVoice为老旧COM组件,在新版Windows/PowerShell中存在兼容性或权限问题,推荐使用官方的System.Speech.NET类替代
内容的提问来源于stack exchange,提问作者nooobb
相关产品推荐
相关产品推荐

