q3rcon-ps/examples/cli.ps1

43 lines
1022 B
PowerShell
Raw Permalink Normal View History

2023-11-29 16:18:03 +00:00
[cmdletbinding()]
param()
Import-Module ../lib/Q3Rcon.psm1
Function Read-HostUntilEmpty {
param([object]$rcon)
"Input 'Q' or <Enter> to exit."
while (($line = Read-Host -Prompt "Send command") -cne [string]::Empty) {
if ($line -eq "Q") {
break
}
if ($line -in @("fast_restart", "map_rotate", "map_restart")) {
$cmd = $line -replace '(?:^|_)(\p{L})', { $_.Groups[1].Value.ToUpper() }
2023-11-30 09:44:34 +00:00
$rcon.$cmd()
}
elseif ($line.StartsWith("map mp_")) {
$mapname = $line.Split()[1]
$rcon.SetMap($mapname)
2023-11-29 16:18:03 +00:00
}
else {
2023-11-30 09:44:34 +00:00
$rcon.Send($line)
2023-11-29 16:18:03 +00:00
}
}
}
Function Get-ConnFromPSD1 {
$configpath = Join-Path $PSScriptRoot "config.psd1"
return Import-PowerShellDataFile -Path $configpath
}
try {
$conn = Get-ConnFromPSD1
$rcon = Connect-Rcon -hostname $conn.host -port $conn.port -passwd $conn.passwd
Read-HostUntilEmpty -rcon $rcon
}
finally {
Disconnect-Rcon -rcon $rcon
}