Для решения данной задачи будем использовать, как маппинг ветки реестра на локальную машину, в данном случае файл находящийся в каталоге пользователя: C:\Users\<username>\ntuser.dat, так и Invoke-Command.
В случае, если на удаленной машине есть активные процессы под пользователем, ветку которого мы хотим загрузить командой reg load <куда> <от куда>, то произойдет исключение и скрипт перейдет в исполнение инструкций из блока Catch в которой указана команда Invoke-Command. Если же процессов нет, то выполняются инструкции из Try.
Пример: reg load HKLM\TempHive \\$remote_server\C$\Users\$user\ntuser.dat
После чего можно уже выполнить определенные действия с подключенной веткой и отвязать ее с помощью команды reg unload <путь к ветке>
Если же вы не отвяжете ветку, то пользователь сможет войти только с временным профилем.
Скрипт тестировался на серверах с Windows Server 2019 и Windows Server 2012 R2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<# This is script add values HKU regedit for active and unactive users #> $ErrorActionPreference = "Stop" $pathreg = "TempHive\Software\Business Objects\Suite 11.5\Crystal Reports\Export\Pdf" $user = "TestUser" $regtype = "dword" $regvalue = "00000001" $remote_server = "rds2" #Variables script block for remote startup $scriptblock = { param($login, $regtype1, $regvalue1) #Add-Content -Path "c:\temp\qwer.log"` #-Value "[$((Get-Date).TimeOfDay)] Login =$login, regtype = $regtype1, Regvalue = $regvalue1 " function get-sid { Param ( $DSIdentity ) $ID = new-object System.Security.Principal.NTAccount($DSIdentity) return $ID.Translate( [System.Security.Principal.SecurityIdentifier] ).toString() } $admin = get-sid $login $sid=$admin.SubString(0, $admin.Length) $pathreg2 = $sid+"\Software\Business Objects\Suite 11.5\Crystal Reports\Export\Pdf" if (Test-Path -Path Registry::HKEY_USERS\$pathreg2) { if (!(Get-ItemProperty -Path Registry::HKEY_USERS\$pathreg2 -name "ForceLargerFonts" -ErrorAction SilentlyContinue)) { New-ItemProperty -Path Registry::HKEY_USERS\$pathreg2 -Name "ForceLargerFonts" -PropertyType $regtype1 -Value $regvalue1 } else{ Set-ItemProperty -Path Registry::HKEY_USERS\$pathreg2 -Name "ForceLargerFonts" -Value $regvalue1 -Force } } else { New-Item -Path Registry::HKEY_USERS\$pathreg2 -Force | Out-Null New-ItemProperty -Path Registry::HKEY_USERS\$pathreg2 -Name "ForceLargerFonts" -PropertyType $regtype1 -Value $regvalue1 } } #end scriptblock if (Test-Path -Path Registry::HKEY_LOCAL_MACHINE\$pathreg) {reg unload HKLM\TempHive} try { #if the user session is not active on the remote server and all processes are stopped #upload the remote HKEY_USERS branch of the registry to the local server reg load HKLM\TempHive \\$remote_server\C$\Users\$user\ntuser.dat if (Test-Path -Path Registry::HKEY_LOCAL_MACHINE\$pathreg) { if (!(Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\$pathreg -name "ForceLargerFonts" -ErrorAction SilentlyContinue)) { New-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\$pathreg -Name "ForceLargerFonts" -PropertyType $regtype -Value $regvalue } else{ Set-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\$pathreg -Name "ForceLargerFonts" -Value $regvalue -Force } } else { New-Item -Path Registry::HKEY_LOCAL_MACHINE\$pathreg -Force | Out-Null New-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\$pathreg -Name "ForceLargerFonts" -PropertyType $regtype -Value $regvalue } reg unload HKLM\TempHive } catch{ #remote start script if user session is active on remote server or some process is running Invoke-Command -ComputerName $remote_server -ScriptBlock $scriptblock -ArgumentList ($user,$regtype,$regvalue) } finally{ if (Test-Path -Path Registry::HKEY_LOCAL_MACHINE\$pathreg) {reg unload HKLM\TempHive} } #garbage collector [gc]::collect() |