PowerShell / WindowsCompatibility

Module that allows Windows PowerShell Modules to be used from PSCore6
Other
137 stars 33 forks source link

Import-WinModule returns error with `Get-WebRequest` #72

Closed DanAtkinson closed 5 years ago

DanAtkinson commented 5 years ago

I posted an issue on the PowerShell Core board regarding the inability to use Get-WebRequest with PowerShell Core.

A suggestion that was provided was to install the WindowsCompatibility module. However, I tried this and it failed.

PS D:\> Install-Module WindowsCompatibility -Scope CurrentUser
PS D:\> Import-WinModule WebAdministration
New-PSSession : [localhost] Connecting to remote server localhost 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.
At C:\Users\Dan\Documents\PowerShell\Modules\WindowsCompatibility\1.0.0\WindowsCompatibility.psm1:232 char:20
+         $session = New-PSSession @newPSSessionParameters | Select-Obj ...
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OpenError: (System.Management.A\u2026tion.RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : CannotConnect,PSSessionOpenFailed
PS D:\> Get-WebRequest
Get-WebRequest : The term 'Get-WebRequest' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-WebRequest
+ ~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Get-WebRequest:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

I checked and IIS is set up just fine, has several sites running on it and the default site is still there. I can also browse to it without problems.

Am I missing something? Should this not just work?

Output from $PSVersionTable.

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
SteveL-MSFT commented 5 years ago

@DanAtkinson When using this module with Windows PowerShell on the same machine it works?

DanAtkinson commented 5 years ago

@SteveL-MSFT If I run it from my normal PowerShell, I get the following:

PS D:\> Import-WinModule WebAdministration
Import-WinModule : The term 'Import-WinModule' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Import-WinModule WebAdministration
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Import-WinModule:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

If I run Get-WebRequest, I'll get a full breakdown of the current requests (apart from the url and hostname which have been redacted).

requestId       : ef0000008000015b
connectionId    : ef00000060000150
verb            : GET
url             : /<redacted>
siteId          : 3
localPort       : 80
hostName        : <redacted>.com
clientIpAddress : 127.0.0.1
timeElapsed     : 172
pipeLineState   : 32
timeInState     : 172
currentModule   : Session
timeInModule    : 172
localIpAddress  : 127.0.0.1
Attributes      : {requestId, connectionId, verb, url...}
ChildElements   : {}
ElementTagName  : request
Methods         :
Schema          : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema

Output of $PSVersionTable:

Name                           Value
----                           -----
PSVersion                      5.1.17763.316
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.316
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
SteveL-MSFT commented 5 years ago

@DanAtkinson did you run Enable-PSRemoting from Windows PowerShell on that machine?

DanAtkinson commented 5 years ago

@SteveL-MSFT Yes, that appears to have been the problem. I'm not sure why I needed to enable it if I'm only checking local connections, and Windows PowerShell doesn't complain.

Either way, the problem does appear to be resolved now. Thank you!

D:\
λ powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS D:\> Enable-PSRemoting
WinRM is already set up to receive requests on this computer.
WinRM is already set up for remote management on this computer.
PS D:\> exit
λ D:\PowershellCore\pwsh
PowerShell 6.2.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

PS D:\> [bool](Test-WSMan -ComputerName $env:COMPUTERNAME -ErrorAction SilentlyContinue)
True
PS D:\> Import-WinModule WebAdministration
PS D:\> Get-WebRequest
PS D:\>

It might be worth asking users to run [bool](Test-WSMan -ComputerName $env:COMPUTERNAME -ErrorAction SilentlyContinue) beforehand in order to check that remoting is enabled.