首页 > 解决方案 > 远程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 语法。有没有人这样做过?

标签: windowspowershelluser-accounts

解决方案


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

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

推荐阅读