How can I use PowerShell to make remote registry changes?

帅比萌擦擦* 提交于 2020-01-10 05:29:05

问题


I have tested the following PowerShell registry settings and it sets them correctly. Could someone show me the way to do this for a remote computer?

New-Item -itemType String HKLM:\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration\Server0ps -Value "MY.DOMAIN.COM"
New-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\PermittedManagers -Name 1 -Value "whatever" 
Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities -Name "Hello" -Value 4
Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\SNMP\Parameters\ValidCommunities -Name "There" -Value 8

回答1:


Use this as example:

$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computername ) 
        $regKey= $reg.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",$true) 
        $regKey.SetValue("New_Valuename_String","New_Valuedata",[Microsoft.Win32.RegistryValueKind]::String) 

To create a new key you need use powershell remoting with invoke-command for new-item cmdlet.




回答2:


You might want to check the PSRemoteRegistry PowerShell Module, and its version for PowerShell 3.0 (with x86.x64 support, http://psrr.codeplex.com/).




回答3:


If you just want to delete a key

$exchangeServers = @("xxxxx");
$hive = [Microsoft.Win32.RegistryHive]::LocalMachine;
$key = "SYSTEM\CurrentControlSet\Control\Lsa";

foreach ($exchangeServer in $exchangeServers)
{
    $regBaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hive, $exchangeServer.ToString());
    $regKeys = $regBaseKey.OpenSubKey($key,$true);
    $beforeVal = $regKeys.GetValue("DisableLoopbackCheck");
    Write-Host $exchangeServer " - " $beforeVal;
    $regKeys.DeleteValue("DisableLoopbackCheck"); # a try catch can be placed here if there is a concern the key won't exist
    $keyNames = $regKeys.GetSubKeyNames();
    $afterVal = $regKeys.GetValue("DisableLoopbackCheck");
    if ($afterVal -eq $null)
    {
        Write-Host $exchangeServer " - deleted" -ForegroundColor DarkGreen;
    }
    else
    {
        Write-Host $exchangeServer " - " $afterVal -ForegroundColor Red;
    }
    Write-Host " ";
}


来源:https://stackoverflow.com/questions/18896248/how-can-i-use-powershell-to-make-remote-registry-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!