Share via


Get-Content returning "Get-Content : Access to the path '...' is denied".

Question

Wednesday, February 8, 2012 12:38 PM

I am trying to run the Get-Content Powershell command, trying to find a certain key word in a file structure, and returning the error:

Get-Content : Access to the path 'E:\wwwroot\aafys' is denied.At line:1 char:12+ get-content <<<<  -path "E:\wwwroot\*" | select-string -pattern "eva1fYlbakBcVSir"    + CategoryInfo          : PermissionDenied: (E:\wwwroot\aafys:String) [Get-Content], UnauthorizedAccessException    + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

The exact command I am running is:

Get-Content -Path "E:\wwwroot\" | Select-String -Pattern "eva1fYlbakBcVSir"

I have checked all the permissions, even going to the extent of setting one of the folder full access to everyone, and running the ISE as administrator, and nothing works.

What am I missing? Please help someone :-)

Thanks in advance

Kevin

All replies (10)

Wednesday, February 8, 2012 1:44 PM ✅Answered | 1 vote

ok, so Get-ChildItem is what you are looking for if its just a file name.

 

gci c:\wwwroot *eva1fYlbakBcVSir* -recurse

 that will find any files that contain that name, if you want to look inside

of files for that, it’s a little different... still possible, just the

tricky part is not searching binary files.

 

do you need to look inside files? do you know if it would have a certain

extension or size?

 

 

Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, February 8, 2012 1:48 PM ✅Answered | 1 vote

if you look at my other post you can see its even easier than that...

 

one thing I'll note on what you've done is that you are thinking more so of

the old cmd prompt days, which generally works, but you'll want to think of

the arguments provided to powershell

 

you'll notice with gci there are a bunch of ways to filter...

 

so when you provide a path as you have with the c:\wwwroot\ you don’t

actually need the * since you are using recurse.. not even sure the * will

do anything there...

 

gci -path c:\ps -filter *test* -Recurse

 

thinking of it this way will help in the future when using new cmdlets

 

 

Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, February 8, 2012 2:22 PM ✅Answered | 1 vote

The OP want to search the content of multiple files in a folder structure.  As Get-Content doesn't have -Recurse option, you would have to use gci and gc together, like so:

Get-ChildItem 'c:\scripts' -Recurse -Include *.txt | Get-Content

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)


Thursday, February 9, 2012 6:22 AM ✅Answered | 4 votes

Hi,

The following should also work:

Get-ChildItem e:\wwwroot -Recurse -Include *.* | Select-String "eva1fYlbakBcVSir"


Wednesday, February 8, 2012 12:48 PM

Looks like the problem is with your get-content path spec. 

"E:\wwwroot\" 

is going to match anything that doesn't have an extension, which means you're trying to do a get-content on directory objects. 

"E:\wwwroot\.*"  would probably work better.  If you have dotted directory names you may need to use a fliter

E:\wwwroot\.* | where {-not $_.psiscontainer}|

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Wednesday, February 8, 2012 1:23 PM

Thanks for your reply,

I have tried with the -Path "E:\wwwroot\.*", which works, but found it will only search that level.

I need to do it in a way where it can follow the folder structure all the way through


Wednesday, February 8, 2012 1:26 PM

are you talking about the file structure (files folders) or data within a

file?

 

get-content is for reading a file, where as get-childitem (gci) is for

looking at files/folders

 

can you provide more details on what it is you are trying to do?

 

 

Justin Rich
http://jrich523.wordpress.com
PowerShell V3 Guide (Technet)
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, February 8, 2012 1:31 PM

Hi,

Basically, I have a webserver which is suspected to have malware.  I have been given a key word (eva1fYlbakBcVSir), which should indicate the malware in a particular script.

What I am trying to do, is search all the files below wwwroot for any instance of the keyword, so I can hand back over to the webdev team to be cleaned up.

Kev


Wednesday, February 8, 2012 1:41 PM

Cheers for the heads up with GCI jrich,

Just had a play around with it, and it appears to be doing exactly what I want ...

Get-ChildItem E:\wwwroot\* -Recurse | Select-String -Pattern "eva1fYlbakBcVSir"

Thanks for your time guys, much appreciated.

Kev


Thursday, February 9, 2012 7:32 AM

Hi,

The following should also work:

Get-ChildItem -Recurse -Include *.* | Select-String "eva1fYlbakBcVSir"

Yes, a good call.  Select-String is very fast.

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)