I have an app executable say.exe to talk the string in specified language. Some of the commands I run repetedly:
> c:\say.exe --voice 11 "that's wicked, innit" # say sentence out loud in English.
> c:\say.exe --voice 7 "Mamma, voglio la pasta." # say sentence out loud in Italian
...
I’d like to have aliases that would spare me from specifying the --voice X parameter and putting the sentence in quotes
> say_en that''s wicked, innit.
> say_it Mamma, voglio la pasta.
...
Attempt
Set-Alias -Name say_en -Value "c:\say.exe --voice 11"
Creation successful. Fails when running
The term ‘c:\say.exe –voice 11’ is not recognized as a name of a cmdlet,…
Oh, you need to create a function
Function sayWithVoice7 {
C:\say.exe --voice 7 "$args"
}
Set-Alias -Name say_it -Value sayWithVoice7
Function sayWithVoice14 {
C:\say.exe --voice 14 "$args"
}
Set-Alias -Name say_en -Value sayWithVoice14
...
but now I have say N function for N aliases.
I’d like to have a parameterized function
Function sayWithVoice {
Param ($voice_id)
C:\say.exe --voice $voice_id "$args"
}
Set-Alias -Name say_en -Value sayWithVoice 11 # wrong way to pass 11
Set-Alias -Name say_it -Value sayWithVoice 7 # wrong way to pass 7
....
but it doesn’t allow me to pass a parameter to sayWithVoice from within the alias definition. Tried sayWithVoice(11), doesn’t work.
How do I pass the argument 11 to sayWithVoice?
>Solution :
You could use a hashtable to map country codes to voice identifiers, then create the wrapper functions programmatically by writing to the function: drive:
$voiceMap = @{
en = 11
it = 7
# ... and so on
}
foreach ($pair in $voiceMap.GetEnumerator()) {
Set-Content -Path "function:\say_$($pair.Name)" -Value "& say.exe --voice $($pair.Value) ""`$args"""
}
You’ll now have two functions defined: say_en and say_it