|
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 |
|
} |