PowerShell_Scripts/xserver/Invoke-XApp.ps1

107 lines
3.3 KiB
PowerShell
Raw Normal View History

<#
.SYNOPSIS
Invoke a graphical application on X-Server.
.DESCRIPTION
Invoke a graphical application, making sure an X server connection is active, start the x-server if needed, and if the application is remote, make sure the remote session has the correct DISPLAY values set.
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
.EXAMPLE
Invoke-XApp xclock
.EXAMPLE
Invoke-XApp xclock -Remote remote@example.com
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
#[OutputType([String])]
# # Param2 help description
# [Parameter(ParameterSetName='Parameter Set 1')]
# [AllowNull()]
# [AllowEmptyCollection()]
# [AllowEmptyString()]
# [ValidateScript({$true})]
# [ValidateRange(0,5)]
# [int]
# $Param2,
# # Param3 help description
# [Parameter(ParameterSetName='Another Parameter Set')]
# [ValidatePattern("[a-z]*")]
# [ValidateLength(0,15)]
# [String]
# $Param3
Param (
# Param1 help description
[Parameter(Mandatory, Position = 0, ParameterSetName = 'Parameter Set 1')]
[string[]]$Command,
[string[]]$Remote,
[ValidateSet('pwsh', 'bash', 'zsh', 'sh')]
[string]$RemoteShell = 'pwsh',
[string]$PrivateKeyPath
)
begin {
$null = Get-Command 'xlaunch' -ErrorAction Stop
if ( -not $(Get-Process -ProcessName 'vcxsrv' -ErrorAction SilentlyContinue) ) {
$local:LaunchCfg = $env:XLAUNCH_CONFIG
if ( -not $env:XLAUNCH_CONFIG ) {
New-TemporaryFile | Tee-Object -Variable 'LaunchCfg' | Remove-Item
}
Write-Verbose "LaunchCfg=$LaunchCfg"
if ( -not (Test-Path $LaunchCfg) ) {
Write-Verbose "Creating missing $LaunchCfg..."
New-Item -ItemType File -Path $LaunchCfg -Value @(
'<?xml version="1.0" encoding="UTF-8"?>'
, '<XLaunch '
, ' WindowMode="MultiWindow"'
, ' ClientMode="NoClient"'
, ' LocalClient="False"'
, ' Display="-1"'
, ' LocalProgram=""'
, ' RemoteProgram=""'
, ' RemotePassword=""'
, ' PrivateKey=""'
, ' RemoteHost=""'
, ' RemoteUser=""'
, ' XDMCPHost=""'
, ' XDMCPBroadcast="False"'
, ' XDMCPIndirect="False"'
, ' Clipboard="True"'
, ' ClipboardPrimary="True"'
, ' ExtraParams=""'
, ' Wgl="True"'
, ' DisableAC="False"'
, ' XDMCPTerminate="False"'
, '/>' )
}
& xlaunch --run $LaunchCfg
}
}
process {
if ( -not "Command" ) {
Write-Warning "No command supplied, exiting."
return
}
if ($pscmdlet.ShouldProcess("Target", "Operation")) {
if ( $Remote ) {
$local:RemoteCommand = "bash", "-c", "'DISPLAY=`"localhost:`"`$( xauth list | awk `"{ print `$1 }`" | cut -d: -f2 | tail -1 ) $Command'"
& ssh -i $PrivateKeyPath $Remote $RemoteCommand
}
}
}
end {
}