Need to export all computers from 2 OU's into 1 .CSV file?

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of Wade Hintze Wade Hintze 3 years, 3 months ago.

  • Author
    Posts
  • #12324
    Profile photo of Wade Hintze
    Wade Hintze
    Participant

    I'm new to powershell and i'm trying to link 2 statements together and list all computers in 2 different OU's in 1 .txt or .csv. Idealy I would like to reboot those computers as well? I'm using get-computer -filter * -searchbase "ou=example dc=microsoft, dc=com" | select name ; get-adcomputer -filter * -searchbase "ou=esxample dc=microsoft, dc=com" | select name | export-csv c:\example? Any help would be great.

  • #12325
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I'd run a separate command for each OU. On the second, use the -Append option on Export-CSV (present in v3, I think). Rebooting them would become a third command, I imagine. Trying to do all of that in a one-liner is a bit ambitious and doesn't really offer a benefit.

  • #12328
    Profile photo of Wade Hintze
    Wade Hintze
    Participant

    I dont really need to do it all in one line. I'm trying to find the best way to write this out?

  • #12329
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Well, I think you've pretty much got it. You just need to separate your two statements. The semicolon "ends" one pipeline, which is why what you've got isn't working; it doesn't "combine" the output of the first command with that of the second.

    get-adcomputer -filter * -searchbase “ou=example2, dc=microsoft, dc=com” | select name | export-csv c:\example.csv 
    get-adcomputer -filter * -searchbase “ou=example1, dc=microsoft, dc=com” | select name | export-csv c:\example.csv -append
    

    That gets you one CSV file. Restart-Computer accepts pipeline input, and can bind a "ComputerName" property to its -ComputerName parameter. So the problem with your CSV right now is that it has a Name property, not a ComputerName property. You can fix that in the pipeline.

    Import-CSV c:\example.csv | Select @{label='computername';expression={$_.name}} | restart-computer -verbose
    

    That takes your CSV file as input, attaches a "computername" property that contains the same thing as the current "name" property (e.g. column), and lets Restart-Computer do the rest.

    That help a bit?

  • #12332
    Profile photo of Wade Hintze
    Wade Hintze
    Participant

    Thanks Don! That helps. Can I make that in a 3 line script and then run a scheduled task?

  • #12333
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Don't see why not.

  • #12334
    Profile photo of Wade Hintze
    Wade Hintze
    Participant

    Don if i would have said | select -computername instead of | select name would i been able to restart without this part( Select @{label='computername';expression={$_.name}} ). What does the -verbose do for the command? Thanks again!

  • #12335
    Profile photo of Don Jones
    Don Jones
    Keymaster

    No, because the ADComputer objects don't have a ComputerName property. They have a Name property.

    Get-ADComputer -searchbase "ou=whatever,dc=domain,dc=com" -filter * | Get-Member
    

    No ComputerName property, so you can't select it. Like, Chapter 8 in "Learn PowerShell 3 in a Month of Lunches," I think. "The Pipeline, Deeper" is the chapter name.

    -Verbose is a common parameter. It enables more verbose output from the command – e.g., a better idea of what it's doing. Read about_common_parameters on that one.

  • #12336
    Profile photo of Wade Hintze
    Wade Hintze
    Participant

    ok awesome. I'm going to have to get that book. 🙂

You must be logged in to reply to this topic.