PowerShell Remoting when proxy server is used for LAN - powershell

A Proxy server is used for the LAN connectivity in my machine and when i try to create a remote powershell session using the command Enter-PSSession in my machine, powershell console throws the following exception,
First Try: Enter-PSSession -ComputerName server.com -Credential domain\user
Now, I tried along with -UseSSL option. Below is the exception i received this time,
Second Try: Enter-PSSession -ComputerName server.com -Credential domain\user -useSSL
Can anyone please let me know how to create remote powershell session when in proxy LAN connection ?

In order to set proxy settings on connections for PS Remoting, use the New-PSSessionOptions commandlet and save the results as an object. $sessionOptions = New-PSSessionOptions [...] (you may need to research the options to specify there)
Then, use this object as a parameter on the -SessionOptions switch

Related

How does authentication work when using Invoke-Command?

I'm looking for a good reference to understand what actually behinds the scene of Invoke-Command. Does this retrieve current logged user context to run command remotely? So saying I'm a domain admin who has administrative rights on target joined computers so what I'd need to do just like
Invoke-Command -ComputerName mypc -FilePath "C:\Script\stuff.ps"
Connecting to remote serve mypc failed with the following error
message : WinRM cannot complete the operation. Verify that the
specified computer name is valid, that the computer is accessible over
the network, and that a firewall exception for the WinRM service is
enabled and allows access from this computer. By default, the WinRM
firewall exception for public profiles limits access to remote
computers within the same local subnet.
If I try with the following code snippet I'm able to access to the pc
$so = New-PsSessionOption –SkipCACheck -SkipCNCheck
Enter-PSSession -ComputerName mypc -Credential contoso\admin -UseSSL -SessionOption $so -Authentication Kerberos
The error output from Invoke-Command seems to indicate that it uses WinRm but I don't know the main difference between the first compared with the second one.

Remote Execution of a PowerShell script results in "The WinRM client cannot process the request. [...] HTTPS transport must be used [...]"

I have written a PowerShell script which uninstall a program and install a newer version of the program on my servers (Update Programs). Now I want to create another script which run the aforementioned script on the servers. Consider that I have to connect to my servers through using IPs, UserName and password and using domain is not an option.
How is this possible?
PowerShell version is 4
I have tried this code to simply get date:
$User = "administrator"
$PWord = ConvertTo-SecureString -String "Password1234" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $PWord
$session = New-PSSession -ComputerName '10.60.60.100' -Credential $Credential
Invoke-Command -Session $session -ScriptBlock {Get-Date}
and I got this error:
New-PSSession : [10.60.60.100] Connecting to remote server 10.60.60.100 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 can get more information about that by running the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
This is because you’re not running your command from a trusted host, or because the remote computers wsman service isn’t configured properly. I’d start by running the following command to configure wsman on the remote machine:
wsman quickconfig
If that doesn’t fix the problem, then you need to add your computer to the remote machines trusted hosts. You can do that by running the following:
winrm s winrm/config/client '#{TrustedHosts="RemoteComputer"}'

How to infer PowerShell Enter-PSSession arguments from an .rdp file?

I normally use Windows Remote Desktop to connect to a server machine. All connection configs are saved in an .rdp file.
I want to instead use PowerShell to connect to that same server, in a similar fashion as SSH. I researched and found about PowerShell remoting with Enter-PSSession, but I don't know what arguments to give it.
What I know so far:
Client machine OS: Windows 10. Host machine OS: Windows Server 2012 R2.
Client and host machines are in different networks/Active Directories. Simply running Enter-PSSession <HOSTNAME> doesn't work.
Host machine's PSRemoting is enabled. If I'm on a machine in its same AD, it can be connected with Enter-PSSession <HOSTNAME>
My question is, if I have a working .rdp file, can I infer what's needed to PSRemote to a remote server? Or even better, can I pass that .rdp file to a PS command to make the shell connection?
If trust is present, this should work.
Enter-PSSession -Computername <FQDN>
If no trust, you have to pass a PowerShell credential object. This should be a credential that has access on the target machine. The .rdp file cannot help at all.
Enter-PSSession -Computername <FQDN> -Credential $CustomPScredentialObject
You can create a credential object by:
$CustomPScredentialObject = Get-Credential "Domain\UserID" #this will give an interactive prompt for password
Non-interactive Credential Object:
$SecurePassword = "PlaintextPassword" | ConvertTo-SecureString -Force -AsPlainText
$CustomPScredentialObject = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "Domain\userid", $SecurePassword
Now you can use this credential object for PSSession.

