Solved

UnauthorizedAccessException in powershell using Clear-Content

Posted on 2013-12-19
13
923 Views
Last Modified: 2013-12-21
I have a log file that I want to clear each day as part of running a script.  However I get an UnauthorizedAccessException even though the script is running as Administrator and Security on it is Full Control for Everyone

I'm stumped, what do I need to do?
0
Comment
Question by:c7c4c7
  • 7
  • 6
13 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
It might be in access by AntiVirus software, or another process. Did you check that yet?
0
 

Author Comment

by:c7c4c7
Comment Utility
Anti Virus is not the problem, I tried that.   Another process - only process that access it is the one that creates it and I disabled that to see if it was causing the problem, no change in outcome.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Is the file local or on a share?
Are you able to delete/change/rename the file manually?
If loacal, check with Process Explorer or handle.exe (both www.sysinternals.com) whether the file is not open anymore. handle.exe might be easier for that purpose, as you just have to provide a part of the filename to get all open handles for that file.
0
 

Author Comment

by:c7c4c7
Comment Utility
Is the file local or on a share? - Local
Are you able to delete/change/rename the file manually? - Yes
If loacal, check with Process Explorer or handle.exe - No matching handles found.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Are you able to issue the Clear-Content manually?
If so, the script's autorization has to be the issue.
0
 

Author Comment

by:c7c4c7
Comment Utility
Are you able to issue the Clear-Content manually? - Nope same result
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Then the following shouldn't work either:
"" > C:\logfile
Clear-Content c:\logfile -force

Open in new window

with the proper path and filename, of course.
0
 

Author Comment

by:c7c4c7
Comment Utility
What does this do

"" > C:\logfile
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Overwrite your log file with an empty string - (almost) the same as Clear-Content should do.
0
 

Author Comment

by:c7c4c7
Comment Utility
Access to Path is denied
UnauthorizedAccessException
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Strange. Rename or delete the logfile, and then try each of both commands again, deleting the log file between the test.
We try to find out whether it is file create or overwrite issue.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
Thanks for the points, but I can't see a solution here?
0
 

Author Comment

by:c7c4c7
Comment Utility
Don't know what it was.  Deleted file, let it get recreated and bingo it worked.

No restart in between, no changes to security .. it just started working
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now