-
Notifications
You must be signed in to change notification settings - Fork 0
/
say.ps1
33 lines (29 loc) · 981 Bytes
/
say.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
param(
[Parameter(Mandatory=$true, Position=0)]
[string]$TextToSpeak
)
try {
$voice = New-Object -ComObject SAPI.SpVoice
# Get all available voices
$voices = $voice.GetVoices()
# Find an English voice
$englishVoice = $voices | Where-Object { $_.GetAttribute("Language") -eq "409" } | Select-Object -First 1
if ($englishVoice) {
# Set the voice to the English one we found
$voice.Voice = $englishVoice
$voice.Speak($TextToSpeak)
[System.Environment]::Exit(0) # Explicit exit with 0 for success
} else {
Write-Error "No English voice found on the system."
[System.Environment]::Exit(1) # Explicit exit with 1 for "No English voice found"
}
}
catch {
Write-Error "An error occurred: $_"
[System.Environment]::Exit(2) # Explicit exit with 2 for general error
}
finally {
if ($voice) {
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($voice) | Out-Null
}
}