Get-Content permissions error

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
sekou2331
Posts: 263
Joined: Wed Aug 15, 2012 6:07 am

Get-Content permissions error

Post by sekou2331 » Tue Jul 02, 2013 7:41 am

This may be a noob question but has anyone have an issue when trying to use get-content on remote computer through UNC.I am admin of all computers on my network. Also i can access the same UNC path through Explorer.

\\computername\C$\username\

User avatar
jvierra
Posts: 13717
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get-Content permissions error

Post by jvierra » Tue Jul 02, 2013 9:08 am

What is the error and what kind of content are you trying to get?

User avatar
sekou2331
Posts: 263
Joined: Wed Aug 15, 2012 6:07 am

Re: Get-Content permissions error

Post by sekou2331 » Tue Jul 02, 2013 9:42 am

Anything under the users profile. The funny thing is I can use copyItem on the same path to another computers path and it works

Get-Content : Access to the path '\\Computername\c$\Users\Username' is denied.
At line:1 char:1
+ Get-Content "\\Computername\c$\Users\Username"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\\Computername\c$\Users\Username:String) [Get-Content], UnauthorizedAccessExceptio
n
+ FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

User avatar
jvierra
Posts: 13717
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get-Content permissions error

Post by jvierra » Tue Jul 02, 2013 9:48 am

Get-Content opens files and not folders.

You must specify a file name.

This is not a correct file name: \Computernamec$UsersUsername

Start by reading up in how to use UNC paths.

User avatar
livefortechnology
Posts: 22
Joined: Sun Mar 03, 2013 12:45 pm

Re: Get-Content permissions error

Post by livefortechnology » Thu Jul 18, 2013 8:29 pm

I've never been able to get Get-content to work via UNC so I don't think the op is crazy (nor did his string look anything like what was quoted - his UNC looks correct).

Okay gurus, so what's a working one-liner to get-content from a unc-based file path that works?

User avatar
jvierra
Posts: 13717
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Get-Content permissions error

Post by jvierra » Fri Jul 19, 2013 3:23 am

I have many domains WWS2003,WS2008,WS2008R2,WS2012. All domains and all OS types -XP/7/8 are able to use Get-Content with a UNC. I have numerous scripts that retrieve information from log files using this method. It has always worked.

Perhaps you have firewall issues or trust issues.

The UNC posted by the original OP was pointed at a folder. Look closely and think about what that UNC is:

"\Computernamec$UsersUsername"
This is bad UNC all the way. It is missing a leading '\' and is missing other '\' characters. I suspect it should be:
"\\Computername\c$\Users\Username"
This would also be a bad UNC for a file as it points to a folder.

Even if it were a file the users\username folder on Vista and later do not allow admin access by default. You must explicitly request to have the admin added to the permissions. If you browse the folder with Explorer from the machine you will be asked if you want to give administrators access. When you answer yes the permissions are permanently altered to allow admin access.

Understanding how these things work is the purpose of MCP certification. It is always worthwhile to, at a minimum, get the course book and read it. You will learn much about each new OS that MS produces.

With all of this inmind go back and try to access a simple file over a UNC. Start by creating a folder you know you have access to and place a text file in the folder. Be sure you can see the file on the UNC via this:

dir \\computer\c$\temp\test.txt
If that works then do this:

Get-Content \\computer\c$\temp\test.txt

If that fails then turn off the firewall on the remote machine and try again.

If that fails still then place a support call with Microsoft as you have a bad system or a bad configuration.

Locked