Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New-PSSession to localhost fails

I have a script that opens a remote session to the localhost. I need this to install NuGet on some devices from within a logonscript.

$Username = "Admin"  
$Password = ConvertTo-SecureString ‘adminPW’ -AsPlainText -Force
$adminCredential = New-Object System.Management.Automation.PSCredential $Username, $Password
$Session = New-PSSession  -Credential $adminCredential
Invoke-Command -Session $Session -ScriptBlock {Install-PackageProvider -Name NuGet -Verbose -MinimumVersion 2.8.5.201 -Force}

Every time I try to run this I get the following error:

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\Mike Holtackers\OneDrive - Foreign Trade Association\Scripts\OutlookSig\getAADconnectionOK.ps1:5 char:12
+ $Session = New-PSSession -ConnectionUri $ConnectionURI -Credential $a ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CannotConnect,PSSessionOpenFailed

Running winrm quickconfig does not change anything...

Following is the output of winrm get winrm/config

PS WSMan:\localhost\Listener\Listener_1084132640> winrm get winrm/config
Config
    MaxEnvelopeSizekb = 500
    MaxTimeoutms = 60000
    MaxBatchItems = 32000
    MaxProviderRequests = 4294967295
    Client
        NetworkDelayms = 5000
        URLPrefix = wsman
        AllowUnencrypted = false
        Auth
            Basic = true
            Digest = true
            Kerberos = true
            Negotiate = true
            Certificate = true
            CredSSP = false
        DefaultPorts
            HTTP = 5985
            HTTPS = 5986
        TrustedHosts = *
    Service
        RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;;;WD)
        MaxConcurrentOperations = 4294967295
        MaxConcurrentOperationsPerUser = 1500
        EnumerationTimeoutms = 240000
        MaxConnections = 300
        MaxPacketRetrievalTimeSeconds = 120
        AllowUnencrypted = false
        Auth
            Basic = false
            Kerberos = true
            Negotiate = true
            Certificate = false
            CredSSP = false
            CbtHardeningLevel = Relaxed
        DefaultPorts
            HTTP = 5985
            HTTPS = 5986
        IPv4Filter = 194.168.254.1-194.168.254.256 [Source="GPO"]
        IPv6Filter [Source="GPO"]
        EnableCompatibilityHttpListener = false
        EnableCompatibilityHttpsListener = false
        CertificateThumbprint
        AllowRemoteAccess = true [Source="GPO"]
    Winrs
        AllowRemoteShellAccess = true
        IdleTimeout = 7200000
        MaxConcurrentUsers = 2147483647
        MaxShellRunTime = 2147483647
        MaxProcessesPerShell = 2147483647
        MaxMemoryPerShellMB = 2147483647
        MaxShellsPerUser = 2147483647
like image 745
Docschnitzel Avatar asked Apr 19 '17 14:04

Docschnitzel


People also ask

How do you use new PSSession?

Use a PSSession to run multiple commands that share data, such as a function or the value of a variable. To run commands in a PSSession, use the Invoke-Command cmdlet. To use the PSSession to interact directly with a remote computer, use the Enter-PSSession cmdlet.

How do I use PowerShell to remot?

PowerShell remoting is enabled by default on Windows Server platforms. You can use Enable-PSRemoting to enable PowerShell remoting on other supported versions of Windows and to re-enable remoting if it becomes disabled. You have to run this command only one time on each computer that will receive commands.

What is the purpose of PSSession?

Why Use a PSSession? Use a PSSession when you need a persistent connection to a remote computer. With a PSSession, you can run a series of commands that share data, such as the value of variables, the contents of a function, or the definition of an alias. You can run remote commands without creating a PSSession.

How do you exit-PSSession?

The Exit-PSSession cmdlet ends interactive sessions that you started by using the Enter-PSSession cmdlet. You can also use the exit keyword to end an interactive session. The effect is the same as using Exit-PSSession .


3 Answers

Check if the winrm service is running on your localhost:

PS C:\>  Get-Service winrm | ft -AutoSize

Status  Name  DisplayName                              
------  ----  -----------                              
Running winrm Windows Remote Management (WS-Management)

Otherwise PS remoting won't work, though you've configured via winrm and have enabled PS remoting via Enable-PSRemoting.

like image 136
Moerwald Avatar answered Oct 02 '22 18:10

Moerwald


Issue was someone had tampered with the firewall... Thanx for the help guys!

Basically the firewall GPO was blocking remote management

like image 26
Docschnitzel Avatar answered Oct 02 '22 19:10

Docschnitzel


Following worked in my case:

# NOTE: Following is set by Enable-PSRemoting, it prevents UAC and
# allows remote access to members of the Administrators group on the computer.

Set-ItemProperty -Name LocalAccountTokenFilterPolicy -Value 1 `
        -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System

For more information about this setting see section in about_Remote_Troubleshooting

like image 25
metablaster Avatar answered Oct 02 '22 18:10

metablaster