Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

UnauthorizedAccessException in powershell using Clear-Content

Posted on 2013-12-19
13
Medium Priority
?
1,120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 71

Expert Comment

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

Author Comment

by:c7c4c7
ID: 39729650
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 71

Expert Comment

by:Qlemo
ID: 39729678
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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 

Author Comment

by:c7c4c7
ID: 39729713
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 71

Expert Comment

by:Qlemo
ID: 39729731
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
ID: 39729750
Are you able to issue the Clear-Content manually? - Nope same result
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39729810
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
ID: 39729843
What does this do

"" > C:\logfile
0
 
LVL 71

Expert Comment

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

Author Comment

by:c7c4c7
ID: 39729892
Access to Path is denied
UnauthorizedAccessException
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 39729923
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 71

Expert Comment

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

Author Comment

by:c7c4c7
ID: 39733875
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

618 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