Announcement

Collapse
No announcement yet.

Export of Password Expiry script runs fine in cmd but not as scheduled task

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Export of Password Expiry script runs fine in cmd but not as scheduled task

    Hi
    We are using Fine grained Password Policy. I have created a script to list all active users password expiry date.
    However when I export the script to csv/txt/xml from scheduled task only the first line is exported.
    When I run the script from command line the export to file is running as it should.

    What is going wrong?

    Code:
    $reportObject = @()
    $userList = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq  $False} -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" |  Where-Object {$_.DisplayName -ne $null}
    $userList | %{
        $output = "" | Select DisplayName, ExpiryDate
        $output.DisplayName = $_.DisplayName
        $output.ExpiryDate = ([datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")).DateTime
        $reportObject += $output
        #Next 2 Lines provide debugging... I'm not sure the date time portion will work without having AD to play with
        #$output | fl *
        #([datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")).DateTime 
    }
    $reportObject | Convertto-CSV -NoTypeInformation
    $reportObject | Out-File c:\DATA\export.txt
    #$reportObject | Export-CSV "c:\DATA\export.csv" -NoTypeInformation
    #$reportObject | export-clixml c:\DATA\export.xml
    Gtz

    Bert
    Last edited by Rems; 27th August 2013, 10:40. Reason: [code]-tags added

  • #2
    Re: Export of Password Expiry script runs fine in cmd but not as scheduled task

    I am missing the Active Directory module import...?
    Code:
    Import-Module activedirectory
    /Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Export of Password Expiry script runs fine in cmd but not as scheduled task

      Hi Rems

      The script runs ok in Powershell via c:\script> .\ListPwdExpirationDate.ps1. The output on the screen is correct.

      Its just when exporting the data to txt/csv/xml that it only includes the header info.

      DisplayName ExpiryDate
      ----------- ----------
      maandag 1 januari 1601 1:00:00


      AD modules are imported so that should not be a problem.
      PS C:\script> Get-Module -ListAvailable
      ModuleType Name ExportedCommands
      ---------- ---- ----------------
      Manifest ActiveDirectory {}
      Manifest ADRMS {}
      Manifest AppLocker {}
      Manifest BestPractices {}
      Manifest BitsTransfer {}
      Manifest GroupPolicy {}
      Manifest PSDiagnostics {}
      Manifest ServerManager {}
      Manifest TroubleshootingPack {}
      Manifest WebAdministration {}

      any thoughts?

      Comment


      • #4
        Re: Export of Password Expiry script runs fine in cmd but not as scheduled task

        Originally posted by bertk View Post
        any thoughts?
        Yes, try putting Import-Module activedirectory as the first line in ListPwdExpirationDate.ps1

        /Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Export of Password Expiry script runs fine in cmd but not as scheduled task

          After running the script, is de array being filled?
          Marcel
          Technical Consultant
          Netherlands
          http://www.phetios.com
          http://blog.nessus.nl

          MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
          "No matter how secure, there is always the human factor."

          "Enjoy life today, tomorrow may never come."
          "If you're going through hell, keep going. ~Winston Churchill"

          Comment


          • #6
            Re: Export of Password Expiry script runs fine in cmd but not as scheduled task

            Rems
            After adding the Import-Module activedirectory to the script, it is working as it should. Thanks!!! I did not know you needed to add this to the script.

            Originally posted by Rems View Post
            Yes, try putting Import-Module activedirectory as the first line in ListPwdExpirationDate.ps1

            /Rems

            Comment


            • #7
              Re: Export of Password Expiry script runs fine in cmd but not as scheduled task

              Glad it is solved.

              the command Get-Module -ListAvailable lists all Modules that are available on the computer. It shows the modules available for import. Additionally, the command Get-Module shows the modules that are imported in the current shell environment.

              It is possible to load a module automatically any time you run PowerShell, by using a prepared shortcut. But, if the shell is lauched by a scheduled task it is not using the shortcut then you should add either -command import-module ActiveDirectory to the arguments to run the program / or, by adding this Cmdlet to the script file.

              /Rems

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment

              Working...
              X