Why can't I read this file on a remote server in Powershell?

Viewed 385

I launched a couple servers in AWS and I want to read the contents of the a file using Powershell.

On server 1 I create a file called App_configuration.txt and placed in the root of c:\.

The file has just 2 lines in it:

<Path>C:\GoodToGo</Path>
<Path>C:\GoodToGo2</Path>

I'm trying to access the file with this command using the remote IP of the server. The xx.xxx.xx.xxx is supposed to be the remote IP:

Get-Content -Path "\\xx.xxx.xx.xxx\c$\App_configuration.txt"
Get-Content : Cannot find path '\\xx.xxx.xx.xxx\c$\App_configuration.txt' because it does not exist.
At line:1 char:1
+ Get-Content -Path "\\xx.xxx.xx.xxx\c$\App_configuration.txt"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\xx.xxx.xx.xxx\c$\App_configuration.txt:String) [Get-Content], ItemNot
   FoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

I can RDP into the remote server and list the file:

PS C:\> ls C:\App_configuration.txt


    Directory: C:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         3/2/2021   4:12 PM             51 App_configuration.txt

I am not the same user on my local machine as the one that can log into the remote machine. The server I am trying to access is in AWS, and I am trying to access it from my local machine.

Why can't I read this remote file with this powershell line?

3 Answers

It seems like the PowerShell cannot access the file at the specified location. So first verify that the file is there and is accessible.

Test-Path "\\xx.xxx.xx.xxx\c$\App_configuration.txt"

If Test-Path cannot return True, then the file cannot be found or the user doesn't have access to get the file from the server. There is no point in using Get-Content until the file can be found.

Also last time when I had the same issue, it was the classic Kerberos delegate/double hop issue. It can't enumerate the path because it can't delegate your credentials, so that's what I'd check first. Try using the same method to connect to just the root of the computer - I'd expect you to get a credential error.

If you're able to resolve this problem by enabling CredSSP for second hop.

Ok, what happens if you try this:

$server = "xx.xxx.xx.xxx"
$user = "$server\your_User_on_xx.xxx.xx.xxx"
$passw = ConvertTo-SecureString 'your_password_on_xx.xxx.xx.xxx' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $user, $passw

Invoke-Command $server {
    Get-Content "C:\App_configuration.txt"
} -Credential $cred

You will need to create access permissions on server 1 for the root of C, which is not the default setup.

To grant access to just the root, run this from an adminstrator account:

ICACLS "C:\" /grant:r "Authenticated Users"

If you want to grant access to the entire drive, use:

ICACLS "C:\" /inheritance:e /grant:r "Authenticated Users"

The "Authenticated Users" group should include powershell remote users as long as you have enabled powershell remoting, which appears so based on the error message.

Related