locked
Script to get list of computers from OU and pass on name as argument RRS feed

  • Question

  • Hi All,

    I have a batch script that takes a computer name as its first argument and schedules a task to reboot that computer. I need to write a powershell script that queries an OU for computer names and passes that name to the reboot batch script. I'm successfully getting the desired list but having trouble passing it to the batch script. Hoping someone can help!

    Current powershell script:

    Import-Module ActiveDirectory
    Get-ADComputer -SearchBase 'OU=Test,DC=abc,DC=xyz,DC=com' -Filter '*' | Select -Exp Name |
     ForEach-Object{
            Start-Process "WeekendReboot.bat" $_.Name
        }

    Wednesday, March 14, 2018 3:42 PM

Answers

  • I believe the recommended option is good but I went this route:

    When you use "Select-Object -ExpandProperty Name" you don't have a $_.Name property anymore inside your Foreach – loop. It should be only Start-Process "WeekendReboot.bat" $_

    • Marked as answer by iTsLiKeAnEgG Thursday, March 15, 2018 3:14 PM
    Thursday, March 15, 2018 3:14 PM

All replies

  • Don't use "-Exp"

    \_(ツ)_/

    Wednesday, March 14, 2018 4:19 PM
  • Thanks, will give that a try!
    Wednesday, March 14, 2018 5:25 PM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 15, 2018 3:04 AM
  • I believe the recommended option is good but I went this route:

    When you use "Select-Object -ExpandProperty Name" you don't have a $_.Name property anymore inside your Foreach – loop. It should be only Start-Process "WeekendReboot.bat" $_

    • Marked as answer by iTsLiKeAnEgG Thursday, March 15, 2018 3:14 PM
    Thursday, March 15, 2018 3:14 PM