Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_say.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2016, Jon Hawkesworth (@jhawkesworth) <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7
8 $spec = @{
9 options = @{
10 msg = @{ type = "str" }
11 msg_file = @{ type = "path" }
12 start_sound_path = @{ type = "path" }
13 end_sound_path = @{ type = "path" }
14 voice = @{ type = "str" }
15 speech_speed = @{ type = "int"; default = 0 }
16 }
17 mutually_exclusive = @(
18 , @('msg', 'msg_file')
19 )
20 required_one_of = @(
21 , @('msg', 'msg_file', 'start_sound_path', 'end_sound_path')
22 )
23 supports_check_mode = $true
24 }
25
26 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
27
28
29 $msg = $module.Params.msg
30 $msg_file = $module.Params.msg_file
31 $start_sound_path = $module.Params.start_sound_path
32 $end_sound_path = $module.Params.end_sound_path
33 $voice = $module.Params.voice
34 $speech_speed = $module.Params.speech_speed
35
36 if ($speech_speed -lt -10 -or $speech_speed -gt 10) {
37 $module.FailJson("speech_speed needs to be an integer in the range -10 to 10. The value $speech_speed is outside this range.")
38 }
39
40 $words = $null
41
42 if ($msg_file) {
43 if (-not (Test-Path -LiteralPath $msg_file)) {
44 $msg = -join @(
45 "Message file $msg_file could not be found or opened. "
46 "Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file."
47 )
48 $module.FailJson($msg)
49 }
50 $words = Get-Content -LiteralPath $msg_file | Out-String
51 }
52
53 if ($msg) {
54 $words = $msg
55 }
56
57 if ($start_sound_path) {
58 if (-not (Test-Path -LiteralPath $start_sound_path)) {
59 $msg = -join @(
60 "Start sound file $start_sound_path could not be found or opened. "
61 "Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file."
62 )
63 $module.FailJson($msg)
64 }
65 if (-not $module.CheckMode) {
66 (new-object Media.SoundPlayer $start_sound_path).playSync()
67 }
68 }
69
70 if ($words) {
71 Add-Type -AssemblyName System.speech
72 $tts = New-Object System.Speech.Synthesis.SpeechSynthesizer
73 if ($voice) {
74 try {
75 $tts.SelectVoice($voice)
76 }
77 catch [System.Management.Automation.MethodInvocationException] {
78 $module.Result.voice_info = "Could not load voice '$voice', using system default voice."
79 $module.Warn("Could not load voice '$voice', using system default voice.")
80 }
81 }
82
83 $module.Result.voice = $tts.Voice.Name
84 if ($speech_speed -ne 0) {
85 $tts.Rate = $speech_speed
86 }
87 if (-not $module.CheckMode) {
88 $tts.Speak($words)
89 }
90 $tts.Dispose()
91 }
92
93 if ($end_sound_path) {
94 if (-not (Test-Path -LiteralPath $end_sound_path)) {
95 $msg = -join @(
96 "End sound file $start_sound_path could not be found or opened. "
97 "Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file."
98 )
99 $module.FailJson($msg)
100 }
101 if (-not $module.CheckMode) {
102 (new-object Media.SoundPlayer $end_sound_path).playSync()
103 }
104 }
105
106 $module.Result.message_text = $words.ToString()
107
108 $module.ExitJson()