Create schedule task on mulitple servers

Hi Guys,

I’ve created the following script for like ~250 server

$servers = Import-csv “C:\temp\servers.csv”
ForEach ($server in $servers){
New-Item “\$($Server.Servername)\c$\Test1” -type directory
Start-Sleep 1
Copy-Item D:\Scripting\Files\bla1.txt -Destination \$($Server.Servername)\c$\Test1

This works. Now i would like to create a scedule task on the servers remotly.

When i do it directly to one server i use:

$Action = New-ScheduledTaskAction -Execute “Cmd”
$Trigger = New-ScheduledTaskTrigger -Once -At 17:00
Register-ScheduledTask Bodhi -Action $Action -Trigger $Trigger

This works. But i would like to do it on all the servers but with foreach i cannot fix it.

Some help please :slight_smile:

why not use group policy and push the task that way ?

or if you wanted to do it via Powershell have a look at the bottom answer here using SCHTASKS.EXE

You might put your scheduled task commands into a script file, and then use the Invoke-Command cmdlet to run that script on each remote server. See the help for the Invoke-Command cmdlet for details.

Thanks Kevyn, i will look into Invoke-Command