Powershell 远程尝试获取NlbClusterNode时拒绝访问

Powershell 远程尝试获取NlbClusterNode时拒绝访问,powershell,windows-server-2008,powershell-2.0,Powershell,Windows Server 2008,Powershell 2.0,正在尝试为SMTP群集编写监视脚本(Powershell),该群集在编写时有3个节点 当我在本地运行SMTP群集时,请运行以下命令: Get-NlbClusterNode 我得到了我需要的输出 但如果我从远程服务器(相同的网络和域)尝试相同的操作,我会得到: [smtp-s001a]: PS C:\> Get-NlbClusterNode Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) + C

正在尝试为SMTP群集编写监视脚本(Powershell),该群集在编写时有3个节点

当我在本地运行SMTP群集时,请运行以下命令:

Get-NlbClusterNode
我得到了我需要的输出

但如果我从远程服务器(相同的网络和域)尝试相同的操作,我会得到:

[smtp-s001a]: PS C:\> Get-NlbClusterNode
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo          :
+ FullyQualifiedErrorId :
AccessDenied,Microsoft.NetworkLoadBalancingClusters.PowerShell.GetNlbClusterNode
为什么呢?只有“Get-NlbClusterNODE”命令允许我访问被拒绝。 例如,“Get-NlbCluster”工作正常

有什么建议吗?

我也有同样的问题


我花了一段时间才弄明白。我在我的服务中将Powershell作为进程运行。该服务在所有主机上以相同的用户帐户运行,但密码是在每个主机/群集节点上随机生成的,这就是为什么我会收到“拒绝访问…”的原因。一旦我将密码更改为相同的密码,一切正常。

我找到了一些解决方法。 在远程会话中使用管理员证书模拟

function EntryPoint()
{
    ImportModule-Impersonate;

    $impersonate = new-object UserSession.Impersonate;
    try
    {
        if ($impersonate.Login("SKODA", "Administrator", "*****") -eq $false) {
            throw new Exception("Invalid credentials");
        }
        Import-Module NetworkLoadBalancingClusters
        Get-NlbClusterNode;    
    }
    finally
    {
        $impersonate.Dispose();
    }
};

function ImportModule-Impersonate {

$assem = @();

$source = @" 
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;

namespace UserSession
{
    public class Impersonate : IDisposable
    {
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;

        private WindowsImpersonationContext _impersonationContext;

        [DllImport("advapi32.dll")]
        public static extern int LogonUserA(String lpszUserName,
                                            String lpszDomain,
                                            String lpszPassword,
                                            int dwLogonType,
                                            int dwLogonProvider,
                                            ref IntPtr phToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int DuplicateToken(IntPtr hToken,
                                                int impersonationLevel,
                                                ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);

        public bool Login(String domain, String userName, String password)
        {
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            if (RevertToSelf())
            {
                if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                               LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        WindowsIdentity tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        _impersonationContext = tempWindowsIdentity.Impersonate();

                        if (_impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }
            if (token != IntPtr.Zero)
            {
                CloseHandle(token);
            }
            if (tokenDuplicate != IntPtr.Zero)
            {
                CloseHandle(tokenDuplicate);
            }
            return false;
        }

        public void Logout()
        {
            if (_impersonationContext != null)
            {
                _impersonationContext.Undo();
                _impersonationContext = null;
            }
        }

        public void Dispose()
        {
            Logout();
        }
    }
}
"@;

    Add-Type -ReferencedAssemblies $assem -TypeDefinition $source -Language CSharp 
}

EntryPoint;

同时禁用UAC并重新启动机器。

我遇到了完全相同的问题。使用这些凭据访问和运行
Get-NlbClusterNode
的用户拥有正确的权限,但由于某些无法解释的原因,他遇到了那个奇怪的错误。 我放弃了调查,做了以下变通:

$username = "domain\user_name"
$securePassword = "secure_hash" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
$result = Start-Process powershell.exe -WorkingDirectory $PSHome -Credential $credential -ArgumentList ("-File $PSScriptRoot\your-script.ps1") | Out-Null
其中,如果在
脚本.ps1中放入:

Invoke-Command -ComputerName "your-remote-server" -ScriptBlock {
    Get-NlbClusterNode
    #Any other commands you want here
}

它按预期工作…

您必须使用所有主机上Administrators组成员的登录名连接到群集。您好,David,非常感谢您的回复。我以域管理员身份连接(它是每个节点上本地管理员组的成员)。我在本地登录时远程使用的同一个帐户,它在本地工作。还有其他想法吗?顺致敬意,