param()# Basic file logging to help diagnose scheduled task behavior$logDir = Join-Path$env:LOCALAPPDATA "RDPFileMonitor"if(-not(Test-Path$logDir)){New-Item-Path $logDir-ItemType Directory -Force |Out-Null}$logPath = Join-Path$logDir"monitor.log"# Emit a simple startup marker regardless of transcript support("Started: "+(Get-Date-Format 'yyyy-MM-dd HH:mm:ss'))|Out-File-FilePath (Join-Path$logDir'started.txt')-Append -Encoding utf8
try{Start-Transcript -Path $logPath-Append -ErrorAction SilentlyContinue |Out-Null}catch{}$downloadFolder = [Environment]::GetFolderPath("UserProfile")+"\Downloads"$targetRDPFileName = "your-rdp-filename.rdp"$processedFiles = @()Write-Host"Starting RDP file monitoring (polling method)"Write-Host"Log: $logPath"Write-Host"Target folder: $downloadFolder"Write-Host"Target file name: $targetRDPFileName"Write-Host"Checking every 2 seconds..."while($true){try{if(Test-Path$downloadFolder){$files = Get-ChildItem-Path $downloadFolder-Filter"*.rdp"-File
foreach($file in $files){if($file.Name -eq$targetRDPFileName-and$file.FullName -notin$processedFiles){Write-Host"Target file detected: $($file.Name) at $(Get-Date-Format 'HH:mm:ss')"Write-Host"Full path: $($file.FullName)"Start-Sleep-Seconds 2
try{Write-Host"Executing RDP file: $($file.FullName)"# Use Start-Process for reliability in scheduled tasksStart-Process-FilePath $file.FullName
$processedFiles+=$file.FullName
Write-Host"Executed successfully"Start-Sleep-Seconds 1
Remove-Item-Path $file.FullName -Force
Write-Host"File deleted: $($file.FullName)"}catch{Write-Error"Error executing RDP file: $($_.Exception.Message)"}}}}}catch{Write-Error"Error during monitoring: $($_.Exception.Message)"}Start-Sleep-Seconds 2
}finally{try{ Stop-Transcript |Out-Null}catch{}}
上記ファイルを実行する bat ファイル
権限を付与して実行する必要があるのでラッパー用のバッチファイルを作成します
vim monitor_rdp_file.bat
@echo offREM This batch file launches the RDP file monitor PowerShell scriptREM It will be called by Task Scheduler at user loginREM Ensure we start in the script directory (useful for relative paths)pushd"C:\Users\username\path\to\powershell_script\"REM Prepare simple logs for troubleshootingset"LOGDIR=%LOCALAPPDATA%\RDPFileMonitor"ifnotexist"%LOGDIR%"mkdir"%LOGDIR%" >nul 2>&1REM Launch the PowerShell monitor hidden with execution policy bypass and redirect output to logspowershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\Users\username\path\to\powershell_script\monitor_rdp_file.ps1"1>>"%LOGDIR%\ps_out.log"2>>"%LOGDIR%\ps_err.log"popdexit/b0
バッチファイルをタスクスケジューラに登録する Powershell
タスクスケジューラで動かす場合はバッチファイルを登録します
vim register_scheduled_task.ps1
# Create scheduled task for RDP file monitor at user login# This script should be run with administrator privileges$taskName = "RDP File Monitor"$taskPath = "\"$scriptPath = "C:\Users\username\path\to\powershell_script\monitor_rdp_file.bat"Write-Host"Creating scheduled task for RDP file monitor..."# Check if task already exists$existingTask = Get-ScheduledTask -TaskName $taskName-ErrorAction SilentlyContinue
if($existingTask){Write-Host"Task already exists. Removing..."
Unregister-ScheduledTask -TaskName $taskName-Confirm:$false}# Create task trigger (at user login)$trigger = New-ScheduledTaskTrigger -AtLogOn
# Create task action$workingDir = Split-Path-Path $scriptPath-Parent
$action = New-ScheduledTaskAction -Execute "cmd.exe"-Argument "/c `"$scriptPath`""-WorkingDirectory $workingDir# Create task settings$settings = New-ScheduledTaskSettingsSet -MultipleInstances IgnoreNew -StartWhenAvailable
# Register the task with highest privilege$principal = New-ScheduledTaskPrincipal -UserId "$env:USERNAME"-RunLevel Highest -LogonType Interactive
Register-ScheduledTask -TaskName $taskName-Trigger $trigger-Action $action-Settings $settings-Principal $principal-Force
Write-Host"Task created successfully!"Write-Host"Task name: $taskName"Write-Host"Batch file: $scriptPath"Write-Host"Trigger: At user login"Write-Host""Write-Host"The task will now run automatically when you log in to Windows."
ii default-libmysqlclient-dev:amd64 1.1.0build1 amd64 MySQL database development files (metapackage)
ii libmysqlclient-dev 8.4.7-1ubuntu24.04 amd64 MySQL development headers
ii libmysqlclient24:amd64 8.4.7-1ubuntu24.04 amd64 MySQL shared client libraries
ii mysql-apt-config 0.8.36-1 all Auto configuration for MySQL APT Repo.
ii mysql-client 8.4.7-1ubuntu24.04 amd64 MySQL Client meta package depending on latest version
ii mysql-common 8.4.7-1ubuntu24.04 amd64 Common files shared between packages
ii mysql-community-client 8.4.7-1ubuntu24.04 amd64 MySQL Client
ii mysql-community-client-core 8.4.7-1ubuntu24.04 amd64 MySQL Client Core Binaries
ii mysql-community-client-plugins 8.4.7-1ubuntu24.04 amd64 MySQL Client plugin
ii mysql-community-server 8.4.7-1ubuntu24.04 amd64 MySQL Server
ii mysql-community-server-core 8.4.7-1ubuntu24.04 amd64 MySQL Server Core Binaries
ii mysql-server 8.4.7-1ubuntu24.04 amd64 MySQL Server meta package depending on latest version