powershell 远程执行命令失败

2023-05-16

Connecting to remote server failed with the following error message : The WinRM client cannot process the
 request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain,
 then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting.
Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You c
an get more information about that by running the following command: winrm help config. For more information, see the 
about_Remote_Troubleshooting Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken


打开 gpedit.msc,原文在这里

http://dustinhatch.tumblr.com/post/24589312635/enable-powershell-remoting-with-credssp-using-group




Enable PowerShell Remoting with CredSSP using Group Policy

Windows PowerShell 2.0 has significantly improved the command-line experience for Windows administration, both for servers and clients. What makes it even better, though, is PowerShell Remoting, which uses Windows Remote Management (WinRM) to send commands between PowerShell sessions on different computers. WinRM is an implementation of WS-Management, an open, standardized SOAP-based web services protocol. In many ways, PowerShell Remoting is similar to SSH, although arguably less mature.

Manual Configuration

Enable PowerShell Remoting Manually

Enabling PowerShell 2.0 Remoting is simple, just run the following command from an elevated PowerShell session:

Enable-PSRemoting -Force

Once that’s done, you can start using it to execute PowerShell commands from a remote host:

Invoke-Command -ComputerName $remotehost -Command { Write-Host "Hello, world!" }

Or, you can open an interactive session on the remote computer:

Enter-PSSession -ComputerName $remotehost

Enable CredSSP Manually

CredSSP is a Security Support Provider introduced with Windows Vista that enables credential delegation. In other words, it allows the remote host to access the credentials that were used to authenticate the user, and pass them on to a third host. For example, when using either basic or Kerberos authentication (the default) when connecting to a remote PowerShell session, the user would not have access to a separate file server. When using CredSSP, however, the session credentials can be passed through to the file server.

To enable CredSSP, both the client and the server must be configured to allow CredSSP. To enable CredSSP on the client side, run the following PowerShell command from an elevated session:

Enable-WSManCredSSP -Role Client -DelegateComputer $remotehost

Note: The DelegateComputer parameter specifies a list of remote hosts to which the client should be allowed to connect. It can accept wildcards, such as * for all hosts, or *.mydomain.local for any host on the mydomain.local DNS domain. If you specify a domain, however, you must always use the server’s FQDN when connecting to it.

To enable CredSSP on the server side, run the following PowerShell 2.0 command from an elevated session:

Enable-WSManCredSSP -Role Server

To connect to a remote host with PowerShell Remoting using CredSSP authentication, you need to specify the Credential and Authentication parameters:

Enter-PSSession -ComputerName $remotehost -Credential (Get-Credential) -Authentication CredSSP

Note: You must specify a fully-qualified username (such as username@domain.tld or DOMAIN\username) when prompted for credentials.

The unfortunate drawback of using CredSSP is that the current implementation of the CredSSP provider for WinRM does not support delegating default credentials (i.e. the current user’s credentials). Go vote for Microsoft Connect Suggestion #498377 if this bothers you; hopefully Microsoft will fix it in a future release. As such, it is best to get a PSCredential object once and store it in a variable for reuse:

$cred = Get-Credential $env:USERNAME@$env:USERDNSDOMAIN

Group Policy Configuration

Enabling PowerShell Remoting and CredSSP manually is fine for only one or two hosts, but what if it needs to be done for every machine on a network? Luckily, Group Policy is able to make all the same configuration changes the Enable-PSRemoting and Enable-WSManCredSSP cmdlets do.

There are several configuration pieces that must be set in order for everything to work correctly:

  • The Windows Remote Management service
  • Windows Firewall exceptions
  • Credential delegation
  • WinRM Client parameters
  • WinRM Service parameters

In addition, some Active Directory objects may need to have permissions changed.

It is probably best to group these settings into one or two separate GPOs, one for servers and one for clients, to keep them separate from the rest of the Group Policy settings that may already exist on the network.

Server Settings

To enable PowerShell Remoting on the server side, create a new GPO and link it an organizational unit containing the computer objects for the server machines. Open the GPO with the Group Policy editor and set the following options:

Windows Remote Management Service

  1. Navigate to Computer Configuration > Windows Settings > Security Settings > System Services
  2. Locate the Windows Remote Management (WS-Management) service and double-click it
  3. Tick the check box nexte to Define this policy setting and select Automatic. Click “OK"

Windows Firewall Exceptions

  1. Navigate to Computer Configuration > Windows Settings > Security Settings> Windows Firewall with Advanced Security > Windows Firewall with Advanced Security - LDAP://{GPO-DistinguishedName} > Inbound Rules
  2. Right-click the pane at the right and choose New Rule…
  3. Select Predefined and choose Windows Remote Management from the drop-down list. Click “Next"
  4. Remove the tick next to Windows Remote Management - Compatibility Mode (HTTP-In), but leave the one for Windows Remote Management (HTTP-In). The “Compatibility Mode" rule provides an upgrade path for systems using WinRM prior to version 2.0 and should not be enabled unless there is a specific need for it. Click “Next"
  5. Select Allow the connection and click “Finish"