Get 'Access denied' on Invoke-Command for administrator

I have follow issue: I trying to run remote command on my server (windows server 2012 r2) via powershell command, powershell script looks follow
$password = ConvertTo-SecureString $pass -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PsCredential($deployadmin,$password)
$scriptBlock1 = {Get-NetAdapter}
Invoke-Command -computername $server -Credential $credentials -scriptblock $scriptBlock1
and I've get an error 'Access is denied'
I've tryied to run on server Enable-PSRemoting for allow remote connection.
I use credential for user that is Administrator on that server.
Strange thing, that this command is succeeds for credentials of another user on this server, those user is also Administrator.
What I'm missing ?
Thank for any advice
Update:
command Test-WSMan $server is succeeds
try command winrm quickconfigthe system suggested setting up a remote access, after the configuration, the Invoke-Command command was executed without errors
I would be grateful if anyone would explain this behavior
Fun!
When you execute winrm quickconfig the following happens:
Starts the WinRM service
Set the WinRM service type to auto start
Create a listener to accept requests on any IP address
Enable firewall exception for WS-Management traffic (for http only)
This article has additional detail.

Powershell remoting error - network path not found

I cannot connect to remote server using enter-pssession -computername serverA. My scenario:
I have 2 Win 2003 R2 servers in the same domain. ServerA is WSUS server, serverB is a domain controller
Both servers have enabled powershell remoting
Both servers have configured winrm (winrm quickconfig)
Both servers have TrustedHosts set to *
setspn.exe is set up correctly (http, https, wsman etc.)
Both servers have FireWall turned off
Both servers have PowerShell 2.0
I am trying to enter-pssession -computername serverA under the domain admin credentials from serverB to serverA and it throws the following error:
"""Enter-PSSession : Connection to remote server failed with the following error message : WinRM cannot process the request. The following error occured while using Kerberos authentication: The network path was not found."""
When I try to enter-pssession -computername serverB under the domain admin credentials from serverA it works fine! It also works if I use localhost so: enter-pssession -computername localhost under the domain admin credentials (on serverA) works as well, but when I try the hostname on serverA (instead of localhost) enter-pssession -computername serverA it throws the same error.
I also tried to use get-credential and provide different types of credentials, but it did not help. The only thing which helped was using a local (not domain) administrator account and running enter-pssession -computername serverA -credentials $cred and it worked, but only locally, I was able to do this from local machine (from serverA to itself) but not from serverB to serverA under the serverA\administrator credentials.
Any ideas?
Thanks
First of all I created credential variable with my domain admin account:
$cred = get-credential - I typed my domain\username and password
Then I used IP address instead of hostname in -ComputerName parameter, so the enter-pssession looks like:
Enter-Pssession -ComputerName 192.168.1.111 -Credential $cred
this approach works for the invoke-command as well
invoke-command -ComputerName 192.168.1.111 -Credential $cred -ScriptBlock {hostname}
I still do not know why it does not work with the hostname and why do I have to create $cred, but as I need a quick solution, this works fine for me.
Thanks for help.
I had the exact same issue. Using the FQDN worked for me.
Chris N is right:
The network path was not found.
This is clearly a DNS resolution error; especially if the IP address is working. I would venture to say there are Name Suffix Routing issues.
The ComputerName description says that NETBIOS name should work, but it does not in my testing in my environment. The FQDN is another option for the -ComputerName property and fixed this error for me.
Try using (use your FQDN, of course):
Invoke-Command -ComputerName servera.vertigion.com
Note: Notice it's in all lowercase. Using camel case (serverA.vertigion.com) failed with the same error. I realize that typically nslookups are case-insensitive.
Note: I did NOT have the issue with the Enter-PSSession command. I believe there's a bug (or at least a blatant inconsistency) with Invoke-Command.
More info: http://go.vertigion.com/PowerShell_Invoke-Command
It sounds like the issue is with name resolution. You can confirm this by pinging ServerA from ServerB. If it fails you could work from there. Try pinging by FQDN (servera.mydomain.com) or by IP.
See more info here. I ran into this in a pair of servers that had been working but then spontaneously stopped and started issuing the OP's error message. I rebooted the target server and that got things working again.
Set TCP/IP NETBIOS service to auto start, and enable service.
also allow NETBIOS in adapter properties if needed.