Как импортировать файл .reg с помощью Powershell в каждый профиль пользователя на компьютере? - PullRequest
0 голосов
/ 10 июля 2019

Я хочу импортировать файл .reg в каждый профиль пользователя на компьютере.

Мне также было бы интересно узнать, как сохранить файл .reg в качестве переменной и как использовать эту переменную для изменения ключей reg.

В reg-файле слишком много строк для отдельного добавления.

$PatternSID = 'S-1-5-21-\d+-\d+\-\d+\-\d+$'
 Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' | Where-Object {$_.PSChildName -match $PatternSID} |
     select  @{name="SID";expression={$_.PSChildName}},
             @{name="UserHive";expression={"$($_.ProfileImagePath)\ntuser.dat"}},
             @{name="Username";expression={$_.ProfileImagePath -replace '^(.*[\\\/])', ''}}

Get-ChildItem Registry::HKEY_USERS | Where-Object {$_.PSChildName -match $PatternSID} | select PSChildName

# Regex pattern for SIDs
$PatternSID = 'S-1-5-21-\d+-\d+\-\d+\-\d+$'

# Get Username, SID, and location of ntuser.dat for all users
$ProfileList = gp 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' | Where-Object {$_.PSChildName -match $PatternSID} | 
    Select  @{name="SID";expression={$_.PSChildName}}, 
            @{name="UserHive";expression={"$($_.ProfileImagePath)\ntuser.dat"}}, 
            @{name="Username";expression={$_.ProfileImagePath -replace '^(.*[\\\/])', ''}}

# Get all user SIDs found in HKEY_USERS (ntuder.dat files that are loaded)
$LoadedHives = gci Registry::HKEY_USERS | ? {$_.PSChildname -match $PatternSID} | Select @{name="SID";expression={$_.PSChildName}}

# Get all users that are not currently logged
$UnloadedHives = Compare-Object $ProfileList.SID $LoadedHives.SID | Select @{name="SID";expression={$_.InputObject}}, UserHive, Username

# Loop through each profile on the machine
Foreach ($item in $ProfileList) {
    # Load User ntuser.dat if it's not already loaded
    IF ($item.SID -in $UnloadedHives.SID) {
        reg load HKU\$($Item.SID) $($Item.UserHive) | Out-Null
    }

    #####################################################################
    # This is where you can read/modify a users portion of the registry 

    #####################################################################
    #I WANT TO IMPORT THESE REG SETTINGS FOR EACH USER, BUT NOT SURE HOW TO FORMAT. HELP?
    reg import "\\fakecompanyname.com\files\public\IT\Protected\Projects\SOLIDWORKS 2019 deployment\fakeregfilename.sldreg"

    #####################################################################
    # This example lists the Uninstall keys for each user registry hive (and was the example from where I copied the rest of the code, I don't need this, just including here for reference)
    #"{0}" -f $($item.Username) | Write-Output
    #Get-ItemProperty registry::HKEY_USERS\$($Item.SID)\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | 
    #    Foreach {"{0} {1}" -f "   Program:", $($_.DisplayName) | Write-Output}
    #Get-ItemProperty registry::HKEY_USERS\$($Item.SID)\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
    #    Foreach {"{0} {1}" -f "   Program:", $($_.DisplayName) | Write-Output}

    #####################################################################

    # Unload ntuser.dat        
    IF ($item.SID -in $UnloadedHives.SID) {
        ### Garbage collection and closing of ntuser.dat ###
        [gc]::Collect()
        reg unload HKU\$($Item.SID) | Out-Null
    }
}

Вид хитрости для тестирования.

Мы закончили с использованием ...

    # read in reg file with the settings we need
    $regstuff = Get-Content "\\fakecomapnyname.com\files\public\IT\Protected\Projects\SOLIDWORKS-2019-deployment\fakesettings.reg"

    # Fix it so it applies to the specific profile for this loop, profile
    $regstuff = $regstuff.Replace("[HKEY_CURRENT_USER\","[HKEY_USERS\$($Item.SID)\")

    # write it to our temporary location
    $regstuff | Out-File "$env:TEMP\regstuff-fixed.reg"

    write-output $($Item.Username)
    # import it
    reg.exe import "$env:TEMP\regstuff-fixed.reg"

, который, кажется, работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...