. .\NewXMLDocument.ps1

$rdp_domainname = "MicrosoftAccount"
$rdp_username = "fred"
$ssh_username = "fred"
$vnc_username = "fred"

$rdp_password = "changeme!"
$ssh_password = "changeme!"
$vnc_password = "changeme!"

$Network = "192.168.1."


$NetworkAddresses = 1..254 | ForEach-Object {
    @{
        ComputerName = "$($Network)$($_)"
    }
}

$Connections = $NetworkAddresses | ForEach-Object {
    if (Test-Connection -ComputerName $_ -Count 1 -TimeToLive 10) {
        if ((Test-NetConnection -ComputerName $_ -Port 22).TcpTestSucceeded) {
            $ssh = $true
        }
        if ((Test-NetConnection -ComputerName $_ -Port 5900).TcpTestSucceeded) {
            $vnc = $true
        }
        if ((Test-NetConnection -ComputerName $_ -Port 3389).TcpTestSucceeded) {
            $rdp = $true
        }
        [PSCustomObject]@{
            'address' = $_
            'ssh'     = $ssh
            'vnc'     = $vnc
            'rdp'     = $rdp
        }
    }
}

## used for testing
# $Connections = @(
#     [PSCustomObject]@{
#         'address' = "192.168.1.1"
#         'ssh'     = $true
#         'vnc'     = $false
#         'rdp'     = $false
#     }
#     [PSCustomObject]@{
#         'address' = "192.168.1.2"
#         'ssh'     = $false
#         'vnc'     = $false
#         'rdp'     = $true
#     }
#     [PSCustomObject]@{
#         'address' = "192.168.1.3"
#         'ssh'     = $true
#         'vnc'     = $false
#         'rdp'     = $true
#     }
# )


$xml = New-XmlDocument -ScriptBlock {
    user-mapping {
        authorize {
            username = $ssh_username
            password = $ssh_password
            $Connections | ForEach-Object {
                $Address = $_.address
                if ($_.rdp) {
                    connection {
                        name = "$([System.Net.Dns]::gethostentry($Address)) Desktop(RDP)"
                        #name = "$Address Desktop(RDP)" # used for testing
                        protocol {
                            'rdp'
                        }
                        param {
                            name = "hostname"
                            "$Address"
                        }
                        param {
                            name = "security"
                            'nla'
                        }
                        param {
                            name = "ignore-cert"
                            'true'
                        }
                        param {
                            name = "domain"
                            "$rdp_domainname"
                        }
                        param {
                            name = "username"
                            '${GUAC_USERNAME}'
                        }
                        param {
                            name = "password"
                            '${GUAC_PASSWORD}'
                        }
                    }
                }
                elseif ($_.vnc) {
                    connection {
                        name = "$([System.Net.Dns]::gethostentry($Address)) Desktop(VNC)"
                        #name = "$Address Desktop(VNC)" # used for testing
                        protocol {
                            'vnc'
                        }
                        param {
                            name = "hostname"
                            "$Address"
                        }
                        param {
                            name = "username"
                            '${GUAC_USERNAME}'
                        }
                        param {
                            name = "password"
                            '${GUAC_PASSWORD}'
                        }
                    }
                }
                if ($_.ssh) {
                    connection {
                        name = "$([System.Net.Dns]::gethostentry($Address)) Terminal"
                        #name = "$Address Terminal" # used for testing
                        protocol {
                            'ssh'
                        }
                        param {
                            name = "hostname"
                            "$Address"
                        }
                        param {
                            name = "port"
                            '22'
                        }
                        param {
                            name = "username"
                            '${GUAC_USERNAME}'
                        }
                        param {
                            name = "password"
                            '${GUAC_PASSWORD}'
                        }
                    }
                }
            }
        }
    }
}

## For testing
#$xml.ToString()

# remove old user mappings
Remove-Item "/etc/guacamole/user-mappings.xml" -Force

# Output $xml to "/etc/guacamole/user-mappings.xml"
$xml.Save("/etc/guacamole/user-mappings.xml")