From 0cc63a2b75bdd177a9e89b73c95e84c3f6c0400f Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 1 Dec 2023 10:47:33 +0000 Subject: [PATCH] add messages example --- examples/messages.ps1 | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/messages.ps1 diff --git a/examples/messages.ps1 b/examples/messages.ps1 new file mode 100644 index 0000000..8d04dc1 --- /dev/null +++ b/examples/messages.ps1 @@ -0,0 +1,38 @@ +[cmdletbinding()] +param() + +Import-Module ../lib/Q3Rcon.psm1 + +Function Get-ConnFromPSD1 { + $configpath = Join-Path $PSScriptRoot "config.psd1" + return Import-PowerShellDataFile -Path $configpath +} + +Function Get-DadJoke { + Invoke-WebRequest -Uri "https://icanhazdadjoke.com" -Headers @{accept = "application/json" } | Select-Object -ExpandProperty Content | ConvertFrom-Json | Select-Object -ExpandProperty Joke +} + +Function Send-Message { + param($rcon) + + $msg = Get-DadJoke + $msg | Write-Debug + $rcon.Say($msg) +} + + +try { + $conn = Get-ConnFromPSD1 + $rcon = Connect-Rcon -hostname $conn.host -port $conn.port -passwd $conn.passwd + + $stopWatch = [system.diagnostics.stopwatch]::StartNew() + $timeSpan = New-TimeSpan -Seconds 30 + do { + Send-Message -rcon $rcon + Start-Sleep -Seconds 10 + } until ($stopWatch.Elapsed -ge $timeSpan) + +} +finally { + Disconnect-Rcon -rcon $rcon +}