Powershell 使用 CNAME 远程处理到服务器,而不是计算机名称

Mat*_*att 9 powershell windows-server-2008

我试图让 powershell 远程处理使用它的 CNAME 而不是计算机名称与服务器一起工作。本地和远程机器都在同一个网络和域中。

我启用了 powershell 远程处理,并且它与计算机名称一起工作正常。我已将 TrustedHosts 设置为 CNAME 并为 CNAME 添加了 SPN,但是当我发出时,Enter-PSSession CNAME我得到以下信息:

Enter-PSSession : Connecting to remote server CNAME failed with the following
error message : WinRM cannot process the request. The following error
occurred while using Kerberos authentication: Cannot find the computer CNAME.
Verify that the computer exists on the network and that the name
provided is spelled correctly. For more information, see the
about_Remote_Troubleshooting Help topic.
Run Code Online (Sandbox Code Playgroud)

执行setspn -l COMPUTERNAME给了我这个:

Registered ServicePrincipalNames for CN=COMPUTERNAME,OU=SERVERS,DC=COMPANY,DC=private:
        WSMAN/CNAME
        WSMAN/COMPUTERNAME
        WSMAN/COMPUTERNAME.local
        TERMSRV/COMPUTERNAME
        TERMSRV/COMPUTERNAME.local
        HOST/COMPUTERNAME
        HOST/COMPUTERNAME.local
Run Code Online (Sandbox Code Playgroud)

通过 CNAME 启用访问还需要什么?

jbs*_*ith 5

我为解决这个问题所做的是为 Enter-PSSession 创建一个代理函数,为我解析 CNAME。这可能不适用于您的情况,具体取决于您为什么需要使用 CNAME,但这对我有用。

代理 powershell 功能的详细信息:http : //www.windowsitpro.com/blog/powershell-with-a-purpose-blog-36/windows-powershell/powershell-proxy-functions-141413

全功能:

function Enter-PSSession {
[CmdletBinding(DefaultParameterSetName='ComputerName')]
param(
    [Parameter(ParameterSetName='ComputerName', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('Cn')]
    [ValidateNotNullOrEmpty()]
    [string]
    ${ComputerName},

[Parameter(ParameterSetName='Session', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.Runspaces.PSSession]
${Session},

[Parameter(ParameterSetName='Uri', Position=1, ValueFromPipelineByPropertyName=$true)]
[Alias('URI','CU')]
[ValidateNotNullOrEmpty()]
[uri]
${ConnectionUri},

[Parameter(ParameterSetName='InstanceId', ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[guid]
${InstanceId},

[Parameter(ParameterSetName='Id', Position=0, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNull()]
[int]
${Id},

[Parameter(ParameterSetName='Name', ValueFromPipelineByPropertyName=$true)]
[string]
${Name},

[Parameter(ParameterSetName='Uri')]
[Parameter(ParameterSetName='ComputerName')]
[switch]
${EnableNetworkAccess},

[Parameter(ParameterSetName='ComputerName', ValueFromPipelineByPropertyName=$true)]
[Parameter(ParameterSetName='Uri', ValueFromPipelineByPropertyName=$true)]
[system.management.automation.pscredential]
${Credential},

[Parameter(ParameterSetName='ComputerName')]
[ValidateRange(1, 65535)]
[int]
${Port},

[Parameter(ParameterSetName='ComputerName')]
[switch]
${UseSSL},

[Parameter(ParameterSetName='ComputerName', ValueFromPipelineByPropertyName=$true)]
[Parameter(ParameterSetName='Uri', ValueFromPipelineByPropertyName=$true)]
[string]
${ConfigurationName},

[Parameter(ParameterSetName='ComputerName', ValueFromPipelineByPropertyName=$true)]
[string]
${ApplicationName},

[Parameter(ParameterSetName='Uri')]
[switch]
${AllowRedirection},

[Parameter(ParameterSetName='Uri')]
[Parameter(ParameterSetName='ComputerName')]
[ValidateNotNull()]
[System.Management.Automation.Remoting.PSSessionOption]
${SessionOption},

[Parameter(ParameterSetName='Uri')]
[Parameter(ParameterSetName='ComputerName')]
[System.Management.Automation.Runspaces.AuthenticationMechanism]
${Authentication},

[Parameter(ParameterSetName='Uri')]
[Parameter(ParameterSetName='ComputerName')]
[string]
${CertificateThumbprint})


begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $PSBoundParameters['ComputerName'] = ([System.Net.Dns]::GetHostByName($PSBoundParameters['ComputerName'])).HostName
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Core\Enter-PSSession', [System.Management.Automation.CommandTypes]::Cmdlet)
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}
<#

.ForwardHelpTargetName Enter-PSSession
.ForwardHelpCategory Cmdlet

#>

}
Run Code Online (Sandbox Code Playgroud)

我添加的唯一一行是:

$PSBoundParameters['ComputerName'] = ([System.Net.Dns]::GetHostByName($PSBoundParameters['ComputerName'])).HostName
Run Code Online (Sandbox Code Playgroud)

这只是在调用本机 Enter-PSSession 之前将 CNAME 解析为代理函数中的 FQDN。

这允许我通过组策略在我的 TrustedHosts 中设置 *.mydomain.local,并且我仍然可以使用“Enter-PSSession ShortName”或“Enter-PSSession CNAME”而不必弄乱额外的 SPN 等。