Principais conclusões
- Criação automatizada de atalhos: O script do PowerShell automatiza a criação de atalhos RDP, economizando tempo e reduzindo erros manuais.
- Opções personalizáveis: Ele oferece ampla personalização, incluindo configurações de exibição, credenciais de usuário e configurações de gateway.
- Escalabilidade para ambientes grandes: Ideal para MSPs e departamentos de TI que gerenciam várias conexões remotas.
- Requisito de direitos administrativos: Requer privilégios administrativos para determinadas operações, garantindo a segurança.
- Gerenciamento de conflitos: Inclui verificações de opções conflitantes, orientando os usuários para a configuração ideal.
- Consciente da segurança: Prioriza o manuseio seguro de credenciais e a implementação de atalhos.
- Produtividade aprimorada: Simplifica o processo de gerenciamento de desktops remotos, aumentando a produtividade geral da TI.
- Ferramentas complementares: Pode ser efetivamente combinado com plataformas de gerenciamento como o NinjaOne para soluções abrangentes de TI.
Os atalhos do protocolo de área de trabalho remota (RDP) são ferramentas vitais no mundo da TI, permitindo a conectividade perfeita com sistemas remotos. Com a evolução dos ambientes em rede e a crescente necessidade de acesso remoto eficiente, a capacidade de criar e gerenciar rapidamente esses atalhos se torna essencial. Esse script, criado no PowerShell, oferece uma abordagem simplificada e personalizável para a criação de atalhos RDP, aumentando a produtividade dos profissionais de TI e dos provedores de serviços gerenciados (MSPs).
Histórico e importância do script do PowerShell
Tradicionalmente, a criação e o gerenciamento de atalhos RDP envolviam processos manuais, que podiam ser demorados e propensos a erros, especialmente em ambientes de TI dinâmicos ou de grande escala. Esse script do PowerShell aborda esse desafio automatizando a criação de atalhos de desktop RDP com parâmetros definidos pelo usuário. Essa automação é especialmente benéfica para as equipes de TI que gerenciam várias conexões remotas e para os MSPs que precisam implementar rapidamente soluções de acesso remoto para seus clientes.
O roteiro:
<#
.SYNOPSIS
This script will create an rdp desktop shortcut with your specified options. It can create a shortcut for all users (including new ones) or existing ones only.
.DESCRIPTION
This script will create an rdp desktop shortcut with your specified options.
It can create a shortcut for all users (including new ones) or existing ones only.
.EXAMPLE
To Create a windowed RDP Shortcut simply specify the size, the name of the shortcut and which users the shortcut is for. You can also specify "MultiMon" for multi-monitor support. Or a gateway to use.
PS C:> ./Create-DesktopShortcut.ps1 -Name "Test" -RDPTarget "SRV19-TEST" -RDPUser "TESTjsmith" -Width "1920" -Height "1080" -AllExistingUsers -ExcludeUsers "ChrisWashington,JohnLocke"
Creating Shortcut at C:UsersJohnSmithDesktopTest.rdp
.PARAMETER NAME
Name of the shortcut ex. "Login Portal".
.PARAMETER RDPtarget
IP Address or DNS Name and port to the RDS Host ex. "TEST-RDSH:28665".
.PARAMETER RDPuser
Username to autofill in username field.
.PARAMETER AlwaysPrompt
Always Prompt for credentials.
.PARAMETER Gateway
IP Address or DNS Name and port of the RD Gateway ex. "TESTrdp.example.com:4433".
.PARAMETER SeperateGateWayCreds
If the RDS Gateway uses different creds than the Session Host use this parameter.
.PARAMETER FullScreen
RDP Shortcut should open window in 'FullScreen' mode.
.PARAMETER MultiMon
RDP Shortcut should open window with Multi-Monitor Support enabled.
.PARAMETER Width
Width of RDP Window should open ex. "1920".
.PARAMETER Height
Height of RDP Window shortcut should open ex. "1080".
.PARAMETER AllExistingUsers
Create the Shortcut for all existing users but not new users ex. C:Users*Desktopshortcut.lnk.
.PARAMETER ExcludeUsers
Comma seperated list of users to exclude from shortcut placement.
.PARAMETER AllUsers
Create the Shortcut in C:UsersPublicDesktop.
.OUTPUTS
None
.NOTES
Minimum OS Architecture Supported: Windows 7, Windows Server 2008
Release Notes: Renamed script, Split script into three, added Script Variable support, fixed bugs in RDP Shortcut
By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider.
Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#>
[CmdletBinding()]
param (
[Parameter()]
[String]$Name,
[Parameter()]
[String]$RDPtarget,
[Parameter()]
[String]$RDPuser,
[Parameter()]
[Switch]$AlwaysPrompt = [System.Convert]::ToBoolean($env:alwaysPromptForRdpCredentials),
[Parameter()]
[String]$Gateway,
[Parameter()]
[Switch]$SeparateGateWayCreds = [System.Convert]::ToBoolean($env:separateRdpGatewayCredentials),
[Parameter()]
[Switch]$FullScreen,
[Parameter()]
[Switch]$MultiMon,
[Parameter()]
[Int]$Width,
[Parameter()]
[Int]$Height,
[Parameter()]
[Switch]$AllExistingUsers,
[Parameter()]
[Switch]$AllUsers
)
begin {
# Replace existing params with form variables if they're used.
if ($env:shortcutName -and $env:shortcutName -notlike "null") { $Name = $env:shortcutName }
if ($env:createTheShortcutFor -and $env:createTheShortcutFor -notlike "null") {
if ($env:createTheShortcutFor -eq "All Users") { $AllUsers = $True }
if ($env:createTheShortcutFor -eq "All Existing Users") { $AllExistingUsers = $True }
}
if ($env:rdpServerAddress -and $env:rdpServerAddress -notlike "null") { $RDPtarget = $env:rdpServerAddress }
if ($env:rdpUsername -and $env:rdpUsername -notlike "null") { $RDPuser = $env:rdpUsername }
if ($env:rdpGatewayServerAddress -and $env:rdpGatewayServerAddress -notlike "null") { $Gateway = $env:rdpGatewayServerAddress }
if ($env:rdpWindowSize -and $env:rdpWindowSize -notlike "null") {
if ($env:rdpWindowSize -eq "Fullscreen Multiple Monitor Mode") { $MultiMon = $True }
if ($env:rdpWindowSize -eq "Fullscreen") { $FullScreen = $True }
}
if ($env:customRdpWindowWidth -and $env:customRdpWindowWidth -notlike "null") { $Width = $env:customRdpWindowWidth }
if ($env:customRdpWindowHeight -and $env:customRdpWindowHeight -notlike "null") { $Height = $env:customRdpWindowHeight }
# Output warnings for conflicting options.
if (($Width -and -not $Height ) -or ($Height -and -not $Width)) {
Write-Warning "You forgot to include both the width and height. RDP Window will be in fullscreen mode."
}
if (($Width -or $Height) -and ($FullScreen -or $MultiMon)) {
if ($MultiMon) {
Write-Warning "Conflicting Display Option selected. Using Fullscreen Multi-monitor."
}
else {
Write-Warning "Conflicting Display Option selected. Using Fullscreen."
}
}
# Double-check that a user is specified for shortcut creation.
if (-not $AllUsers -and -not $AllExistingUsers -and -not $User) {
Write-Error "You must specify which desktop to create the shortcut on!"
exit 1
}
# Double-check that a shortcut name was provided.
if (-not $Name -or -not $RDPtarget) {
Write-Error "You must specify a name and target for the shortcut!"
exit 1
}
# Creating a shortcut at C:UsersPublicDesktop requires admin rights.
function Test-IsElevated {
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
$p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
if (!(Test-IsElevated)) {
Write-Error -Message "Access Denied. Please run with Administrator privileges."
exit 1
}
# Retrieve all registry paths for actual users (excluding system or network service accounts).
function Get-UserHives {
param (
[Parameter()]
[ValidateSet('AzureAD', 'DomainAndLocal', 'All')]
[String]$Type = "All",
[Parameter()]
[String[]]$ExcludedUsers,
[Parameter()]
[switch]$IncludeDefault
)
# User account SIDs follow a particular pattern depending on whether they're Azure AD, Domain, or local "workgroup" accounts.
$Patterns = switch ($Type) {
"AzureAD" { "S-1-12-1-(d+-?){4}$" }
"DomainAndLocal" { "S-1-5-21-(d+-?){4}$" }
"All" { "S-1-12-1-(d+-?){4}$" ; "S-1-5-21-(d+-?){4}$" }
}
# We'll need the NTuser.dat file to load each users registry hive. So we grab it if their account sid matches the above pattern.
$UserProfiles = Foreach ($Pattern in $Patterns) {
Get-ItemProperty "HKLM:SOFTWAREMicrosoftWindows NTCurrentVersionProfileList*" |
Where-Object { $_.PSChildName -match $Pattern } |
Select-Object @{Name = "SID"; Expression = { $_.PSChildName } },
@{Name = "UserHive"; Expression = { "$($_.ProfileImagePath)NTuser.dat" } },
@{Name = "UserName"; Expression = { "$($_.ProfileImagePath | Split-Path -Leaf)" } },
@{Name = "Path"; Expression = { $_.ProfileImagePath } }
}
# In some cases, it's necessary to retrieve the .Default user's information.
switch ($IncludeDefault) {
$True {
$DefaultProfile = "" | Select-Object UserName, SID, UserHive, Path
$DefaultProfile.UserName = "Default"
$DefaultProfile.SID = "DefaultProfile"
$DefaultProfile.Userhive = "$env:SystemDriveUsersDefaultNTUSER.DAT"
$DefaultProfile.Path = "C:UsersDefault"
$DefaultProfile | Where-Object { $ExcludedUsers -notcontains $_.UserName }
}
}
$UserProfiles | Where-Object { $ExcludedUsers -notcontains $_.UserName }
}
}
process {
$ShortcutPath = New-Object System.Collections.Generic.List[String]
# Create the filenames for the path.
if ($RDPTarget) { $File = "$Name.rdp" }
# Build the paths and add them to the ShortcutPath list.
if ($AllUsers) { $ShortcutPath.Add("$env:PublicDesktop$File") }
if ($AllExistingUsers) {
$UserProfiles = Get-UserHives
# Loop through each user profile
$UserProfiles | ForEach-Object { $ShortcutPath.Add("$($_.Path)Desktop$File") }
}
if ($User) {
$UserProfile = Get-UserHives | Where-Object { $_.Username -like $User }
$ShortcutPath.Add("$($UserProfile.Path)Desktop$File")
}
$RDPFile = New-Object System.Collections.Generic.List[String]
# Base template of an .RDP file. Additional options will be appended based on user selection.
$Template = @"
session bpp:i:32
compression:i:1
keyboardhook:i:2
audiocapturemode:i:0
videoplaybackmode:i:1
connection type:i:7
networkautodetect:i:1
bandwidthautodetect:i:1
displayconnectionbar:i:1
enableworkspacereconnect:i:0
disable wallpaper:i:0
allow font smoothing:i:0
allow desktop composition:i:0
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
disable cursor setting:i:0
bitmapcachepersistenable:i:1
audiomode:i:0
redirectprinters:i:1
redirectcomports:i:0
redirectsmartcards:i:1
redirectwebauthn:i:1
redirectclipboard:i:1
redirectposdevices:i:0
autoreconnection enabled:i:1
authentication level:i:2
negotiate security layer:i:1
remoteapplicationmode:i:0
alternate shell:s:
shell working directory:s:
gatewaycredentialssource:i:4
gatewaybrokeringtype:i:0
use redirection server name:i:0
rdgiskdcproxy:i:0
kdcproxyname:s:
enablerdsaadauth:i:0
"@
$RDPFile.Add($Template)
# This will generate the actual .rdp file
$ShortcutPath | ForEach-Object {
$RDPFile.Add("full address:s:$RDPTarget")
$RDPFile.Add("gatewayhostname:s:$Gateway")
if ($Width) { $RDPFile.Add("desktopwidth:i:$Width") }
if ($Height) { $RDPFile.Add("desktopheight:i:$Height") }
if ($MultiMon) { $RDPFile.Add("use multimon:i:1") }else { $RDPFile.Add("use multimon:i:0") }
if ($FullScreen -or $MultiMon -or !$Height -or !$Width) { $RDPFile.Add("screen mode id:i:2") }else { $RDPFile.Add("screen mode id:i:1") }
if ($AlwaysPrompt) { $RDPFile.Add("prompt for credentials:i:1") }else { $RDPFile.Add("prompt for credentials:i:0") }
if ($Gateway) { $RDPFile.Add("gatewayusagemethod:i:2") }else { $RDPFile.Add("gatewayusagemethod:i:4") }
if ($SeparateGateWayCreds) {
$RDPFile.Add("promptcredentialonce:i:0")
$RDPFile.Add("gatewayprofileusagemethod:i:1")
}
else {
$RDPFile.Add("promptcredentialonce:i:1")
if ($Gateway) { $RDPFile.Add("gatewayprofileusagemethod:i:0") }
}
if ($RDPUser) { $RDPFile.Add("username:s:$RDPUser") }
Write-Host "Creating Shortcut at $_"
$RDPFile | Out-File $_
if (!(Test-Path $_ -ErrorAction SilentlyContinue)) {
Write-Error "Unable to create Shortcut at $_"
exit 1
}
}
exit 0
}end {
}
Acesse mais de 300 scripts no NinjaOne Dojo
Explorando o roteiro: Uma análise passo a passo
O script começa com uma sinopse e uma descrição, fornecendo uma visão geral clara de sua funcionalidade. Em seguida, ele descreve parâmetros como o nome do atalho, o servidor RDP de destino, as credenciais do usuário, as opções de exibição (como tela cheia ou suporte a vários monitores) e a opção de criar atalhos para todos os usuários ou para usuários específicos.
- Definição de parâmetros: O script define vários parâmetros que permitem a personalização do atalho RDP. Isso inclui detalhes básicos, como o nome do atalho, o destino do RDP e o usuário, bem como opções mais avançadas, como tamanho da janela, suporte a vários monitores e configuração do gateway.
- Variáveis de ambiente e verificação de conflitos: O script verifica e substitui os parâmetros por variáveis de ambiente, se disponíveis. Ele também emite avisos no caso de opções conflitantes, como a especificação de tela cheia e tamanho da janela.
- Manuseio de direitos administrativos e perfis de usuário: O script inclui uma função para verificar se há direitos administrativos, necessários para criar atalhos em determinados caminhos de diretório. Ele também contém uma função para recuperar perfis de usuário, excluindo contas de sistema, o que é crucial para a implementação de atalhos em áreas de trabalho de usuários específicos.
- Processo de criação de atalhos: O script gera o arquivo .RDP, anexando as opções selecionadas pelo usuário a um modelo básico. Em seguida, ele percorre os caminhos de atalho determinados, criando o arquivo RDP em cada local.
Casos de uso em potencial: Um aplicativo do mundo real
Imagine um MSP responsável por gerenciar o acesso remoto de uma empresa com vários departamentos, cada um exigindo configurações específicas de RDP. Com esse script, o MSP pode gerar rapidamente atalhos RDP personalizados para cada departamento, implementando-os nos respectivos desktops dos usuários de forma eficiente e reduzindo significativamente o tempo de configuração manual.
Análise comparativa: Script vs. métodos tradicionais
Em comparação com a criação manual de atalhos RDP, esse script oferece vantagens significativas em termos de escalabilidade, personalização e redução de erros. Embora os métodos manuais possam ser suficientes para configurações individuais, eles se tornam impraticáveis em ambientes maiores e mais dinâmicos, nos quais esse script pode gerenciar configurações complexas sem esforço.
Implicações e considerações sobre segurança
Embora esse script simplifique significativamente a criação de atalhos RDP, é essencial considerar as implicações de segurança. O manuseio adequado das credenciais e a garantia de que os atalhos sejam implementados apenas para usuários autorizados são aspectos cruciais. Além disso, os MSPs devem revisar regularmente as configurações de atalhos para manter os padrões de segurança.
Práticas recomendadas para o uso de scripts
- Testes minuciosos: Antes de uma implementação generalizada, teste completamente o script em um ambiente controlado.
- Atualizações regulares: Mantenha o script atualizado com as alterações na infraestrutura de rede.
- Verificações de segurança: Examine regularmente as implementações de atalhos para verificar se há vulnerabilidades de segurança.
Conclusão: Aprimoramento do gerenciamento de área de trabalho remota com o NinjaOne
Em conclusão, esse script do PowerShell é uma ferramenta avançada para criar e gerenciar atalhos RDP, oferecendo personalização e eficiência. Ferramentas como o NinjaOne complementam esses scripts, fornecendo uma plataforma unificada para o gerenciamento de operações de TI, incluindo conexões de área de trabalho remota, simplificando assim as tarefas de gerenciamento de TI e aumentando a produtividade e a segurança gerais em cenários de trabalho remoto.