0

I have a PowerShell command like this:

Search-ADAccount -AccountDisabled -UsersOnly | FT Name 
      > C:\Users\hou\Downloads\DisabledAccount.csv

This command can grab all disabled account names from AD and put it into a .CSV file.

I want to set up a job in SQL Server agent so it will run the command whenever I need it.

But the Agent keep gives me error when I was trying to run the job.

Can anyone let me know the right command for this while running in SQL Server agent?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jack
  • 281
  • 1
  • 3
  • 17
  • 2
    `But the Agent keep gives me error when I was trying to run the job.` Share error message – Lukasz Szozda Sep 21 '15 at 19:02
  • 1
    The command should be `Search-ADAccount -AccountDisabled -UsersOnly | Export-CSV -notypeinformation C:\Users\hou\Downloads\DisabledAccount.csv`. You didnt have a csv file... just a text file. Sure the error was about the input file. – Matt Sep 21 '15 at 19:31
  • Do I need to add something like Invoke xxxx before the command. Cuz this will comes out when I try to run it: The term 'Search-ADAccount' is not recognized as the name of a cmdlet, function scriot file or operable program @Matt – Jack Sep 22 '15 at 15:34
  • That was the error you were getting? Well this means that you do not have the activedirectory module available where you are running this code. – Matt Sep 22 '15 at 15:34
  • I do have AD Module enabled and I can run my code under powershell and generate a csv file without any problem, it just dont let me do that using SQL Server Job Agent. dont understand. – Jack Sep 22 '15 at 15:43
  • Your powershell session might load it but it does not mean sql will. Possibly just need to add `import-module activedirectory`. Also use the @ symbol to ping users you are chatting to. I didnt know you responded until I checked just now. – Matt Sep 23 '15 at 16:14

1 Answers1

0

You might have other issues but the glaring first one you have is you do not have a CSV file. If you opened the file you would see that it is not following any CSV standard but that of a formatted table.

Search-ADAccount -AccountDisabled -UsersOnly | 
    Export-CSV -notypeinformation C:\Users\hou\Downloads\DisabledAccount.csv

Format-Table is just for showing information on the screen. Not for use of generating output. If you want to make a CSV then that is what Export-CSV is there to do. If you only wanted the name column then you could add a Select-Object in the pipe.

Search-ADAccount -AccountDisabled -UsersOnly | 
    Select-Object Name |
    Export-CSV -notypeinformation C:\Users\hou\Downloads\DisabledAccount.csv
Matt
  • 45,022
  • 8
  • 78
  • 119
  • Do I need to add something like Invoke xxxx before the command. Cuz this will comes out when I try to run it: The term 'Search-ADAccount' is not recognized as the name of a cmdlet, function scriot file or operable program – Jack Sep 22 '15 at 15:33