Powershell indicating WinRm not running when it is - powershell

I have a script that builds a VM and installs multiple programs. The script runs on a separate server, and builds the VM by using Invoke-Command to execute commands remotely on the VM server. There are perhaps 20 steps to the process, with each step creating a session, invoking a command on the VM, then removing the session.
Towards the end of the script I'm always getting the error below, but not always on the same step. When I run the script again leaving off where it last failed, it works fine. Obviously Winrm is enabled so I'm at a loss as to what can be causing it.
What possible reasons could explain getting this error when WinRM is most definitely running?
"Processing data for a remote command failed with the following error message: The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic."

It sounds like the 'Web Management Service' is not running on the remote machine.
This service is required for IIS management and some applications.
Regards

Related

powershell on target machine causes "PSSession could not be created for Machine:'XXX:5986"

I try to use release pipeline and setup a task "run powershell on target machines"
And here are the error messages:
##[error]Unable to create pssession. Error: 'Connecting to remote server XXX failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.'
##[error]The remote session query failed for XXX with the following error message: The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".
##[error]PSSession could not be created for Machine:'XXX:5986'
My VM is on Azure, and I already open PORT 5986\5985 in 'internet' settings.
I found some article talking about "remote management", however, I am not sure how to set this up on my VM.
https://learn.microsoft.com/en-us/windows/win32/winrm/portal
https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/winrmsecurity?view=powershell-7.2
Maybe I need some setup are needed for the instance which pipelines are running on (agent pools)?
https://developercommunity.visualstudio.com/t/with-powershell-on-target-machines-task-cant-able/635225
Any suggestion is appreciated.
Update-1
The suggestion from #wade-zhou-msft seems not working. But thanks!
Updata-2
change the network setting, no more error.
But the situation still the same ...
In addition, my 'copy file to remote machine' task works well.
Update-3
I find articles talks about "how to remotely run powershell command from Linux"
https://adamtheautomator.com/psremoting-linux/
It shows that some setup is needed. Maybe I need to setup the instance running my CD jobs?
The task "run powershell on target machines" will start a Enter-PSSession session from your agent machine to target VM.
As per the error message, on your target VM, please run winrm quickconfig to configure the service, run winrm set winrm/config/client '#{TrustedHosts="*"}' to trust the host.
If you use the HTTPS, then you need to open port 5986 in the firewall and install a self-signed certificate.
If you create a classic virtual machine from the Azure portal,the virtual machine is already set up for WinRM over HTTPS, with the default port 5986 already opened in the firewall and a self-signed certificate installed on the machine.These virtual machines can be accessed with no further configuration required. Please refer to the task perquisite for the detials.
Or you can use self-hosted agent, with http port 5985 for the task.
I had the same problem and I solved the problem by using azure cli.
The build agent and the VM were added in the same vNet.
az vm run-command invoke -g MyResourceGroup -n MyVm --command-id RunShellScript --scripts 'echo $1 $2' --parameters hello world
https://learn.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest#az-vm-run-command-invoke

Error when running winrm in jenkins pipeline

Connecting to remote server 172.18.32.39 failed with the following error message : The WinRM client
14:53:09 cannot process the request. Default authentication may be used with an IP address under the following conditions: the
14:53:09 transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use
14:53:09 winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more
14:53:09 information on how to set TrustedHosts run the following command: winrm help config. For more information, see the
14:53:09 about_Remote_Troubleshooting Help topic.
I'm getting the above winrm error when the test-wsman command is called from my build node against my test node. When I manually run the test-wsman command from the build node against the test node, it works. I guess besides that oddity, has anyone had success resolving the above error? Is there any way I can get more output from winrm to troubleshoot this?
Add Host to TrustedHosts
Can you run the following commands in Powershell on the node that the pipeline is running on
winrm quickconfig
winrm set winrm/config/client #{TrustedHosts="172.18.32.39”}
Afterwards try running the pipeline again. Also, can you verify that you are running the test-wsman cmdlet from the same user that’s running it when it’s ran via the pipeline?

WinRM not returning client information - quickconfig not working

