远程SAM用户查询

问题描述

我正在尝试从远程Windows框中的本地SAM数据库查询用户信息。 我已经知道可以进行本地通话的语法了,但是对于远程通话,我似乎找不到正确的语法来连接到远程系统。

Add-Type -AssemblyName System.DirectoryServices.AccountManagement;
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Machine;
$User = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ct,"sean");
$User;

用于远程连接的C#代码在这里

    PrincipalContext insprincipalContext = new PrincipalContext(ContextType.Machine,"TAMERO","administrator","password");

我似乎无法为PS获得正确的语法。 有人做过吗?

解决方法

要在PowerShell中调用PrincipalContext构造函数,请使用特殊的new()静态方法:

#...
$principalContext = [System.DirectoryServices.AccountManagement.PrincipalContext]::new($ct,"Computername","Username","Password")
$User = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($principalContext,"targetSAMAccountName")