New to PW - Access Denied

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Adam Adam 2 weeks ago.

  • Author
    Posts
  • #72428
    Profile photo of Adam
    Adam
    Participant

    Hi all, literally my first day dipping in to PowerSHell. I have done ok so far, but i have hit a hurdle. In short I am currently trying to set all AD users account passwords to be set to expire in 90 days. I have been using this script however I keep getting access denied.

    Other scripts have worked fine that are in the c:\ location. I am using Domain admin.

    PS C:\WINDOWS> Import-Module activedirectory

    $list = Import-CSV c:\passwordneverexpires.csv

    forEach ($item in $list) {

    $samAccountName = $item.samAccountName

    Disable-ADAccount -Identity $samAccountName
    }

    Import-CSV : Access to the path 'C:\passwordneverexpires.csv' is denied.
    At line:3 char:9
    + $list = Import-CSV c:\passwordneverexpires.csv
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OpenError: (:) [Import-Csv], UnauthorizedAccessException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand

  • #72434
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Welcome! I hope you're considering "Learn Windows PowerShell in a Month of Lunches" as a resource.

    The error is telling you that whatever user account you're using to run the PowerShell console doesn't have access to that file. I suspect you're not running the shell as Administrator, since users by default don't have access to C: on newer systems.

  • #72479
    Profile photo of Adam
    Adam
    Participant

    Thanks Don,

    I will try again, just bit confused why the other scripts I have run on C;\ have worked fine. Will send an update in a bit.

    I will look at that resource as well.

  • #72484
    Profile photo of Adam
    Adam
    Participant

    Still not working, But my other scripts work when pointing to C:\ for example this works.

    Import-Module activedirectory

    $list = Import-CSV c:\disableusers.csv

    forEach ($item in $list) {

    $samAccountName = $item.samAccountName

    Disable-ADAccount -Identity $samAccountName
    }

    • #72485
      Profile photo of Justin Mathews
      Justin Mathews
      Participant

      Have you looked at the permissions on the file? One thing I've done before when testing something over and over is leave the file open. I think the error message is different, like "path not found" but still make sure it's closed, make sure you have permissions by looking at the permissions tab and try moving it to your desktop or something. By the way, the Month of Lunches book is totally worth it.

  • #72487
    Profile photo of Adam
    Adam
    Participant

    Thanks I shall have a look. If i move to the desktop what command would i use to point to it in PS.

    sl Desktop

  • #72491
    Profile photo of Adam
    Adam
    Participant

    Very odd, I decided to re create the excel file and it's worked.

    thanks all

You must be logged in to reply to this topic.