I am trying to change settings on WinRM however I keep getting an error.
I have read many articles across the web about it but I cannot get any of the suggested fixes to work
When I try to get the winRM quickconfig, which is the common suggested fix, to work I get this:
PS C:\Windows\system32> winrm qc
WinRM service is already running on this machine.
WSManFault
Message = The client cannot connect to the destination specified in the request. Verify that the service on the dest
ination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running o
n the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the
destination to analyze and configure the WinRM service: "winrm quickconfig".
Error number: -2144108526 0x80338012
The client cannot connect to the destination specified in the request. Verify that the service on the destination is run
ning and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destinat
ion, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination t
o analyze and configure the WinRM service: "winrm quickconfig".
The service is running and the firewall rules are all set correctly.
I get the same error regardless of what I'm trying to achieve.
E.G.
PS C:\Windows\system32> winrm get winrm/config
WSManFault
Message = The client cannot connect to the destination specified in the request. Verify that the service on the dest
ination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running o
n the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the
destination to analyze and configure the WinRM service: "winrm quickconfig".
Error number: -2144108526 0x80338012
The client cannot connect to the destination specified in the request. Verify that the service on the destination is run
ning and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destinat
ion, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination t
o analyze and configure the WinRM service: "winrm quickconfig".
PS C:\Windows\system32> winrm enumerate winrm/config/listener
WSManFault
Message = The client cannot connect to the destination specified in the request. Verify that the service on the dest
ination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running o
n the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the
destination to analyze and configure the WinRM service: "winrm quickconfig".
Error number: -2144108526 0x80338012
The client cannot connect to the destination specified in the request. Verify that the service on the destination is run
ning and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destinat
ion, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination t
o analyze and configure the WinRM service: "winrm quickconfig".
I have tried stopping and restarting the Windows Remote Management service in the local Services.
Also tried changing the settings in the Group Policy on the machine. All to no avail.
Checked the expanded analysis logs in the event viewer, no further clues there, it just shows the error above.
Is there anyway to uninstall and reinstall ? I cannot find any info on that, online to configure. I don't seem to be able to get back to the initial setup like this (example from another machine)
PS P:\> winrm qc
WinRM is not set up to receive requests on this machine.
The following changes must be made:
Start the WinRM service.
Set the WinRM service type to delayed auto start.
Make these changes [y/n]? n
I am current using :
Windows 2008 R2 SP1
Powershell 5.1
TFS 2017 is also installed on the server.
Any ideas ?
You know... I could point you to some links that would help, but that always aggravates me when I'm looking for an answer. Try this from an elevated powershell prompt:
Set-NetConnectionProfile -NetworkCategory Private <-- This assumes you're not in a domain
Enable-PSRemoting -SkipNetworkProfileCheck -Force <- Yes, a bit redundant
set-service -ServiceName WinRm -StartupType Automatic
start-service winrm
If you continue to have trouble, add this:
winrm set winrm/config/client '#{TrustedHosts="NameOfYourComputer"}'

Issues configuring WinRM while deploying a Windows VM on Azure via API

When deploying on Azure via REST API a VM with Windows2008-SQL-Server template, we need to configure WinRM with a powershell script to enable basic auth. For doing so we use the Azure extension 'CustomScriptExtension' that loads a script from our blobstorage and runs it.
The script's output has errors and Powershell doesn’t respond on port 5985.
We use the SQL-Server-2008R2-SP2-10.50.4319.0-Standard-ENU-Win2008R2 template.
In the deployment we set a listener that uses as protocol ‘Http’.
We open the 5985 port and at the end of the VM provisioning we are able to open a socket on it.
Code:
winrm quickconfig -q
winrm set winrm/config/winrs '#{MaxMemoryPerShellMB="300"}'
winrm set winrm/config '#{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '#{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '#{Basic="true"}'
For each script's command we have the following error:
Message = Access is denied. \\n\\nError number: -2147024891 0x80070005\\nAccess is denied.
Once the provisioning of the VM is done, we can log in using Remote Desktop and if we launch the powershell script from there, it perfectly works.
After some research we have found this article that proposes a possible solution, but the problem with it is that it exposes a security issue, because the script needs an admin password as parameter.
We are looking for other advice or for a workaround. We are working with a full automated workflow so sadly “hand-made solutions” are not an option for us. Thanks in advance for any help.
I've done this to make the Chef client bootstrap work on an Azure Windows VM. In my case, in addition to what you've done, I had to open 5985 on the Windows Firewall and enable the 5985 endpoint on the cloud service. It was a PITA, but it got the job done.

Using C# API to connect to Powershell Remoting

Looking for help with a powershell api connectivity issue. The following code works for all customers, however, for one of the customers, it is returning an error:
var context = new WSManConnectionInfo(new Uri(uri),
"http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
new PSCredential(username, securePassword))
{
SkipCACheck = true,
SkipCNCheck = true
};
using (var pool = RunspaceFactory.CreateRunspacePool(1, counterDefinitions.Count, context))
{
pool.Open();
pool.Open() line returns the following exception:
A first chance exception of type
'System.Management.Automation.Remoting.PSRemotingTransportException'
occurred in System.Management.Automation.dll
Additional information: Processing data from remote server
xxx.xx.xx.xx failed with the following error message: The request for
the Windows Remote Shell with ShellId some-guid-number failed because
the shell was not found on the server. Possible causes are: the
specified ShellId is incorrect or the shell no longer exists on the
server. Provide the correct ShellId or create a new shell and retry
the operation. For more information, see the
about_Remote_Troubleshooting Help topic.
If one is to examine the Intellitrace of the exceptions, the one exception thrown a little higher in the stack is as follows:
"The client cannot connect to the destination specified in the
request. Verify that the service on the destination is running and is
accepting requests. Consult the logs and documentation for the
WS-Management service running on the destination, most commonly IIS or
WinRM. If the destination is the WinRM service, run the following
command on the destination to analyze and configure the WinRM service:
"winrm quickconfig". " (System.Runtime.InteropServices.COMException) A
System.Runtime.InteropServices.COMException was thrown: "The client
cannot connect to the destination specified in the request. Verify
that the service on the destination is running and is accepting
requests. Consult the logs and documentation for the WS-Management
service running on the destination, most commonly IIS or WinRM. If the
destination is the WinRM service, run the following command on the
destination to analyze and configure the WinRM service: "winrm
quickconfig". "
Any thoughts as to what could be the problem? Customer has verified that winrm is running and configured, and that firewall has an open port, etc.
Server is running Windows Server 2012, as a VM on Azure
You may need to configure the TrustedHosts
http://support.microsoft.com/kb/555966