异常提示:the specified credentials were rejected by the server
set-executionpolicy remotesigned
winrm quickconfig
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
查看是否启动
winrm e winrm/config/listener
快速配置:
winrm quickconfig
默认是 Kerberos 认证方式,如果设备添加了域,则本地用户无法访问。
所以需要做以下配置:
允许未加密密码:
winrm s winrm/config/Client '@{AllowUnencrypted="true"}'
开启Basic认证
winrm set winrm/config/service/auth ’@{Basic="true"}‘
winrm set winrm/config/service ‘@{AllowUnencrypted="true"}’
查看配置
winrm get winrm/config
gci wsman::localhost\client\trustedhosts
在客户端添加信任
Set-Item WSMan:localhost\client\trustedhosts -value 192.168.0.49 -Force
远程连接
Enter-PSSession 192.168.0.49 -Credential: win-uvp18pocoal\administrator -Authentication: Basic
当出现"the specified credentials were rejected by the server"错误时,表明目标服务器拒绝了您提供的凭据。这是WS-Management(WinRM)服务的常见问题,以下是系统化的解决方案:
原因类别 | 具体问题 | 验证方法 |
---|---|---|
账户问题 | 1. 用户名/密码错误 2. 账户被锁定 3. 权限不足 | net user [username] /domain Get-ADUser -Identity [username] |
认证策略 | 1. Basic认证禁用 2. CredSSP未启用 3. Kerberos策略问题 | winrm get winrm/config Get-Item WSMan:\localhost\Client\Auth\* |
网络配置 | 1. 目标主机解析错误 2. 双跳问题 3. 防火墙阻止 | nslookup [target] Test-NetConnection [target] -Port 5985 |
系统策略 | 1. UAC限制 2. LSA保护 3. 用户权限分配 | Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System |
# 在本地验证凭据有效性
$cred = Get-Credential
Invoke-Command -ComputerName localhost -ScriptBlock { whoami } -Credential $cred
# 尝试使用管理员账户连接(需在目标主机操作)
net localgroup administrators [username] /add
# 启用所有认证协议(临时测试)
Set-Item WSMan:\localhost\Service\Auth\Basic -Value $true
Set-Item WSMan:\localhost\Service\Auth\Kerberos -Value $true
Set-Item WSMan:\localhost\Service\Auth\CredSSP -Value $true
# 永久配置(需管理员权限)
winrm set winrm/config/service/auth '@{Basic="true"}'
# 在客户端启用CredSSP
Enable-WSManCredSSP -Role Client -DelegateComputer * -Force
# 在服务器端启用CredSSP
Invoke-Command -ComputerName $target -ScriptBlock {
Enable-WSManCredSSP -Role Server -Force
}
# 检查SPN注册(需在域控制器执行)
setspn -L [计算机名]
# 注册SPN(若缺失)
setspn -A HOST/[完整计算机名] [计算机名]
setspn -A HOST/[NETBIOS名] [计算机名]
# 修改远程UAC限制
New-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System `
-Name LocalAccountTokenFilterPolicy -Value 1 -PropertyType DWORD -Force
# 设置执行策略为RemoteSigned
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
# 创建自签名证书(需要管理员权限)
$cert = New-SelfSignedCertificate -DnsName $env:COMPUTERNAME -CertStoreLocation Cert:\LocalMachine\My
winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=$env:COMPUTERNAME;CertificateThumbprint=$cert.Thumbprint}
# 启用5985端口防火墙规则
New-NetFirewallRule -Name "WinRM-HTTP" -DisplayName "WinRM (HTTP-In)" `
-Protocol TCP -LocalPort 5985 -Action Allow -Enabled True
# 强制刷新组策略
gpupdate /force
# 配置计算机账户委托(需域管理员)
Set-ADComputer -Identity $targetServer `
-TrustedForDelegation $true `
-Add @{"msDS-AllowedToDelegateTo"=@("WSMAN/$targetServer","WSMAN/$targetServer.$domain")}
# 使用SSH替代(Windows 10/Server 2019+)
Connect-PSSession -HostName $targetServer -SSHTransport -Credential $cred
graph TD
A[遇到凭证错误] --> B{目标系统可达?}
B -->|No| C[检查网络/DNS]
B -->|Yes| D{基础认证通过?}
D -->|No| E[重置本地凭证缓存]
D -->|Yes| F{远程连接测试}
F -->|失败| G[检查WinRM服务状态]
F -->|成功| H[检查应用层配置]
G --> I{winrm服务正常?}
I -->|No| J[重建WinRM监听器]
I -->|Yes| K[验证认证协议]
# 完整WinRM配置导出
winrm get winrm/config -format:pretty > winrm_config.xml
# Kerberos票据检查
klist purge # 清除旧票据
kinit [username] # 获取新票据
klist # 验证票据
# 网络层追踪
Test-WSMan -ComputerName $targetServer -UseSSL -SessionOption (New-PSSessionOption -IncludePortInSPN)
# 详细事件日志检查
Get-WinEvent -LogName 'Microsoft-Windows-WinRM/Operational' -MaxEvents 50 |
Where-Object { $_.Id -eq 35 } # 认证失败事件
# 创建专用管理组
New-ADGroup -Name "WinRM_Admins" -GroupScope Global
# 配置受限管理权限
Set-PSSessionConfiguration -Name Microsoft.PowerShell -ShowSecurityDescriptorUI
WinRM_Admins
组读取
和执行
权限完全控制
权限# 创建JEA端点
New-PSSessionConfigurationFile -Path .\RestrictedEndpoint.pssc `
-SessionType RestrictedRemoteServer `
-RunAsVirtualAccount `
-RoleDefinitions @{ 'CONTOSO\DatabaseAdmins' = @{ RoleCapabilities = 'DatabaseAdministration' } }
# 注册端点
Register-PSSessionConfiguration -Name 'RestrictedDBAdmin' `
-Path .\RestrictedEndpoint.pssc -Force
生产环境安全警告:
域环境特殊要求:
# 配置Kerberos委派
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*.contoso.com" -Force
跨域认证方案:
# 建立域间信任
New-PSSessionOption -Authentication Negotiate -EnableNetworkAccess
凭证传递替代方案:
# 使用托管服务账户
Install-ADServiceAccount -Identity gMSA_Svc
通过系统化应用这些解决方案,绝大多数"The specified credentials were rejected by the server"错误可以得到解决。对于持续性问题,建议按网络层→认证层→应用层的顺序进行分层排查。