Windows RemoteApp integration
Allow user to access windows-based applications remotely through SkyForm AIP.

Both AD server and RemoteApp server are tested on Windows Server 2016
===================================================

=====Prerequisite
1. Configure AD server; all windows hosts and SkyForm AIP Linux hosts have joined into the same domain.
   Refer to "AD+Samba.docx"
2. Deploy RemoteApp on windows hosts. Refer to "remoteapp.docx"   
3. Windows hosts have installed SkyformAIP as client host
4. Install OpenSSH on all windows hosts.  Refer to https://www.server-world.info/en/note?os=Windows_Server_2016&p=openssh&f=1
(1) Download OpenSSH-Win64.zip from https://github.com/PowerShell/Win32-OpenSSH/releases
(2) Extract ZIP archive and put the folder to any Path
(3) Add SSH commands' PATH to Environment Variables
(4) Run [Powershell] and move to OpenSSH folder you located. Next, run a command [.\install-sshd.ps1] to install sshd service. If successfully message is shown, the installation is OK.
(5) On the Services screen, [OpenSSH SSH Server] has been added. Start it and turn to [Automatic] for [Startup Type].
(6) Back to PowerShell and move to OpenSSH installed folder, then run [.\FixHostFilePermission.ps1].
(7) If Windows firewall is running, allow 22/TCP port like follows.
5. Passwordless ssh login from AIP master host to all windows hosts. 
   For example, "emma" is an admin domain user that will be used for remote ssh
(1) on master host:
# su emma; cd ~/.ssh/; ssh-keygen
# scp id_rsa.pub to windows host C:\Users\emma\.ssh\id_rsa.pub
(2) on Windows host:
# New-Item -ItemType file "C:\Users\emma\.ssh\authorized_keys"
# Append C:\Users\emma\.ssh\id_rsa.pub to C:\Users\emma\.ssh\authorized_keys  
(3) By default setting of OpenSSH on Windows, only [Administrators] group is configured as that [authorized_keys] file is not the default location of OpenSSH like follows, but if you'd like to set it on default location for all users, it needs to comment out these 2 lines in the Conf file [C:\ProgramData\ssh\sshd_config]:
Match Group administrators
      AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
(4) On the Services screen, [OpenSSH Authentication Agent] has been added. Start it and turn to [Automatic] for [Startup Type].
   
=====Add windows hosts as AIP Agentless hosts

- Master Host

1. Edit $CB_ENVDIR/cb.yaml
   a. Add windows hosts to cb.yaml. In hosts section: add
      -	name: hostx
        attr: remote
      -	name: hosty
        attr: remote
   b. Add a queue "win" with windows host list. In queues section: add
      - name: win
        hosts: hostx hosty
   c. Add domain user that runs service "SkyformaipSvc" (e.g., skycloud\administrator) as AIP administrator.
      administrators:
        - administrator  
   d. Add a limit in limits section for win queue: 1 slot/per_host/per_user
    limits:
     - name: winJobLimit
       per_queue: win
       per_host: all
       per_user: all
       slots: 1

2. Copy cble.remote to /opt/skyformai/sbin/. Edit following variables in cbls.remote if necessary.
        winqueue = "win"  # windows queue in cb.yaml
        aipcmd = "/opt/skyformai/bin/aip"   # aip command path
        aduser = "emma"   # ad user; must be admin
        keyfile = "/home/emma/.ssh/id_rsa"  # ssh private key
3. Restart AIP service        

- Each Windows host

Create windows service "SkyformaipSvc"
   a. By default, windows service run as "nt authority\system". We need configure 
      the service to run as a domain user (e.g., skycloud\administrator). 
      The user must have the "Log on as a service" right. To give him that right,
      open the Local Security Policy management console, go to the 
      "\Security Settings\Local Policies\User Rights Assignments" folder, and edit
      the "Log on as a service" policy there.
    b. Before setup the service, edit parameters in SkyformaipSvc.ps1 if necessary
       #################### SkyForm AIP Configuration #################################
       $period = 5                 # cycle period in seconds
       $jobIdleThreshold =  1      # job idle threshold in minutes
       $detectIdleAfter = 2        # detect job idle after job running for x minutes
       $queue = "win"              # monitor queue
       #################################################################################    
    c. Add PowerShell' PATH "C:\Windows\System32\WindowsPowerShell\v1.0" to Environment Variables
    d. Run SkyformaipSvc.ps1 to setup and start service
       # $cred = Get-Credential -UserName skycloud\administrator
       # SkyformaipSvc.ps1 -Setup -Credential $cred    
       # SkyformaipSvc.ps1 -Status
       Not installed
       # SkyformaipSvc.ps1 -Setup
       # SkyformaipSvc.ps1 -Status
       Stopped
       # SkyformaipSvc.ps1 -Start
       # SkyformaipSvc.ps1 -Status
       Running   
    e. Log file location: C:\Windows\Logs\SkyFormAIP\SkyformaipSvc.log
    f. To uninstall the service, you may run:
       # SkyformaipSvc.ps1 -Stop
       # SkyformaipSvc.ps1 -Status
       Stopped
       # SkyformaipSvc.ps1 -Remove
       # SkyformaipSvc.ps1 -Status
       Not installed    

=== Submit job

1. remote desktop

# rdpsub -q win desktop
Job 208 has been submitted to the queue [win].
# cread 208
Messages posted to jobID 208
POST_TIME: Jul 30 05:30:46  MESSAGE: rdp:
redirectclipboard:i:1
redirectprinters:i:1
redirectcomports:i:1
redirectsmartcards:i:1
devicestoredirect:s:*
drivestoredirect:s:*
redirectdrives:i:1
session bpp:i:32
prompt for credentials on client:i:0
promptcredentialonce:i:0
span monitors:i:1
use multimon:i:1
server port:i:3389
allow font smoothing:i:1
gatewayhostname:s:
gatewayusagemethod:i:4
gatewaycredentialssource:i:2
gatewayprofileusagemethod:i:0
gatewaybrokeringtype:i:0
use redirection server name:i:0
full address:s:node1
remoteapplicationmode:i:0

2. remote application (application "calc" must be same with published RemoteApp program)

# ./rdpsub -q win calc
Job 209 has been submitted to the queue [medium].
# cread 209
Messages posted to jobID 209
POST_TIME: Jul 30 05:31:57  MESSAGE: rdp:
redirectclipboard:i:1
redirectprinters:i:1
redirectcomports:i:1
redirectsmartcards:i:1
devicestoredirect:s:*
drivestoredirect:s:*
redirectdrives:i:1
session bpp:i:32
prompt for credentials on client:i:0
promptcredentialonce:i:0
span monitors:i:1
use multimon:i:1
server port:i:3389
allow font smoothing:i:1
gatewayhostname:s:
gatewayusagemethod:i:4
gatewaycredentialssource:i:2
gatewayprofileusagemethod:i:0
gatewaybrokeringtype:i:0
use redirection server name:i:0
full address:s:node1
remoteapplicationmode:i:1
alternate shell:s:||calc
remoteapplicationprogram:s:||calc
remoteapplicationcmdline:s:||calc
remoteapplicationname:s:calc
