Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1155
  • Last Modified:

UnauthorizedAccessException in powershell using Clear-Content

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
c7c4c7
Asked:
c7c4c7
  • 7
  • 6
1 Solution
 
QlemoC++ DeveloperCommented:
It might be in access by AntiVirus software, or another process. Did you check that yet?
0
 
c7c4c7Author Commented:
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
 
QlemoC++ DeveloperCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
c7c4c7Author Commented:
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
 
QlemoC++ DeveloperCommented:
Are you able to issue the Clear-Content manually?
If so, the script's autorization has to be the issue.
0
 
c7c4c7Author Commented:
Are you able to issue the Clear-Content manually? - Nope same result
0
 
QlemoC++ DeveloperCommented:
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
 
c7c4c7Author Commented:
What does this do

"" > C:\logfile
0
 
QlemoC++ DeveloperCommented:
Overwrite your log file with an empty string - (almost) the same as Clear-Content should do.
0
 
c7c4c7Author Commented:
Access to Path is denied
UnauthorizedAccessException
0
 
QlemoC++ DeveloperCommented:
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
 
QlemoC++ DeveloperCommented:
Thanks for the points, but I can't see a solution here?
0
 
c7c4c7Author Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now