Powershell Core 6.2 Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

While Invoke-Command via web service management and Windows remote management should be your preferred way of accessing remote registry keys, serialization and other issues can occur. Using .NET, you can also simply access a remote registry hive via Distributed Component Object Model (DCOM) and Remote Procedure Calls (RPC) from your local session. To get started, have a look at the following code sample:

$remoteKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', 'MyHost', 'Registry64')
$remoteUserKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('CurrentUser', 'MyHost', 'Registry64')

The previous lines opened two remote hives, HKLM and HKCU. With the next lines, you can get value names in a key and get a specific value from said key:

$remoteKey.OpenSubKey('SOFTWARE\Microsoft\Windows NT\CurrentVersion').GetValueNames()
$remoteKey.OpenSubKey('SOFTWARE\Microsoft\Windows NT\CurrentVersion').GetValue('ProductName')

To create remote sub keys and remote values, you can use the following lines. Notice the Boolean parameter value for the OpenSubKey method. The key needs to opened with write access, the default is read-only:

$remoteUserKey.OpenSubKey('SOFTWARE',$true).CreateSubKey('MyProduct').SetValue('Version','1.2.3.4', 'String')
$remoteUserKey.OpenSubKey('SOFTWARE\MyProduct').GetValue('Version')
$remoteUserKey.DeleteSubKeyTree('Software\MyProduct')

While not strictly necessary, you should always free up any references to .NET objects so that the garbage collection can clean up and free up resources:

$remoteKey.Dispose()
$remoteUserKey.Dispose()