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

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
    }

When you use “Select-Object -ExpandProperty Name” you don’t have a $.Name property anymore inside your Foreach - loop. I don’t know how your batch file looks like (actually I don’t know why you’re still using batch files) but with your code it should be only
Start-Process “WeekendReboot.bat” $

Thanks, I’ll give that a try. In short, we were using SCCM to schedule weekend reboots but found this method unreliable as many machines would reboot during the weekday. I was asked to put together a script that will perform the reboot and give the user the option to cancel. The powershell script above executes the following WeekendReboot.bat:

copy /y rebootbatchscript.bat \%1\c$\temp
SCHTASKS /s %1 /f /RU BUILTIN\Users /create /tn “Weekend Reboot” /tr “cmd.exe /k C:\temp\rebootbatchscript.bat” /sc ONCE /sd 03/01/2018 /st 00:00
SCHTASKS /s %1 /run /tn “Weekend Reboot”

Which copies the following locally to the machine (RebootBatchScript.bat):

@ECHO OFF
:myLabel
SHUTDOWN /R /F /T 600
SET /P continue="Your computer will restart in 10 min, do you want to abort (y/n): "
IF %continue% EQU y (
SHUTDOWN /A
ECHO Restart aborted.
TIMEOUT 5
Exit
)

I’m not particularly adept at scripting so I would greatly appreciate any advice on doing this more cleanly/efficiently. I’ve tried powershell alone, wmic, psexec, etc and haven’t found a way to do this with a cancellation prompt.

As for…

' haven't found a way to do this with a cancellation prompt. '

… you could just do this vs the whole batch file thing.

Taking your sample and Olaf comment…

    Import-Module ActiveDirectory

    Get-ADComputer -SearchBase 'OU=Test,DC=abc,DC=xyz,DC=com' -Filter '*' | 
    Select-Object -ExpandProperty Name |
     ForEach-Object{
            If((Get-Date).DayOfWeek -match 'Wednesday|Sunday')
            {
                # Warn the user, via a message box, that it is policy to restart on the the systme on weekends
                [void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

                $result = [Microsoft.VisualBasic.Interaction]::MsgBox( `
                  "It is a corproate policy to restart systems on weekends. 
                  Do you wish to do this now?", 
                  'YesNo,Question', "Policy Enforcement - Action Required!")

                # Process the user response
                switch ($result) 
                {
                  'Yes'		{ Restart-Computer -ComputerName $_.Name -WhatIf}
                  'No'		{ }
                }
            }
        }

Just remove the -WhatIf for the restart to happen.

Also, since you say…

I'm not particularly adept at scripting

Jump over to Microsoft Virtual Academy and take a no-cost PowerShell course and hit YouTube and search for ‘Beginning PowerShell’ for more.
See also the many learning posts/discussion on this forum, Just search for Learn PowerShell. There are lots of sample via MS PowerShellGallery.com and the MS Scripting guys blog as well as the TechNet PowerShell Survival Guide and all over the web of course.

social.technet.microsoft.com/wiki/contents/articles/183.windows-powershell-survival-guide.aspx

There are a lot of no-cost eBooks, and good paid for ones as well. Use this forums search box for some of them. There are plenty of resources to help you.

    # Get parameters, examples, full and Online help for a cmdlet or function

    Get-Command -CommandType Function
    Get-Command -CommandType Cmdlet

    (Get-Command -Name Get-Content).Parameters
    Get-help -Name Get-Content -Examples
    Get-help -Name Get-Content -Full
    Get-help -Name Get-Content -Online

    Get-Help about_*
    Get-Help about_Functions

    # Find all cmdlets / functions with a target parameter
    Get-Help * -Parameter Append

    # All Help topics locations
    explorer "$pshome\$($Host.CurrentCulture.Name)"

postanote,

I tried your powershell script without batch but unfortunately the cancellation prompt is rendered on the machine running the script rather than the machines being rebooted.

Thanks,
Dmitriy