至少没有手动登录一次,无法远程获取机器的组策略

Unable to get group policy for machine, remotely, without having logged in manually at least once

我已经使用 System.Management.Automation 编写了一个 C# 桌面应用程序,它可以远程访问我们网络中的机器并进行诊断以确保它们都按照我们组织的策略进行配置。

PSCredential Credentials = new PSCredential(Username, Password);
WSManConnectionInfo ThisConnection = new WSManConnectionInfo(false, ThisSystem.IpName, 5985, "/wsman",
    "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", Credentials);

using (Runspace ThisRunspace = RunspaceFactory.CreateRunspace(ThisConnection))
{
    ThisRunspace.Open();
    using (PowerShell ThisShell = PowerShell.Create())
    {
        ThisShell.Runspace = ThisRunspace;

要获取组策略中的横幅,它会执行以下代码来执行 powershell 命令

        ThisShell.Commands.Clear();
        ThisShell.AddScript("echo N | gpupdate /force");
        ThisShell.AddScript("gpresult /h c:\Windows\Temp\gpdata.html /F");
        ThisShell.AddScript("type c:\Windows\Temp\gpdata.html");
        List<String> GroupPolicy = ThisShell.Invoke().Select(x => x.ToString()).ToList();

应该使用该机器的组策略数据创建 html 文件。确实如此,只要我使用相同的凭据手动登录到那台机器。如果我没有,我什么也得不到。我 运行 其他 powershell 命令就像这样并且得到的结果很好,但由于某种原因,这个获取组策略的命令只适用于我至少手动登录过一次(使用远程桌面)的机器,并且有成千上万的服务器要检查,这不是一个选项。

我认为可能是手动登录为我正在使用的管理员帐户创建了主文件夹,但这不可能是因为我正在获取通过管道传输到文件的其他 powershell 命令的结果。我认为可能是手动登录更新了该机器上的组策略,但我认为这也不可能,因为我添加了 gpupdate /force,但没有任何区别。

有什么线索吗?

来自documentation for gpresult

Because you can apply overlapping policy settings to any computer or user, the Group Policy feature generates a resulting set of policy settings when the user logs on. gpresult displays the resulting set of policy settings that were enforced on the computer for the specified user when the user logged on.

由于您是在谈论在服务器上执行策略,通常不应期望用户登录,也许您可​​以尝试添加 /scope computer 选项以仅获取机器策略,这应该始终有空。