Skip to content

Instantly share code, notes, and snippets.

@romero126
Last active July 24, 2024 17:38
Show Gist options
  • Save romero126/1563474a975d53c8159a5ea851dd5dc5 to your computer and use it in GitHub Desktop.
Save romero126/1563474a975d53c8159a5ea851dd5dc5 to your computer and use it in GitHub Desktop.
Invoke-RemoteDesktopAppInstaller

Requirements

PSRemoting

Description

This script turns on RemoteApplication services and creates shortcuts on your startmenu for applications in your machine.

Import the script

. .\RemoteDesktopAppInstaller.ps1

# For any Microsoft Online account prefix the Domain 'MicrosoftAccount' to it
# Example: 
#    UserName MicrosoftAccount\username@outlook.com
#    Password <your password>
$creds = Get-Credential
$computerName = '<your hostname>'

# Create a PSSession with the remote host
$session = New-PSSession $computerName -Credential $creds -UseSSL -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
  
Invoke-RemoteDesktopAppInstaller $Session
function Invoke-RemoteDesktopAppInstaller
{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[System.Management.Automation.Runspaces.PSSession] $Session
)
$installShortcutDirectory = "$($env:USERPROFILE)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Remote App\"
$installResourceDirectory = "$($env:USERPROFILE)\AppData\Roaming\Microsoft\Workspaces\"
if (
$session.State -ne "Opened" -and
$session.Availability -ne "Available"
) {
throw "The remote session must be Available and its State Opened"
}
Write-Host "Installing Terminal Services"
$install_terminalServices = Invoke-Command -Session $session -ScriptBlock {
$_terminalServicesReg = Get-ItemProperty -Path "hklm:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services"
if ($_terminalServicesReg.fAllowUnlistedRemotePrograms -ne 1) {
$null = Set-ItemProperty -Path "hklm:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" -Name fAllowUnlistedRemotePrograms -Value 1
$null = New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\TSAppAllowList\Applications\" -ItemType Directory -ErrorAction SilentlyContinue
return 1
}
return 0
}
if ($install_terminalServices -eq 1) {
Write-Host "[Installed] Terminal Services Registry" -ForegroundColor Cyan
}
Write-Host "Cache Applications"
$rdpTemplate = Get-Content -Path "$PSScriptRoot\RemoteDesktopAppInstaller.rdp" -raw
Invoke-Command -Session $session -ScriptBlock {
$null = Remove-Item -Path "$env:TEMP\RemoteAppInstaller\" -Recurse -Force -ErrorAction SilentlyContinue
$null = New-Item -Path "$env:TEMP\RemoteAppInstaller\" -ItemType Directory
$remoteHost = $using:session.ComputerName
#$using:Session
# ($using:Session).ComputerName #
$computerSystem = Get-CIMInstance CIM_ComputerSystem
$appShell = New-Object -ComObject Shell.Application
$startMenu = $appShell.NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}')
$WSScriptObj = New-Object -ComObject ("WScript.Shell")
"Begin Loop"
foreach ($item in $startMenu.Items())
{
"LOOP"
$fileName = "$($item.Name) - ($($computerSystem.Name))"
# RDP File
$shortcutTargetPath = "shell:appsFolder\$($item.Path)"
$sourceFilePath = "$env:TEMP\RemoteAppInstaller\$fileName.rdp"
#$shortcutTargetPath = "$($using:installResourceDirectory)\$($computerSystem.Name)\$fileName.rdp"
Write-Host "Performing the operation ""Build RDP"" on Target: ""Item: $($item.Name) Destination: $sourceFilePath""."
Set-Content -Path $sourceFilePath -Value $ExecutionContext.InvokeCommand.ExpandString($using:rdpTemplate) -Force
# Create Shortcuts
$shortcutPath = "$env:TEMP\RemoteAppInstaller\$fileName.lnk"
Write-Verbose "Performing the operation ""Build Shortcut"" on Target: ""Item: $($item.Name) Destination: $shortcutPath""."
$shortcut = $WSScriptObj.CreateShortCut($shortcutPath)
$shortcut.TargetPath = "$($using:installResourceDirectory)\$($computerSystem.Name)\$fileName.rdp"
$shortcut.Save()
}
}
# Move RDP Files into location
$remoteComputerSystem, $remoteTemp = Invoke-Command -Session $session -ScriptBlock {
Get-CIMInstance CIM_ComputerSystem
$env:TEMP
}
$ProgressPreference = "SilentlyContinue"
Write-Host "Copying RDP Files to its destination"
$null = Remove-Item -Path "$($installResourceDirectory)\$($remoteComputerSystem.Name)\" -Recurse -Force -ErrorAction SilentlyContinue
$null = New-Item -Path "$($installResourceDirectory)\$($remoteComputerSystem.Name)\" -ItemType Directory
Copy-Item -FromSession $session -Path "$remoteTemp\RemoteAppInstaller\*.rdp" -Destination "$($installResourceDirectory)\$($remoteComputerSystem.Name)\" -Verbose:$VerbosePreference
Write-Host "Copying LNK Files to its destination"
$null = Remove-Item -Path "$($installShortcutDirectory)\$($remoteComputerSystem.Name)\" -Recurse -Force -ErrorAction SilentlyContinue
$null = New-Item -Path "$($installShortcutDirectory)\$($remoteComputerSystem.Name)\" -ItemType Directory
Copy-Item -FromSession $session -Path "$remoteTemp\RemoteAppInstaller\*.lnk" -Destination "$($installShortcutDirectory)\$($remoteComputerSystem.Name)\" -Verbose:$VerbosePreference
}
full address:s:$($remoteHost)
server port:i:3389
negotiate security layer:i:1
EnableCredSspSupport:i:1
allow font smoothing:i:1
allow desktop composition:i:1
audiocapturemode:i:1
audiomode:i:0
authentication level:i:2
autoreconnection enabled:i:1
bandwidthautodetect:i:1
compression:i:1
connection type:i:7
connect to console:i:1
devicestoredirect:s:*
drivestoredirect:s:*
disable wallpaper:i:0
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
disable cursor setting:i:0
displayconnectionbar:i:1
enableworkspacereconnect:i:0
gatewayusagemethod:i:4
gatewaycredentialssource:i:4
gatewayprofileusagemethod:i:0
gatewaybrokeringtype:i:0
keyboardhook:i:1
networkautodetect:i:1
prompt for credentials:i:0
promptcredentialonce:i:0
redirectprinters:i:1
redirectcomports:i:1
redirectsmartcards:i:1
redirectclipboard:i:1
redirectposdevices:i:0
screen mode id:i:2
session bpp:i:32
span monitors:i:1
use multimon:i:1
selectedmonitors:s:0,1
videoplaybackmode:i:1
use redirection server name:i:0
bitmapcachepersistenable:i:1
usbdevicestoredirect:s:1
disableremoteappcapscheck:i:1
alternate shell:s:rdpinit.exe
remoteapplicationmode:i:1
remoteapplicationname:s:$($item.Name)
remoteapplicationfile:s:$($shortcutTargetPath)
workspace id:s:$($item.Name)
loadbalanceinfo:s:tsv://MS Terminal Services Plugin.1.QuickSessionCollection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment