2016-10-24 13:49:06 +00:00
#!powershell
# This file is part of Ansible
#
# Copyright 2016, Jon Hawkesworth (@jhawkesworth) <figs@unity.demon.co.uk>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON
# POWERSHELL_COMMON
2017-02-14 02:05:49 +00:00
$params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name " _ansible_check_mode " -type " bool " -default $false
$msg = Get-AnsibleParam -obj $params -name " msg " -type " str "
2017-01-16 19:39:58 +00:00
$msg_file = Get-AnsibleParam -obj $params -name " msg_file " -type " path "
$start_sound_path = Get-AnsibleParam -obj $params -name " start_sound_path " -type " path "
$end_sound_path = Get-AnsibleParam -obj $params -name " end_sound_path " -type " path "
2017-02-14 02:05:49 +00:00
$voice = Get-AnsibleParam -obj $params -name " voice " -type " str "
2017-06-28 20:28:44 +00:00
$speech_speed = Get-AnsibleParam -obj $params -name " speech_speed " -type " int " -default 0
2017-02-14 02:05:49 +00:00
$result = @ {
changed = $false
}
2016-10-24 13:49:06 +00:00
$words = $null
2017-06-28 20:28:44 +00:00
f ( $speech_speed -lt -10 -or $speech_speed -gt 10 ) {
Fail-Json $result " speech_speed needs to a integer in the range -10 to 10. The value $speech_speed is outside this range. "
2016-10-24 13:49:06 +00:00
}
2017-06-28 20:28:44 +00:00
if ( $msg_file -and $msg ) {
2016-10-24 13:49:06 +00:00
Fail-Json $result " Please specify either msg_file or msg parameters, not both "
}
2017-06-28 20:28:44 +00:00
if ( -not $msg_file -and -not $msg -and -not $start_sound_path -and -not $end_sound_path ) {
2016-10-24 13:49:06 +00:00
Fail-Json $result " No msg_file, msg, start_sound_path, or end_sound_path parameters have been specified. Please specify at least one so the module has something to do "
}
2017-06-28 20:28:44 +00:00
if ( $msg_file ) {
if ( Test-Path -Path $msg_file ) {
2017-02-14 02:05:49 +00:00
$words = Get-Content $msg_file | Out-String
2016-10-24 13:49:06 +00:00
} else {
2017-02-14 02:05:49 +00:00
Fail-Json $result " Message file $msg_file could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file. "
2016-10-24 13:49:06 +00:00
}
}
2017-06-28 20:28:44 +00:00
if ( $start_sound_path ) {
if ( Test-Path -Path $start_sound_path ) {
2017-02-14 02:05:49 +00:00
if ( -not $check_mode ) {
( new-object Media . SoundPlayer $start_sound_path ) . playSync ( )
}
} else {
Fail-Json $result " Start sound file $start_sound_path could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file. "
}
2016-10-24 13:49:06 +00:00
}
2017-06-28 20:28:44 +00:00
if ( $msg ) {
2016-10-24 13:49:06 +00:00
$words = $msg
2017-02-14 02:05:49 +00:00
}
2016-10-24 13:49:06 +00:00
2017-06-28 20:28:44 +00:00
if ( $words ) {
2016-10-24 13:49:06 +00:00
Add-Type -AssemblyName System . speech
$tts = New-Object System . Speech . Synthesis . SpeechSynthesizer
2017-06-28 20:28:44 +00:00
if ( $voice ) {
2016-10-24 13:49:06 +00:00
try {
$tts . SelectVoice ( $voice )
} catch [ System.Management.Automation.MethodInvocationException ] {
2017-02-14 02:05:49 +00:00
$result . voice_info = " Could not load voice $voice , using system default voice. "
2016-10-24 13:49:06 +00:00
}
}
2017-02-14 02:05:49 +00:00
$result . voice = $tts . Voice . Name
2017-06-28 20:28:44 +00:00
if ( $speech_speed -ne 0 ) {
$tts . Rate = $speech_speed
2016-10-24 13:49:06 +00:00
}
2017-02-14 02:05:49 +00:00
if ( -not $check_mode ) {
$tts . Speak ( $words )
}
2016-10-24 13:49:06 +00:00
$tts . Dispose ( )
}
2017-06-28 20:28:44 +00:00
if ( $end_sound_path ) {
if ( Test-Path -Path $end_sound_path ) {
2017-02-14 02:05:49 +00:00
if ( -not $check_mode ) {
( new-object Media . SoundPlayer $end_sound_path ) . playSync ( )
}
} else {
Fail-Json $result " End sound file $start_sound_path could not be found or opened. Ensure you have specified the full path to the file, and the ansible windows user has permission to read the file. "
}
2016-10-24 13:49:06 +00:00
}
2017-02-14 02:05:49 +00:00
$result . message_text = $words
2016-10-24 13:49:06 +00:00
2017-02-14 02:05:49 +00:00
Exit-Json $result