使用Powershell远程验证注册表项

时间:2015-06-19 17:36:25

标签: powershell remote-access

我正在尝试编写一个脚本来检查网络上哪些打印服务器用户正在连接。为此,我正在尝试检查Printer \ Connections下列出的注册表值。在我的本地机器上,以下两种方法都有效:

1)

  
    

Get-ChildItem HKCU:\ Printers \ Connections

  

输出:

Hive: HKEY_CURRENT_USER\Printers\Connections

Name                              Property
-----                             --------
,,printServer,printerName         GuidPrinter        : {guid}
                                  Server             : \\printserver.domain
                                  Provider           : win32spl.dll
                                  LocalConnection    : 1

2)

>> Get-ChildItem HKCU:\Printers\Connections | ForEach-Object {Get-ItemProperty $_.pspath}

该命令的输出是:

GuidPrinter     : {guid}
Server          : \\printServer.domain
Provider        : win32spl.dll
LocalConnection : 1
PSPath          : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Printers\Connections\,,printServerName,printerName
PSParentPath    : Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Printers\Connections
PSChildName     : ,,printServerName, printerName
PSProvider      : Microsoft.PowerShell.Core\Registry    

这些实现本身都不容易应用于远程计算机。我最近尝试从远程计算机上获取上面显示的相同信息是:

$machine = 'computerName';
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('CurrentUser',$machine);
$regKey = $reg.OpenSubKey("Printers\Connections");
$regKey.GetValueName()

# I also tried the following which I didn't think
# would work, but I was grasping at straws:
# Get-ChildItem $regKey;

以上代码不返回任何内容。它不会抛出错误,但是regKey返回一个空字符串。

有没有人知道获取我正在寻找的信息或在上面的实现中看到错误的替代解决方案?

感谢。

2 个答案:

答案 0 :(得分:3)

这是因为您正在寻找的密钥是HKEY当前用户,虽然每个用户的更改都是正确的,具体取决于谁登录,因此当您远程运行时加载您的配置单元而不是用户。你必须让用户运行它,最简单的方法之一是通过它进入机器的启动文件夹,它将运行并在没有他们知道的情况下向你报告。

答案 1 :(得分:1)

绝不是完美但我想出了这个。在检查它的部分时,有几种不同的方法来获得相同的东西,但这是我能够获得切实结果的第一种方法。缺乏功能,但我想展示的结果不仅仅是手段。

<div id="key"></div>

您可以将单个计算机名称传递给该功能,它将找到已登录的用户。他们确定登录的用户是基于一个具有关联Function Get-NetworkPrinters($computer){ # This was the only consistent way I could get the "users" to show up. win32_computersystem has a username but its null for me (UAC maybe?) $remoteUsers = Get-WmiObject win32_process -ComputerName $computer -Filter 'Name = "explorer.exe"' -ErrorAction SilentlyContinue | ForEach-Object{ $_.GetOwner().User } # Get the users sids from active directory. We need these to check the user hives. $users = $remoteUsers | Get-ADUser | Select-Object samaccountname,name,sid $users | ForEach-Object{ # Keep the user for use in another pipe $user = $_ # Key that contains the mappings $keyPath = "$($user.Sid)\Printers\Connections" # Open the remote registry keys $Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $computer) # Get the key names of connections which "match" printer mappings $reg.OpenSubKey($keyPath).GetSubKeyNames() | ForEach-Object{ $props = @{ Printer = $_.Replace(",","\") Computer = $computer UserName = $user.samaccountname } New-Object -TypeName PSCustomObject -Property $props } } } 进程的用户。对于每个用户名,再次检查活动目录以获取其SID(explorer.exe可能不是最好的方法,但它是一个简单的方法)。然后我们使用该信息打开远程注册表并获取网络打印机配置。

一些示例输出:

Get-AdUser

有几个潜在的错误没有考虑到。所以这肯定会随着时间的推移而有所改善。

您还可以考虑this question中涵盖的其他远程注册表选项,如果我的内容对您没有帮助,可能会将其调整为逻辑。

相关问题