ken_nogi/PAD/Create-RdpShortcuts.ps1
Kenichiro NOGI 88a402ce0f up
2026-07-10 18:13:30 +09:00

107 lines
3.5 KiB
PowerShell

#requires -Version 5.1
<#
Generate .rdp files for each PC from clientpc.csv.
Output directory defaults to .\RDP under the current directory.
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[string]$ClientPcCsvPath = '.\clientpc.csv',
[string]$RdpOutputDirectory = '.\RDP',
[int]$DesktopWidth = 1920,
[int]$DesktopHeight = 1080
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function New-RdpShortcutFiles {
param(
[Parameter(Mandatory)] [string]$CsvPath,
[Parameter(Mandatory)] [string]$OutputDirectory,
[Parameter(Mandatory)] [int]$Width,
[Parameter(Mandatory)] [int]$Height
)
$resolvedCsvPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($CsvPath)
$resolvedOutputDirectory = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputDirectory)
if (-not (Test-Path -LiteralPath $resolvedCsvPath -PathType Leaf)) {
throw ("CSV file not found: {0}" -f $resolvedCsvPath)
}
if (-not (Test-Path -LiteralPath $resolvedOutputDirectory -PathType Container)) {
if ($PSCmdlet.ShouldProcess($resolvedOutputDirectory, 'Create RDP output directory')) {
New-Item -Path $resolvedOutputDirectory -ItemType Directory -Force | Out-Null
}
}
$clients = $null
foreach ($enc in @('UTF8', 'Default', 'Unicode', 'OEM')) {
try {
$tryRows = Import-Csv -LiteralPath $resolvedCsvPath -Encoding $enc
if ($tryRows -and $tryRows.Count -gt 0) {
$clients = $tryRows
break
}
}
catch {
continue
}
}
if (-not $clients -or $clients.Count -eq 0) {
throw 'No rows were found in CSV.'
}
$columnNames = $clients[0].PSObject.Properties.Name
if ($columnNames.Count -lt 2) {
throw 'CSV must contain at least two columns: computer name and login id.'
}
$computerColumn = $columnNames[0]
$loginIdColumn = $columnNames[1]
$createdCount = 0
foreach ($client in $clients) {
$computerName = [string]$client.$computerColumn
$loginId = [string]$client.$loginIdColumn
if ([string]::IsNullOrWhiteSpace($computerName)) {
Write-Warning 'Skipped a row because computer name is empty.'
continue
}
$rdpFilePath = Join-Path -Path $resolvedOutputDirectory -ChildPath ("{0}.rdp" -f $computerName)
$rdpContent = @(
'screen mode id:i:2',
'use multimon:i:0',
("desktopwidth:i:{0}" -f $Width),
("desktopheight:i:{0}" -f $Height),
'session bpp:i:32',
'prompt for credentials:i:1',
'administrative session:i:0',
'redirectclipboard:i:1',
'audiomode:i:0',
'authentication level:i:2',
'enablecredsspsupport:i:1',
("full address:s:{0}" -f $computerName),
("username:s:{0}" -f $loginId)
)
if ($PSCmdlet.ShouldProcess($rdpFilePath, 'Create RDP shortcut file')) {
Set-Content -LiteralPath $rdpFilePath -Value $rdpContent -Encoding Unicode
$createdCount++
}
}
return [PSCustomObject]@{
OutputDirectory = $resolvedOutputDirectory
Count = $createdCount
}
}
$result = New-RdpShortcutFiles -CsvPath $ClientPcCsvPath -OutputDirectory $RdpOutputDirectory -Width $DesktopWidth -Height $DesktopHeight
Write-Host ("Created RDP files: {0} / Output: {1}" -f $result.Count, $result.OutputDirectory) -ForegroundColor Green