WinRM Service Parameters

  1. Navigate to Computer Settings > Administrative Templates > Windows Components > Windows Remote Management (WinRM) > WinRM Service
  2. Double-click Allow automatic configuration of listeners
  3. Select Enabled
  4. In the box labeled IPv4 filter, enter a comma-separated list of IP address ranges to specify to which IP addresses the WinRM service should bind on the server. For example,192.168.1.0-192.168.1.255 would allow the WinRM service to bind to network adapters with an IP address in that range, but no other adapter.
  5. Do the same for IPv6 filter, using IPv6 addresses instead, or leave it blank to disable WinRM over IPv6
  6. Click “OK"
  7. Double-click Allow CredSSP authentication
  8. Select Enabled
  9. Click “OK"

Client Settings

To enable PowerShell remoting on the client side, create a new GPO and link it to an organizational unit containing the computer objects for the client machines. Open the GPO with the Group Policy editor and set the following options:

Credential Delegation

  1. Navigate to Computer Settings > Administrative Templates > System > Credentials Delegation
  2. Double-click Allow Delegating Fresh Credentials
  3. Select Enabled
  4. Click “Show…"
  5. Enter a list of service principal names representing hosts to which clients should be allowed to delegate credentials. Wildcards are allowed in the host name portion of the SPN. For example:
    • WSMAN/Server01 — Allows delegation only to the server named Server01, and only using its single-label name
    • WSMAN/Server01.mydomain.local — Allows delegation only to the server namedServer01, and only using its fully-qualified domain name
    • WSMAN/*.mydomain.local — Allows delegation to any host on the mydomain.localDNS domain, using their fully-qualified domain names only
    • WSMAN/* — Allows delegation to any host by any name
  6. Click “OK"
  7. Click “OK"

WinRM Client Parameters

  1. Navigate to Computer Settings > Administrative Templates > Windows Components > Windows Remote Management (WinRM) > WinRM Client
  2. Double-click Allow CredSSP authentication
  3. Select Enabled
  4. Click “OK"
  5. Double-click Trusted Hosts
  6. Select Enabled
  7. In the box labeled TrustedHostList, enter a comma-separated list of hosts the client should trust. Wildcards are allowed, and there is a special <local> value meaning trust all single-label names. For example:
    • Server01 — Trust only the server named Server01, and only using its single-label name
    • server01.mydomain.local — Trust only the server named Server01, and only using its fully-qualified domain name
    • *.mydomain.local — Trust any host on the mydomain.local DNS domain, using their fully-qualified domain names only
    • <local> — Trust any host by single-label name
    • * — Trust any host by any name
  8. Click “OK"

Troubleshooting

Here are some common error messages and some troubleshooting tips for each:

Operation timed out

Enter-PSSession : Connecting to remote server failed with the following error me
ssage : The WinRM client cannot complete the operation within the time specified
. Check if the machine name is valid and is reachable over the network and firew
all exception for Windows Remote Management service is enabled. For more informa
tion, see the about_Remote_Troubleshooting Help topic.
  • Can you ping the machine using the same name you used for the ComputerName parameter?
  • If the settings are defined in Group Policy, has the machine performed a policy refresh? Force one by running gpupdate /target:computer with elevated privileges
  • Does the machine have the Windows Remote Management (HTTP-In) rules enabled in Windows Firewall?
  • Is the Windows Remote Management (WS-Management) service running on the machine?

Policy does not allow delegation of user credentials

Enter-PSSession : Connecting to remote server failed with the following error me
ssage : The WinRM client cannot process the request. A computer policy does not 
allow the delegation of the user credentials to the target computer. Use gpedit.
msc and look at the following policy: Computer Configuration -> Administrative T
emplates -> System -> Credentials Delegation -> Allow Delegating Fresh Credentia
ls.  Verify that it is enabled and configured with an SPN appropriate for the ta
rget computer. For example, for a target computer name "myserver.domain.com", th
e SPN can be one of the following: WSMAN/myserver.domain.com or WSMAN/*.domain.c
om. For more information, see the about_Remote_Troubleshooting Help topic.
  • Make sure the name specified in the ComputerName parameter matches the SPN specified in the GPO. If the policy specifies a wildcard with a domain name, for example, make sure theComputerName parameter is the fully-qualified domain name of the remote host, not just its single-label name

The target computer is not trusted

Enter-PSSession : Connecting to remote server failed with the following error me
ssage : The WinRM client cannot process the request. A computer policy does not 
allow the delegation of the user credentials to the target computer because the 
computer is not trusted. The identity of the target computer can be verified if 
you configure the WSMAN service to use a valid certificate using the following co
mmand: winrm set winrm/config/service '@{CertificateThumbprint="<thumbprint>"}' 
 Or you can check the Event Viewer for an event that specifies that the followin
g SPN could not be created: WSMAN/<computerFQDN>. If you find this event, you ca
n manually create the SPN using setspn.exe .  If the SPN exists, but CredSSP can
not use Kerberos to validate the identity of the target computer and you still w
ant to allow the delegation of the user credentials to the target computer, use 
gpedit.msc and look at the following policy: Computer Configuration -> Administr
ative Templates -> System -> Credentials Delegation -> Allow Fresh Credentials w
ith NTLM-only Server Authentication.  Verify that it is enabled and configured w
ith an SPN appropriate for the target computer. For example, for a target comput
er name "myserver.domain.com", the SPN can be one of the following: WSMAN/myserv
er.domain.com or WSMAN/*.domain.com. Try the request again after these changes. 
For more information, see the about_Remote_Troubleshooting Help topic.
  • Make sure the remote host has a Service Principal Name starting with WSMAN and matching the value specified in the ComputerName parameter. To list a host’s service principal names, run setspn -l <computername> with elevated privileges on a domain controller. If a proper SPN does not exist, try restarting the Windows Remote Management (WS-Management)service, and check the System event log for event ID 10154. If that event exists, you will need to modify permissions in Active Directory in order for hosts to be able to register their SPNs correctly (see below)
  • Make sure you are specifying a fully-qualified user name in the PSCredential object passed to the Credential parameter (i.e. DOMAIN\username or username@domain.local)

Modifying Active Directory Permissions

Note: Perform these steps ONLY if you receive the “target computer is not trusted" error, Windows Remote Managment logs event ID 10154 in the System event log, and setspn -l does not list anyWSMAN/... SPNs for the remote host!

  1. Open ADSI Edit
  2. Click Action > Connect to…
  3. Under Connection Point, select Select a well known Naming Context and choose Default naming context
  4. Under Computer, select Default (Domain or server that you logged in to)
  5. If your domain controllers support it (i.e. you are running Active Directory Certificate Services), tick Use SSL-based Encryption
  6. Expand the objects in the tree at the left until you find the container containing the computer object for the server exhibiting the issue, such as CN=Computers
  7. Right-click on the container object and choose Properties
  8. Click the Security tab
  9. Click “Advanced"
  10. Click “Add…"
  11. In the box labeled Enter the name of the object to select, enter NETWORK SERVICE
  12. In the drop-down list labeled Apply to, select Descendant Computer objects
  13. Scroll all the way to the bottom of the Permissions list and tick the box in the Allow column for Validated write to service principal name
  14. Tick Apply these permissions to objects and/or containers within this container only
  15. Click “OK"
  16. Click “OK"
  17. Click “OK"
  18. Repeat steps 6-17 for any container with computer objects for hosts on which PowerShell Remoting is enabled
  19. Restart the Windows Remote Management (WS-Management) service on the affected hosts
  20. Run setspn -l <computername> with elevated privileges on a domain controller to verify that the SPN was correctly created



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

powershell 远程执行命令失败 的相关文章

随机推荐

  • linux mount 远程服务器共享目录

    NFS是文件系统 在网络存储方面我们应该有所了解 那么针对NFS服务器的安装和设置我们来详细介绍一下 首先让我们看一下NFS服务器的安装步骤 一 NFS服务器的安装 检查linux系统中是否安装了nfs utils和portmap两个软件包
  • 【每日一题】969. 煎饼排序

    969 煎饼排序 题目描述解决方案 xff1a 类选择排序法代码 xff1a Python 题目来源 xff1a Leetcode 原文链接 xff1a https mp weixin qq com s jboDC0R oYAy ssCXp
  • 让Num Lock默认开启

    让Num Lock默认开启 2008年11月18日 星期二 10 46 A M 1 对于2000或者XP操作系统 xff0c 登陆前NUM LOCK默认为关闭 xff0c 此为正常现象 xff0c 若用户需要此功能 xff0c 则需更改注册
  • Windows下使用Sublime Text配置C++编译环境

    1 打开Sublime xff0c 选择Tools gt Build System gt New Build System 2 将以下代码复制粘贴到新文件中去 34 span class hljs attribute path span 3
  • 一步步将ffmpeg封装golang/cgo库

    欢迎访问博客原文 xff1a https lightfish cn 2018 12 24 ffmpeg cgo 前言 继上一篇 ffmpeg音视频C编程入门 使用高性能的C语言进行音视频的处理 xff0c 比较执行效率比较高 xff0c 但
  • 十六.Spark SQL之读取复杂的json数据

    第一步 准备json数据 test json 34 name 34 34 liguohui 34 34 nums 34 1 2 3 4 5 34 name 34 34 zhangsan 34 34 nums 34 6 7 8 9 10 te
  • “当前不会命中断点 还没有为该文档加载任何符号”问题的解决

    今天在实验室的电脑上调试程序出现了 当前不会命中断点 还没有为该文档加载任何符号 断点失效的情况 xff0c 是调用的静态库中断点失效 xff0c 但程序在我自己电脑上是可以正常打断点的 按照网上的方法试过没有成果 xff0c 但是启发了我
  • 【经验分享】设置电脑定时开关机

    文章目录 1 定时开机设置 xff08 BIOS固件设置 xff09 2 定时关机设置 放长假回家 xff0c 不想拷贝资料 xff0c 因此打算用todesk远程连接办公 但是工位电脑一直开着 xff0c 还不能睡眠 xff0c 担心会过
  • AirSim多台无人机第一视角键盘控制进阶版

    AirSim多台无人机第一视角键盘控制进阶版 目录 AirSim多台无人机第一视角键盘控制进阶版本文实现的效果前言一 环境依赖二 图像读取与显示1 使用的API2 实时显示的一种方法 三 键盘控制改进总结 本文实现的效果 前言 本篇文章实现
  • 百度APP iOS端内存优化实践-内存管控方案

    01 背景 随着业务的发展 xff0c 百度APP有很多大内存业务场景如直播 短视频 小程序 百度识图等 xff0c 通过线上页面统计数据得知超过150M页面有40个 xff0c 耗内存最多的页面有400M 单个页面不会有内存或者稳定性问题
  • 百度APP iOS端内存优化-原理篇

    一 Mach虚拟内存 1 1 Mach内存简介 iOS系统架构可分为内核驱动层 xff08 Kernel and Device Drivers Layer xff09 核心操作系统层 xff08 Core OS xff09 核心服务层 xf
  • 【图文并茂】手把手教你重装Win10系统

    当遇见电脑出现问题 xff0c 想要重装系统的小伙伴们 xff0c 小编在这里手把手教你重装Win10系统 xff0c 推荐系统之家装机大师 xff0c 他是一款非常好用的一键重装工具 xff0c 有不少小伙伴都在使用 xff0c 但对于刚
  • 【每日一题】1994.好子集的数目

    1994 好子集的数目 题目描述解决方案 xff1a 状态压缩 43 动态规划代码 xff1a Python 题目来源 xff1a LeetCode 原文链接 xff1a https mp weixin qq com s myI7 ZwJM
  • Win11打开移动热点后电脑无法上网怎么办?

    Win11打开移动热点后电脑无法上网怎么办 xff1f 有用户将自己的电脑开启移动热点来使用的时候 xff0c 发现自己的电脑出现了无法上网的情况 那么为什么开启热点之后 xff0c 就会无法进行上网呢 xff1f 来看看以下的解决方法分享
  • abaqus导出全部节点应力值

    一 查询应力点 1 2 3 二 导出应力点 1 点击报告 xff08 E xff09 场输出 xff08 F xff09 2 选择想要输出的参数 xff0c 应用 xff08 提前设置好文件夹与文件名 xff09
  • Docker容器之Dockerfile构建镜像

    目录 一 Dcokerfile概念 1 dockerfile的原理 二 Docker镜像的创建 1 基于已有镜像创建 2 基于本地模板创建 3 基于dockerfile创建 三 镜像分层的原理 1 Docker镜像分层 xff08 基于AU
  • 树莓派——开机指南

    1 准备 硬件准备 树莓派一块 SD卡 xff08 小卡 xff09 读卡器 树莓派电源或安卓手机电源 xff08 功率10w以上 xff0c 不然会导致电压不足会影响其性能 xff09 一台电脑 xff08 可以没有显示屏和鼠标键盘 xf
  • pytesseract的使用 | python识别验证码

    目录 1 安装tesseract2 安装pytesseract3 修改包中部分代码4 代码网站测试 1 安装tesseract 详见 xff1a https blog csdn net lijiamingccc article detail
  • 从一道面试题彻底搞懂hashCode与equals的作用与区别及应当注意的细节

    最近去面试了几家公司 xff0c 被问到hashCode的作用 xff0c 虽然回答出来了 xff0c 但是自己还是对hashCode和equals的作用一知半解的 xff0c 所以决定把它们研究一下 以前写程序一直没有注意hashCode
  • powershell 远程执行命令失败

    Connecting to remote server failed with the following error message The WinRM client cannot process the request If the a