2

This should be absuredly easy but I keep running into a brick wall. I am running a Powershell script which performs an audit of my tasks and saves the result to a CSV. I keep getting a permissions fault though and I can not figure out what I am missing.

Here is my code:

Get-ScheduledTask | 
    Where State -ne "Disabled" | 
    Get-ScheduledTaskInfo | 
    Select TaskName,TaskPath,LastRunTime, LastTaskResult,NextRunTime,NumberofMissedRuns | 
    Where { $_.TaskName -like "test_*"} |
    Export-Csv -NoTypeInformation -Path C:\temp\scheduled_tasks_audit

Here is what I am getting in response:

Export-Csv : Access to the path 'C:\temp\scheduled_tasks_audit' is denied.
At C:\temp\scheduled_tasks_audit\example_task_audit.ps1:6 char:5
+     Export-Csv -NoTypeInformation -Path C:\temp\scheduled_tasks_audit
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], UnauthorizedAccessException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

I checked the user that Powershell was running under using:

[Environment]::UserName

And made sure that the account had full permissions for the destination folder but I'm still getting a access denied error.

I am running Windows Server 2012 R2 and Powershell 4.0

2 Answers 2

4

Your path parameter seems to be pointing to the name of the folder that you're running the script from, so you run the script and it tries to create a file called C:\temp\scheduled_tasks_audit which already exists as a folder. If you run Get-Help Export-CSV -Full you can see the Path parameter requires the name of the file

 -Path [<String>]
        Specifies the path to the CSV output file. This parameter is required.

        Required?                    false
        Position?                    1
        Default value                none
        Accept pipeline input?       false
        Accept wildcard characters?  false

Change your code to

Get-ScheduledTask | 
    Where State -ne "Disabled" | 
    Get-ScheduledTaskInfo | 
    Select TaskName,TaskPath,LastRunTime, LastTaskResult,NextRunTime,NumberofMissedRuns | 
    Where { $_.TaskName -like "test_*"} |
    Export-Csv -NoTypeInformation -Path C:\temp\scheduled_tasks_audit\output.csv

and you should be good to go.

2

You need to specify a filename at the end of your -Path variable, like this:

C:\temp\scheduled_tasks_audit\audit.csv

2
  • Wow, I feel stupid. I knew it was something simple. Thanks for the help! Feb 22, 2016 at 20:14
  • No problem at all - we all need some occasional nonjudgmental help when it comes to Powershell!
    – vigilem
    Feb 22, 2016 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .